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注解,效果一样!
以上集成完成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();
原文地址:https://www.cnblogs.com/kibana/p/10344496.html
时间: 2024-10-18 06:26:02