操作步骤
下面是Spring boot集成MyBatis和Druid的步骤和配置
1、引入jar
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
2、配置application.properties
# 数据库访问配置
# 主数据源,默认的
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://ip:3306/database?autoReconnect=true
spring.datasource.username=root
spring.datasource.password=xxx
# 下面为连接池Druid的补充设置,应用到上面所有数据源中
# 初始化大小,最小,最大
spring.datasource.initialSize=5
spring.datasource.minIdle=10
spring.datasource.maxActive=50
# 配置获取连接等待超时的时间
spring.datasource.maxWait=60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.timeBetweenEvictionRunsMillis=9000
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
# 打开PSCache,并且指定每个连接上PSCache的大小
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
spring.datasource.filters=stat,wall,log4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多个DruidDataSource的监控数据
# spring.datasource.useGlobalDataSourceStat=true
# 数据库连接的自动回收机制
# 如果连接泄露,是否需要回收泄露的连接
spring.datasource.removeAbandoned=true
# 连接回收的超时时间
spring.datasource.removeAbandonedTimeout=1800
# 如果回收了泄露的连接,是否要打印一条log
spring.datasource.logAbandoned=true
#mybatis配置
#mapper文件地址
mybatis.mapper-locations=classpath:mappers/*Mapper.xml
#简化mapper.xml中的类型使用,不需要写全路径名
mybatis.type-aliases-package=com.huawei.hms.mail
#开启MyBatis Sql日志
logging.level.com.huawei.hms.mail.test.dao.JamesMailVOMapper=debug
3、配置mapper接口的扫描
有两种方式来扫描,一是通过xml配置文件的方式,二是通过注解MapperScan方式(加到启动类上),推荐第二种
方式一,配置到applicationContext.xml中
<!-- SqlSessionFactory管理 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource" />
<!-- 加载mybatis的全局配置文件 -->
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
<property name="mapperLocations">
<list>
<value>classpath*:dm01_01/**/mapper/*.xml</value>
<value>classpath*:dm01_03/**/mapper/*.xml</value>
<value>classpath*:dm01_04/**/mapper/*.xml</value>
<value>classpath*:dm01_05/**/mapper/*.xml</value>
<value>classpath*:dm01_06/**/mapper/*.xml</value>
<value>classpath*:cn/com/xxx/dao/*.xml</value>
</list>
</property>
</bean>
<!-- 批量管理代理对象 接口类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描包 -->
<property name="basePackage" value="dm01_01.**.dao;dm01_03.**.dao;dm01_04.**.dao;dm01_05.**.dao;dm01_06.**.dao;cn.com.xxx.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource" />
<!-- 加载mybatis的全局配置文件 -->
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
<property name="mapperLocations">
<list>
<value>classpath*:dm01_01/**/mapper/*.xml</value>
<value>classpath*:dm01_03/**/mapper/*.xml</value>
<value>classpath*:dm01_04/**/mapper/*.xml</value>
<value>classpath*:dm01_05/**/mapper/*.xml</value>
<value>classpath*:dm01_06/**/mapper/*.xml</value>
<value>classpath*:cn/com/xxx/dao/*.xml</value>
</list>
</property>
</bean>
<!-- 批量管理代理对象 接口类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描包 -->
<property name="basePackage" value="dm01_01.**.dao;dm01_03.**.dao;dm01_04.**.dao;dm01_05.**.dao;dm01_06.**.dao;cn.com.xxx.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
方式二 直接在启动类添加注解,指名扫描路径,支持通配符
@MapperScan("com.z.**.dao")
配置说明
集成到Spring Boot后,对于Mybatis和Druid的配置均可配置在application.properties中配置,无需再添加额外的配置文件
参考
Spring Boot集成Mybatis
https://www.cnblogs.com/wx_blog/p/10371840.html
https://www.iteye.com/blog/412887952-qq-com-2292362
https://gitee.com/lcg0124/bootdo/blob/master/bootdo/src/main/resources/application-dev.yml
http://www.ityouknow.com/springboot/2016/11/06/spring-boot-mybatis.html
https://blog.csdn.net/u012702547/article/details/88643598
https://blog.csdn.net/qq_32719003/article/details/72123917
官方文档
http://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/