Springboot集成Mybatis+PageHelper
1、Springboot项目引入mysql和mybatis的依赖:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
<scope>runtime</scope>
</dependency>
2、在application.yml配置数据库连接信息,Mybatis信息,是否打印SQL等(具体看注释):
# Springboot2.1.2默认引入的mysql版本version是8.0.13,其driver-class-name为com.mysql.cj.jdbc.Driver
spring:
datasource:
url: jdbc:mysql://118.25.190.197:3306/order?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: ******
driver-class-name: com.mysql.jdbc.Driver
# type-aliases-package批量设置别名作用:就是在mapper.xml文件中直接写类名,不配置就需要写类的全路径名
mybatis:
type-aliases-package: com.example.demo.entity
mapper-locations: classpath:mapping/*.xml
# 将mapper接口所在包的日志级别改成debug,可以在控制台打印sql
logging:
level:
com.example.demo.mapper: debug
3、编写Mapper接口(普通接口,无需任何注解参与)统一放在com.example.demo.mapper包下,编写mapper.xml文件(根据上面的配置,统一放在resources/mapping下)
4、在启动类上加注解,扫描Mapper接口所在包路径:
@MapperScan("com.example.demo.mapper")
注:
也可以不在主类使用@MapperScan,而在每个Mapper接口上面添加@Mapper注解,效果一样!
Mapper接口方法有多个参数需要加@Param(“参数名”)注解,否则抛异常说找不到参数。
以上集成完成mybatis,如需集成druid数据库连接池:
5、添加druid依赖:
<!-- alibaba的druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
6、修改application.yml,添加数据库连接池的配置,完成以后内容如下:
# mysql version 8.0.13的driver-class-name:com.mysql.cj.jdbc.Driver
spring:
datasource:
url: jdbc:mysql://118.25.190.197:3306/order?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
# druid
type: com.alibaba.druid.pool.DruidDataSource
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
# type-aliases-package批量设置别名作用:就是在mapper.xml文件中直接写类名,不配置就需要写类的全路径名
mybatis:
type-aliases-package: com.example.demo.entity
mapper-locations: classpath:mapping/*.xml
# 将mapper接口所在包的日志级别改成debug,可以在控制台打印sql
logging:
level:
com.example.demo.mapper: debug
如需集成PageHelper分页:
7、添加依赖:
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
</dependency>
8、简单使用,采用默认配置即可,调用controller接口传入pageNum(即当前页码,从1开始)和pageSize(即每页条数),添加PageHepler(在开始查询之前)和PageInfo两条语句,原来的查询不需要任何修改:
@GetMapping("/selectByPage/{pageNum}/{pageSize}")
public PageInfo<GdUsers> selectByPage(@PathVariable int pageNum, @PathVariable int pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<GdUsers> list = gdUsersMapper.selectByPage(); //得到从pageNum开始的pageSize条数据
//如果直接返回list,得到了分页的数据,如果添加下面步骤,返回pageInfo,则能得到包括list在内的分页信息
PageInfo<GdUsers> pageInfo = new PageInfo<>(list);
return pageInfo;
}
9、访问http://localhost:8080/index/1/5,控制台打印2条Sql(一条查询总数,一条查询分页数据):
2019-01-31 23:26:29.749 DEBUG 14696 --- [nio-8080-exec-9] c.e.d.m.G.selectByPage_COUNT : ==> Preparing: SELECT count(0) FROM gd_users
2019-01-31 23:26:29.750 DEBUG 14696 --- [nio-8080-exec-9] c.e.d.m.G.selectByPage_COUNT : ==> Parameters:
2019-01-31 23:26:29.823 DEBUG 14696 --- [nio-8080-exec-9] c.e.d.m.G.selectByPage_COUNT : <== Total: 1
2019-01-31 23:26:29.824 DEBUG 14696 --- [nio-8080-exec-9] c.e.d.mapper.GdUsersMapper.selectByPage : ==> Preparing: select * from gd_users LIMIT ?
2019-01-31 23:26:29.824 DEBUG 14696 --- [nio-8080-exec-9] c.e.d.mapper.GdUsersMapper.selectByPage : ==> Parameters: 5(Integer)
2019-01-31 23:26:29.893 DEBUG 14696 --- [nio-8080-exec-9] c.e.d.mapper.GdUsersMapper.selectByPage : <== Total: 5
返回的pageInfo数据:

注释:
网上说下面2句代码之间不能插入其它逻辑,否则分页不成功,我测试可以,但最好不要在中间插入别的数据库操作(未测):
PageHelper.startPage(pageNum, pageSize);
List<GdUsers> list = gdUsersMapper.selectByPage();
SQL 映射文件有很少的几个顶级元素(按照它们应该被定义的顺序):
cache – 给定命名空间的缓存配置。
cache-ref – 其他命名空间缓存配置的引用。
resultMap – 是最复杂也是最强大的元素,用来描述如何从数据库结果集中来加载对象。
parameterMap – 已废弃!老式风格的参数映射。内联参数是首选,这个元素可能在将来被移除,这里不会记录。
sql – 可被其他语句引用的可重用语句块。
insert – 映射插入语句
update – 映射更新语句
delete – 映射删除语句
select – 映射查询语句
Springboot集成Mybatis+PageHelper的更多相关文章
- SpringBoot集成Mybatis并具有分页功能PageHelper
SpringBoot集成Mybatis并具有分页功能PageHelper 环境:IDEA编译工具 第一步:生成测试的数据库表和数据 SET FOREIGN_KEY_CHECKS=0; ...
- BindingException: Invalid bound statement (not found)问题排查:SpringBoot集成Mybatis重点分析
重构代码,方法抛出异常:BindingException: Invalid bound statement (not found) 提示信息很明显:mybatis没有提供某方法 先不解释问题原因和排查 ...
- springboot集成mybatis(二)
上篇文章<springboot集成mybatis(一)>介绍了SpringBoot集成MyBatis注解版.本文还是使用上篇中的案例,咱们换个姿势来一遍^_^ 二.MyBatis配置版(X ...
- springboot集成mybatis(一)
MyBatis简介 MyBatis本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation迁移到了google code,并且改名为MyB ...
- SpringBoot 集成Mybatis 连接Mysql数据库
记录SpringBoot 集成Mybatis 连接数据库 防止后面忘记 1.添加Mybatis和Mysql依赖 <dependency> <groupId>org.mybati ...
- SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版)
SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版) ================================ ©Copyright 蕃薯耀 2 ...
- Springboot集成mybatis(mysql),mail,mongodb,cassandra,scheduler,redis,kafka,shiro,websocket
https://blog.csdn.net/a123demi/article/details/78234023 : Springboot集成mybatis(mysql),mail,mongodb,c ...
- springboot整合mybatis+pageHelper
springboot整合mybatis+pageHelper 〇.搭建sporingboot环境,已经整合mybatis环境,本篇主要是添加pageHelper工具 一.添加依赖 <!-- 分页 ...
- SpringBoot集成Mybatis配置动态数据源
很多人在项目里边都会用到多个数据源,下面记录一次SpringBoot集成Mybatis配置多数据源的过程. pom.xml <?xml version="1.0" encod ...
随机推荐
- thinkphp开发微信小程序后台流程
thinkphp开发微信小程序后台流程,简单分享一下微信开发流程 1,注册微信小程序账号 2,注册好后,登陆微信小程序,下载微信小程序开发工具 3,用thinkphp开发企业后台,前台数据用json返 ...
- 【JAVA】java编译错误:编码UTF8/GBK的不可映射字符
环境: win7 cmd窗口编译 javac xx.java时报错 错误显示:错误:编码GBK的不可映射字符 背景: 分析发现是中文字符所在行报错了 查阅相关资料发现,是因为编译器设置为了utf-8, ...
- axios 如何获取下载文件的进度条
exportFun(){ let _that = this const instance = this.axios.create({ onDownl ...
- shell条件判断命令test
- python类中方法__str__()和__repr__()简单粗暴总结
在交互式模式下,类中同时实现__str__()和__repr__()方法: 直接输入实例名称显示repr返回的类容: 用print打印实例名称显示str返回的内容: >>> clas ...
- html5 图片墙
代码实例: <!DOCTYPE html> <html> <head> <style> body,html{ padding:0;margin:0;wi ...
- Spring----事件(Application Event)
1.概述 1.1.Spring的事件 为Bean与Bean之间的消息通信提供了支持: 当一个Bean处理完一个任务后,希望另一个Bean知道并能做出相应的处理,这时我们需要 让另一个Bean ...
- 指针使用const修饰总结
1 double rates[5] = {1, 2, 3, 4, 5}; const double * pd = rates; 被pd指向的值不可改变,比如,不允许*pd = 20 但是pd的指向改变 ...
- 使用idea搭建Spring boot+jsp的简单web项目
大家好: 这是我的第一篇博客文章,简单介绍一下Spring boot + jsp 的搭建流程,希望给跟我一样新接触Spring boot的读者一点儿启发. 开发工具:jdk1.8 idea2017 ...
- mybatis源码分析之01环境搭建
直接使用maven搭建一个mybatis的运行环境 1. pom.xml <?xml version="1.0" encoding="UTF-8"?> ...