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 ...
随机推荐
- XMPP即时通讯协议使用(一)——Openfire安装
Openfire服务器安装 下载地址:https://www.igniterealtime.org/downloads/index.jsp,根据你的操作系统,选择对应的下载版本.本文选择的是openf ...
- bounds与frame的区别及setBounds的使用
转自http://www.cocoachina.com/ios/20140925/9755.html 在iOS开发中经常遇到两个词Frame和bounds,本文主要阐述Frame和bound的区别,尤 ...
- 2019-1-5-Windows-的-Pen-协议
title author date CreateTime categories Windows 的 Pen 协议 lindexi 2019-01-05 11:14:49 +0800 2019-01-0 ...
- python序列的深拷贝和浅拷贝
python中的不可变类型 列举:数值,字符串.元组.字节串 数值及字符串“可变”'的假象 num = 123 mystr = 'abc' print(id(num), num) print(id(m ...
- 牛客小白月赛16 D 小阳买水果 (思维题)
链接:https://ac.nowcoder.com/acm/contest/949/D来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言52428 ...
- Jmeter —— Test Fragment (测试片段)
Test Fragment(测试片段) 1. 概念 JMeter中的Test Fragent:是控制器上一种特殊的线程组:它在测试树上与线程组处于同一个层级.但是使用的时候需要和Include ...
- C#基础提升系列——C#委托
C# 委托 委托是类型安全的类,它定义了返回类型和参数的类型,委托类可以包含一个或多个方法的引用.可以使用lambda表达式实现参数是委托类型的方法. 委托 当需要把一个方法作为参数传递给另一个方法时 ...
- 命令行启动appium服务
Android终端 appium --avd test -a 127.0.0.1 -p 4723 --language "zh_CN" --locale "CN" ...
- window 安装VisualSvn Server
VisualSvn Server 1 .VisualSvn Server 介绍和下载 VisualSvn Server是免费的,而VisualSvn是收费的.VisualSvn是Svn的客户端,和V ...
- [LightOJ1070]Algebraic Problem
题目:Algebraic Problem 链接:https://vjudge.net/problem/LightOJ-1070 分析: 1)$ a^n+b^n = ( a^{n-1}+b^{n-1} ...