Springboot整合myBatis(附加pagehelper分页插件)
前提
大家好,我在身边的人当着发现对spring boot整合mybatis和pageHepler的理解不是很好,经常会报错,所以我编写了此文档,给刚入门的人观看,仅限自己的理解,如果有问题的地方,希望大家可以提出,我一定修改。
第一步,创建spring boot,导入maven依赖
依赖共6个
- spring-boot-starter-web ,springmvc的依赖;
- mybatis-spring-boot-starter ,springboot整合mybatis依赖;
- mysql-connector-java ,springboot整合mysql依赖;
- lombok ,springboot整合实体类的getsettostring等方法依赖;
- spring-boot-starter-test ,单元测试依赖;
- pagehelper-spring-boot-starter ,分页依赖;
代码
<!--springmvc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--springboot整合mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<!--springboot整合mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--springboot整合实体类的getsettostring等方法-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--单元测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--分页-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
第二步,进行appliction配置
-
配置mybatis扫描文件:mybatis.mapper-locations= classpath:com/bdqn//mapper/.xml
-
配置mybatis打印sql语句:mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
-
配置连接数据库:
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver -
分页配置
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
代码(将application后缀名改为yml,这是springboot推荐支持的,带提示)
mybatis:
#扫描xml文件
mapper-locations: classpath:com/bdqn/*/mapper/*.xml
#打印sql语句
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# map-underscore-to-camel-case: true
#配置连接数据库
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
#分页配置
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
第三步,将springboot启动类进行扫描接口配置
扫描Mapper接口
@MapperScan(basePackages={“com.bdqn.*.dao”})
扫描第二种方法,在mapper接口上进行@Mapper,当项目运行后,进行扫描
第四步创建三层架构项目
第四步,运行项目,访问Controller接口
测试结果
控制台打印的sql语句
分页插件pagehelper
springboot整合mybatis最简单的一个demo已经完成了,下面就来说一说分页如何编写。
分页有固定的接受参数,第一是当前页数,第二是每页个数。返回值是总数据条数和当前页码的数据。
在业务逻辑层ServiceImpl中进行编写。
@Override
public PageInfo<Group> getPage(Integer pageNum, Integer pageSize) {
//分页处理,显示第pageNum页的pageSize条数据
PageHelper.startPage(pageNum,pageSize);
//查询分组列表
List<Group> list = groupDao.getAll();
// 获取分页信息
PageInfo<Group> infoList = new PageInfo<>(list);
return infoList;
}
1.在前台获得pageNum和pageSize
2.在PageHelper中定义当前页码和显示条数
3.通过PageInfo获得分页信息
4.返回PageInfo对象
在分页插件的PageInfo中还有很多的属性:
pageNum 当前页
pageSize 每页的数量
size 当前页的数量
startRow 当前页面第一个元素在数据库中的行号
endRow 当前页面最后一个元素在数据库中的行号
total 总记录数
pages 总页数
list 结果集
等等。。。。
还有一些属性没有介绍。
总结
本文章表名了springboot整合mybatis,外加附带讲解mybatis的分页插件。如果有讲的模糊不清晰的地方,希望大家提出来,一起共同进步。
如果有对springboot整合mybatisPlus有兴趣的可以查看我的文章,在里面也有详细的讲解和mybatisPlus的分页都有讲解。