需求分析:
我们在前端界面获取用户表的时候,在界面上一次显示出成百上千条数据,用户体验,软件性能都会很糟糕,假设数据库内存储十万条记录,后端一次性返回这么多数据,前端和测试小姐姐会杀了你的。
那么,我们就考虑一次查询,返回可以接收的一组记录数,你好,我好,大家好。
这时候,最好的解决方案,就是分页查询。
com.github.pagehelper
pagehelper
5.2.0
在MyBatis的核心配置文件中配置插件
我们运用动态Sql 给t_user 表中插入100条数据
/**
* 利用动态sql 批量插入数据
*/
int insertBatch(@Param("users") List users);
insert into t_user values
(null,#{user.userName},#{user.passWord},#{user.age},#{user.gender},#{user.email})
@Test
public void testInsertBatch(){
UserMapper mapper = SqlSessionUtil.getMapper(UserMapper.class);
List users = new ArrayList<>();
for(int i=0;i<100;i++){
User user = new User("user"+i //username
, UUID.randomUUID().toString().substring(0,6) // password
,i //age
,i%2==0?"男":"女" //gender
,i+"@gmail.com"); //email
users.add(user);
}
int i = mapper.insertBatch(users);
System.out.println("添加了 "+i+" 行记录");
}
/**
* 测试密码生成器
*/
@Test
public void testRandom(){
//方案1
String str = UUID.randomUUID().toString().substring(0, 7);
System.out.println(str);
//方案2
String source = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
Random random = new Random();
StringBuffer flag = new StringBuffer();
for(int j=0;j<6;j++){
flag.append(source.charAt(random.nextInt(62))); //随机数产生10以内的任何一个数字,代表stringbuff下标
}
System.out.println(flag.toString());
}
/**
* 分页测试
*/
@Test
public void testLimitPages(){
UserMapper mapper = SqlSessionUtil.getMapper(UserMapper.class);
PageHelper.startPage(2,10);
List users = mapper.getUserLimitPage();
System.out.println(users);
PageInfo pageInfo = new PageInfo<>(users, 5);
System.out.println(pageInfo);
}
}
数据库插入成功
分页查询测试:
/**
* 分页查询
* 你也可以根据条件去查询
*/
List getUserLimitPage();
/**
* 分页测试
*/
@Test
public void testLimitPages(){
UserMapper mapper = SqlSessionUtil.getMapper(UserMapper.class);
PageHelper.startPage(2,10);
List users = mapper.getUserLimitPage();
System.out.println(users);
PageInfo pageInfo = new PageInfo<>(users, 5);
System.out.println(pageInfo);
}
测试结果:
Page{count=true, pageNum=2, pageSize=10, startRow=10, endRow=20, total=103, pages=11, reasonable=false, pageSizeZero=false}[User{id=14, userName='user7', passWord='47a047', age=7, gender='女', email='[email protected]'}, User{id=15, userName='user8', passWord='20fc70', age=8, gender='男', email='[email protected]'}, User{id=16, userName='user9', passWord='1b59a2', age=9, gender='女', email='[email protected]'}, User{id=17, userName='user10', passWord='05555b', age=10, gender='男', email='[email protected]'}, User{id=18, userName='user11', passWord='8c89f5', age=11, gender='女', email='[email protected]'}, User{id=19, userName='user12', passWord='721c69', age=12, gender='男', email='[email protected]'}, User{id=20, userName='user13', passWord='d1ce22', age=13, gender='女', email='[email protected]'}, User{id=21, userName='user14', passWord='3d47e2', age=14, gender='男', email='[email protected]'}, User{id=22, userName='user15', passWord='7f7e24', age=15, gender='女', email='[email protected]'}, User{id=23, userName='user16', passWord='b877be', age=16, gender='男', email='[email protected]'}]
PageInfo{pageNum=2, pageSize=10, size=10, startRow=11, endRow=20, total=103, pages=11, list=Page{count=true, pageNum=2, pageSize=10, startRow=10, endRow=20, total=103, pages=11, reasonable=false, pageSizeZero=false}[User{id=14, userName='user7', passWord='47a047', age=7, gender='女', email='[email protected]'}, User{id=15, userName='user8', passWord='20fc70', age=8, gender='男', email='[email protected]'}, User{id=16, userName='user9', passWord='1b59a2', age=9, gender='女', email='[email protected]'}, User{id=17, userName='user10', passWord='05555b', age=10, gender='男', email='[email protected]'}, User{id=18, userName='user11', passWord='8c89f5', age=11, gender='女', email='[email protected]'}, User{id=19, userName='user12', passWord='721c69', age=12, gender='男', email='[email protected]'}, User{id=20, userName='user13', passWord='d1ce22', age=13, gender='女', email='[email protected]'}, User{id=21, userName='user14', passWord='3d47e2', age=14, gender='男', email='[email protected]'}, User{id=22, userName='user15', passWord='7f7e24', age=15, gender='女', email='[email protected]'}, User{id=23, userName='user16', passWord='b877be', age=16, gender='男', email='[email protected]'}], prePage=1, nextPage=3, isFirstPage=false, isLastPage=false, hasPreviousPage=true, hasNextPage=true, navigatePages=5, navigateFirstPage=1, navigateLastPage=5, navigatepageNums=[1, 2, 3, 4, 5]}
我们可以看到,当前页码是2,每页显示10条,刚好从第14条开始显示。(起始页码是1 ,注意我的表数据的id ,1后直接就是5)
如果你 pageNum写的是0,它返回一个空的结果集
Page{count=true, pageNum=0, pageSize=10, startRow=0, endRow=0, total=103, pages=11, reasonable=false, pageSizeZero=false}[]
注意事项:
在查询功能之前使用PageHelper.startPage(int pageNum, int pageSize)开启分页功能
pageNum:当前页的页码
pageSize:每页显示的条数
在查询获取list集合之后,使用PageInfo pageInfo = new PageInfo<>(List list, int navigatePages)获取分页相关数据
list:分页之后的数据
navigatePages:导航分页的页码数
分页相关数据:
PageInfo{ pageNum=8, pageSize=4, size=2, startRow=29, endRow=30, total=30, pages=8, list=Page{count=true, pageNum=8, pageSize=4, startRow=28, endRow=32, total=30, pages=8, reasonable=false, pageSizeZero=false}, prePage=7, nextPage=0, isFirstPage=false, isLastPage=true, hasPreviousPage=true, hasNextPage=false, navigatePages=5, navigateFirstPage4, navigateLastPage8, navigatepageNums=[4, 5, 6, 7, 8] }
pageNum:当前页的页码
pageSize:每页显示的条数
size:当前页显示的真实条数
total:总记录数
pages:总页数
prePage:上一页的页码
nextPage:下一页的页码
isFirstPage/isLastPage:是否为第一页/最后一页
hasPreviousPage/hasNextPage:是否存在上一页/下一页
navigatePages:导航分页的页码数
navigatepageNums:导航分页的页码,[1,2,3,4,5]
_______________________________________________________________________________________________
git推送到远程仓库的时候(push),容易报 OpenSSL SSL_read: Connection was reset, errno 10054
在本地仓库文件夹下 bash里输入:
git config --global http.sslVerify "false"
再不行,就科学上网吧。
代码仓库地址:
GitHub - Chai-Feng/git-demo01