SpringBoot - 整合Mybatis
导入依赖
pom.xml
<!--开启SpringBoot jdbc--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!--mysql 依赖--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.20</version> </dependency> <!--druid 数据源--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.8</version> </dependency> <!--mybatis: 整合依赖包--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency>
配置文件
application.yaml
spring:
datasource:
#数据源基本配置
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/learn?serverTimezone=UTC
type: com.alibaba.druid.pool.DruidDataSource
#数据源其他配置
druid:
#配置初始化大小、最小、最大线程数
initialSize: 5
minIdle: 5
#CPU核数+1,也可以大些但不要超过20,数据库加锁时连接过多性能下降
maxActive: 20
#最大等待时间,内网:800,外网:1200(三次握手1s)
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
#配置一个连接在池中最大空间时间,单位是毫秒
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1
testWhileIdle: true
#设置从连接池获取连接时是否检查连接有效性,true检查,false不检查
testOnBorrow: true
#设置从连接池归还连接时是否检查连接有效性,true检查,false不检查
testOnReturn: true
#可以支持PSCache(提升写入、查询效率)
poolPreparedStatements: true
#配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
filters: stat,wall,log4j
#保持长连接
keepAlive: true
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
#mybatis 配置
#mybatis
mybatis:
#标注mybatis配置文件的位置
config-location: classpath:mybatis-config.xml
#配置别名
type-aliases-package: com.example.springboot_security_learn.bean
#配置 xml 文件映射
mapper-locations: classpath:mapper/*.xml
configuration:
# 开启驼峰uName自动映射到u_name
map-underscore-to-camel-case: true
接口与mapper.xml
com.levi.springboot_learn.dao.BankMapper
classpath:mapper/BankMapper.xml