SpringBoot集成Mybatis和Druid连接池
导入Maven依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
解决不解析XML问题
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
SpringBoot配置文件设置别名,扫描Mapper.xml文件
mybatis.type-aliases-package=com.java.springboot.bean
mybatis.mapper-locations=com/java/dao/*.xml
扫描dao下的mapper接口生成代理对象
@SpringBootApplication
@MapperScan("com.java.springboot.dao")
public class MySpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringbootApplication.class, args);
}
}
配置Druid连接池
#关闭thymeleaf缓存
spring:
thymeleaf:
cache: false
#配置数据源
datasource:
type: com.alibaba.druid.pool.DruidDataSource
druid:
username: root
password: root
initial-size: 5
# mysql8.0 需指定时区
url: jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
# mysql8.0 驱动地址与5.0不同
driver-class-name: com.mysql.cj.jdbc.Driver
filters: stat,wall,log4j
use-global-data-source-stat: true
测试
测试通过,以上就是SpringBoot整合Mybatis与Druid的内容。