1、pom.xml

  1. <!-- mybatis分页插件 -->
  2. <dependency>
  3. <groupId>com.github.pagehelper</groupId>
  4. <artifactId>pagehelper-spring-boot-starter</artifactId>
  5. <version>1.1.1</version>
  6. </dependency>

2、驼峰命名
在application.properties中添加以下配置,在执行查询后,可以将数据库的NN_NN格式字段,在java结果集对象中自动转换成驼峰命名参数。

  1. mybatis.configuration.mapUnderscoreToCamelCase=true

3、可复用的@Results
a、声明时给id赋值为user

  1. @Results(id="user",value={
  2. @Result(property="nnNn",column="NN_NN")
  3. })

b、在其他方法中,重复使用id为user的结果映射

  1. @ResultMap("user")

c、结果映射@Results
如果结果集不是JAVA对象而是Map,Map中的列名会和数据库中的NN_NN一样,是不会自动驼峰转换的。可以使用@Result来指明结果映射,同样也适用JAVA对象

  1. @Results({
  2. @Result(property="nnNn",column="NN_NN")
  3. })
  4. @Select("select * from user")
  5. public List<Map> findAll();

4、打印SQL日志到控制台
在application.properties中添加以下配置

  1. logging.level.你的包名.mybatis接口包=debug
    eglogging.level.com.lynch.mapper.PageMapper=debug #不能用*替代


第一行:==>左边是执行SQL的接口及其方法,右边是执行语句
第二行:传参数1,String类型
第三行:查到一行数据

5、分页

  1. package com.lynch.mapper;
  2.  
  3. import java.util.List;
  4.  
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Service;
  7. import org.springframework.transaction.annotation.Transactional;
  8.  
  9. import com.github.pagehelper.Page;
  10. import com.github.pagehelper.PageHelper;
  11. import com.lynch.entity.UserEntity;
  12.  
  13. @Service
  14. @Transactional
  15. public class PageServiceImpl {
  16. @Autowired
  17. private PageMapper pageMapper;
  18.  
  19. public Page<UserEntity> pageUser(int pageNum, int pageSize) {
  20. // 分页插件: 查询第1页,每页10行
  21. Page<UserEntity> page = PageHelper.startPage(pageNum, pageSize);
  22. pageMapper.getAll();
  23. // 数据表的总行数
  24. page.getTotal();
  25. // 分页查询结果的总行数
  26. page.size();
  27. // 第一个User对象,参考list,序号0是第一个元素,依此类推
  28. page.get(0);
  29.  
  30. return page;
  31. }
  32. }

分页原理:PageHelper.startPage会拦截下一个sql,也就是pageMapper.getAll()的SQL。并且根据当前数据库的语法,把这个SQL改造成一个高性能的分页SQL,同时还会查询该表的总行数,具体可以看SQL日志。
PageHelper.startPage和pageMapper.getAll()最好紧跟在一起,中间不要有别的逻辑,否则可能出BUG。
Page<User> page:相当于一个list集合,findAll()方法查询完成后,会给page对象的相关参数赋值。

6、回传ID
假设数据库表的ID主键是自动增长的,现在添加一条数据,想要得到这条数据自动增长的ID,方法如下:
a、dao层
useGeneratedKeys=true:获取数据库生成的主键
keyProperty="id":把主键值存入User param对象的id属性

  1. @Insert("insert into users(username,password,sex) values(#{username}, #{password}, #{sex})")
  2. @Options(useGeneratedKeys=true, keyProperty="id")
  3. int insert(UserEntity user);

b、service层

  1. UserEntity userEntity = new UserEntity("laosis", "123456", SexEnum.WOMAN);
  2. int result = pageMapper.insert(userEntity);
  3. System.out.println("result=" + result);
  4. System.out.println("回传ID值:" + userEntity.getId());

完整demo代码
1、PageMapper

  1. package com.lynch.mapper;
  2.  
  3. import java.util.List;
  4.  
  5. import org.apache.ibatis.annotations.Insert;
  6. import org.apache.ibatis.annotations.Options;
  7. import org.apache.ibatis.annotations.Select;
  8.  
  9. import com.lynch.entity.UserEntity;
  10.  
  11. public interface PageMapper {
  12. @Select("select * from users order by id")
  13. List<UserEntity> getAll();
  14.  
  15. @Insert("insert into users(username,password,sex) values(#{username}, #{password}, #{sex})")
  16. @Options(useGeneratedKeys=true, keyProperty="id")
  17. int insert(UserEntity user);
  18. }

2、PageServiceImpl -- 分页service

  1. package com.lynch.mapper;
  2.  
  3. import java.util.List;
  4.  
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Service;
  7. import org.springframework.transaction.annotation.Transactional;
  8.  
  9. import com.github.pagehelper.Page;
  10. import com.github.pagehelper.PageHelper;
  11. import com.lynch.entity.UserEntity;
  12.  
  13. @Service
  14. @Transactional
  15. public class PageServiceImpl {
  16. @Autowired
  17. private PageMapper pageMapper;
  18.  
  19. public Page<UserEntity> pageUser(int pageNum, int pageSize) {
  20. // 分页插件: 查询第1页,每页10行
  21. Page<UserEntity> page = PageHelper.startPage(pageNum, pageSize);
  22. pageMapper.getAll();
  23. // 数据表的总行数
  24. page.getTotal();
  25. // 分页查询结果的总行数
  26. page.size();
  27. // 第一个User对象,参考list,序号0是第一个元素,依此类推
  28. page.get(0);
  29.  
  30. return page;
  31. }
  32. }

3、PageMapperTest -- 单元测试

  1. package com.lynch.mapper;
  2.  
  3. import java.util.List;
  4.  
  5. import org.junit.Test;
  6. import org.junit.runner.RunWith;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.boot.test.context.SpringBootTest;
  9. import org.springframework.test.context.junit4.SpringRunner;
  10.  
  11. import com.github.pagehelper.Page;
  12. import com.lynch.entity.UserEntity;
  13. import com.lynch.enums.SexEnum;
  14.  
  15. @RunWith(SpringRunner.class)
  16. @SpringBootTest
  17. public class PageMapperTest {
  18. @Autowired
  19. private PageMapper pageMapper;
  20. @Autowired
  21. private PageServiceImpl pageService;
  22.  
  23. @Test
  24. public void getAll() throws Exception {
  25. List<UserEntity> users = pageMapper.getAll();
  26. for(UserEntity user : users) {
  27. System.out.println(user);
  28. }
  29.  
  30. }
  31.  
  32. @Test
  33. public void pageUser() throws Exception {
  34. int pageNum = 1;
  35. int pageSize = 10;
  36. Page<UserEntity> page = pageService.pageUser(pageNum, pageSize);
  37. System.out.println("page:" + page);
  38. for(UserEntity user : page.getResult()) {
  39. System.out.println(user);
  40. }
  41.  
  42. }
  43.  
  44. @Test
  45. public void insert() throws Exception {
  46. UserEntity userEntity = new UserEntity("laosis", "123456", SexEnum.WOMAN);
  47. int result = pageMapper.insert(userEntity);
  48. System.out.println("result=" + result);
  49. System.out.println("回传ID值:" + userEntity.getId());
  50. }
  51.  
  52. }

SpringBoot+Mybatis+Pagehelper分页的更多相关文章

  1. springboot mybatis pagehelper 分页问题

    1:添加依赖 compile group: 'com.github.pagehelper', name: 'pagehelper-spring-boot-starter', version: '1.2 ...

  2. springboot + mybatis +pageHelper分页排序

    今天下午写查出来的数据的排序,原来的数据没有排序,现在把排序功能加上...原来用的,是xml中的sql动态传参 ,,1个小数没有弄出来,果断放弃... 网上百度一下,发现用pageHelper  可以 ...

  3. SpringBoot+MyBatis+PageHelper分页无效

    POM.XML中的配置如下:<!-- 分页插件 --><!-- https://mvnrepository.com/artifact/com.github.pagehelper/pa ...

  4. SpringBoot+Mybatis+PageHelper实现分页

    SpringBoot+Mybatis+PageHelper实现分页 mybatis自己没有分页功能,我们可以通过PageHelper工具来实现分页,非常简单方便 第一步:添加依赖 <depend ...

  5. springboot+mybatis+pagehelper

    springboot+mybatis+pagehelper整合 springboot   版本2.1.2.RELEASE mybatis  版本3.5 pagehelper 版本5.18 支持在map ...

  6. Springboot+Mybatis+Pagehelper+Aop动态配置Oracle、Mysql数据源

      本文链接:https://blog.csdn.net/wjy511295494/article/details/78825890 Springboot+Mybatis+Pagehelper+Aop ...

  7. spring-boot + mybatis +pagehelper 使用分页

    转自:https://segmentfault.com/a/1190000015668715?utm_medium=referral&utm_source=tuicool 最近自己搭建一个sp ...

  8. springboot + mybatis配置分页插件

    一:使用pagehelper配置分页插件 1:首先配置springboot +mybatis框架  参考:http://www.cnblogs.com/liyafei/p/7911549.html 2 ...

  9. mybatis pagehelper 分页 失效

    pagehelper 不分页几种情况的解决方法 - web洋仔 - CSDN博客https://blog.csdn.net/csdn___lyy/article/details/77160488 分页 ...

随机推荐

  1. abp xunit Can not register IHostingEnvironment. It should be a non-abstract class. If not, it should be registered before.”

    在测试项目的ServiceCollectionRegistrar类提前注册.

  2. 图片利用 new Image()预加载原理 和懒加载的实现原理

    二:预加载和懒加载的区别 预加载与懒加载,我们经常经常用到,这些技术不仅仅限于图片加载,我们今天讨论的是图片加载: 图片预加载:顾名思义,图片预加载就是在网页全部加载之前,提前加载图片.当用户需要查看 ...

  3. LOJ-10108(欧拉回路+并查集)一个图至少用几笔画成

    题目链接:传送门 思路: 用并查集统计出每个区块奇数个节点的个数x,每个区块对笔画的贡献是max(x/2,1): 然后每个区块求和即可. #include<iostream> #inclu ...

  4. Win7 VS2015编译wxWidgets-3.1.0

    下载 https://www.wxwidgets.org/downloads/ 打开SLN工程 D:\CPPLibs\wxWidgets-3.1.0\build\msw\wx_vc14.sln 编译 ...

  5. MySQL之二 yum安装及初识

      安装   yum install mysql-server chkconfig -list mysqld 查看mysqld服务是否为开机启动 chkconfig mysqld on 设为开机启动 ...

  6. Exception、Error、运行时异常与一般异常有何异同

    转自博客  https://blog.csdn.net/m0_37531231/article/details/79502778 一.开场白 对于程序运行过程中的可能出现异常情况,java语言使用一种 ...

  7. 仿boost::any的泛型指针类any的实现

    在boost库中,any是一种特殊容器,只能容纳一个元素,但这个元素可以是任意的类型----int.double.string.标准容器或者任何自定义类型.程序可以用any保存任意的数据,也可以在任何 ...

  8. shell 文件操作

      序   在linux平台下开发,我们经常会接触到一些任务性质的工作,而处理方式多样化.现积累各个案例.   从远程服务器拉取文件到本地服务器   scp work@cp01-xx-dev.com: ...

  9. MongoDB学习小结

    启动对应server:cd:到mangodb安装根目录下 mongod --dbpath db路径 创建MangoDB服务: mongod.exe --logpath d:/mongodb/logs/ ...

  10. 从零开始的程序逆向之路 第一章——认识OD(Ollydbg)以及常用汇编扫盲

    作者:Crazyman_Army 原文来自:https://bbs.ichunqiu.com/thread-43041-1-1.html 0×00 序言: 1.自从上次笔者调戏完盗取文件密码大黑客后, ...