mybatis全面解析
地址: http://www.mybatis.org/mybatis-3/zh/index.html
springboot 整合mybatis配置
添加依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<!--数据库连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!--分页-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.10</version>
</dependency>
配置
spring:
application:
name: pay-server
datasource:
url: jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
username: root
password: 123
driver-class-name: com.mysql.cj.jdbc.Driver
druid:
initial-size: 5 # 初始化大小
min-idle: 5 # 最小
max-active: 100 # 最大
max-wait: 60000 # 连接超时时间
time-between-eviction-runs-millis: 60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
min-evictable-idle-time-millis: 300000 # 指定一个空闲连接最少空闲多久后可被清除,单位是毫秒
validationQuery: select 'x'
test-while-idle: true # 当连接空闲时,是否执行连接测试
test-on-borrow: false # 当从连接池借用连接时,是否测试该连接
test-on-return: false # 在连接归还到连接池时是否测试该连接
filters: config,wall,stat,slf4j # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
maxOpenPreparedStatements: 20
connectionProperties: druid.stat.slowSqlMillis=200;druid.stat.logSlowSql=true
web-stat-filter:
enabled: true
url-pattern: /*
exclusions: /druid/*,*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico
session-stat-enable: true
session-stat-max-count: 10
stat-view-servlet:
enabled: true
url-pattern: /druid/*
reset-enable: true
login-username: admin
login-password: admin
可以访问地址: 项目url/druid/ 查看sql执行情况
自动生成mapper
用mybatis自动生成工具生成mapper后,在mapper上添加 @Mapper 注解
@Mapper
public interface AddressDOMapper {
int deleteByPrimaryKey(Integer id);
int insert(AddressDO record);
int insertSelective(AddressDO record);
}
druid 连接池详情
https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98
配置多数据源参考
31如何在Spring Boot中集成Druid连接池和监控?
分页查询
PageHelper 可以简单地实现分页查询
PageHelper.startPage(pageNum, pageSize); // 该语句下一句必须是mybatis查询语句,否则会出现问题
List<GoodsDO> list = goodsMapper.selectByCategoryId(categoryId);
PageInfo<GoodsDO> goodsDOPageInfo = new PageInfo<>(list); // 分页数据
网友评论