专业网站开发/b站推广网站2024年不用下载
实现步骤
- 在mysql数据库中定义存储过程,并通过游标返回
- 编写实体类用于接收数据库查询返回的数据
- 在springboot中编写mapper.xml,添加查询语句
- 在测试类中进行测试
- 测试结果展示
定义存储过程并通过游标返回
#创建存储过程
create procedure queryAllUser()
begin#创建 由于接收游标值得变量declare username varchar(255);declare password varchar(100);#游标结束的标志declare done boolean default true;#查询SQLdeclare cur cursor for select * from user;#定义标识declare continue handler for not found set done=false; #如果该表存在则删除drop temporary table if exists copytbale;#创建临时表create temporary table copytable(username varchar(100),password varchar(255));#开启游标open cur;#循环获取数据while done do#获取数据并添加到声明的变量中fetch cur into username,password;#判断标识是否为trueif done = true then#将数据添加到临时表中insert into copytable values(username,password);end if;end while;close cur;#返回统计的记录select * from copytable;
end;
#删除临时表
drop table copytable;
#调用存储过程
call queryAllUser();
编写实体类与查询的表对应
@AllArgsConstructor
@NoArgsConstructor
@Data
public class User {private String username;private String password;
}
mapper.xml编写
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lifly.mapper.UserMapper"><select id="findAll" statementType="CALLABLE" resultType="User">{call queryAllUser()}</select>
</mapper>
测试类
@Testpublic void TestFindAll(){List<User> all = userMapper.findAll();System.out.println(all);}