Mybatis为我们提供了基于Spring Boot的starter,所以在Spring Boot工程中使用Mybatis很简单,只需要简单的几步就可以搞定。
一、首先我们需要引入Mybatis的依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
二、声明Mapper接口的位置
@MapperScan("com.x.y.x.mapper")
三、配置SQL文件位置
mybatis.mapper-locations=classpath:mapper/*.xml
四、进行一些其它的配置
#是否开启二级缓存,默认是关闭的
mybatis.configuration.cache-enabled=true
#配置SQL文件中使用的model类型路径,为其生成别名
mybatis.type-aliases-package=com.x.y.x.model,com.x.y.x.entity
当然还有一些其它的配置项,如有需要可以在网上自行查找,或者查阅mybatis提供的autoconfiguration的源码
通过上面几步配置,然后编写自己的mapper接口和xml文件,就可以在Spring Boot工程中使用Mybatis了。
附上数据源的配置:
#--------------------数据源配置--------------------
spring.datasource.url=jdbc:mysql://192.168.2.238:3306/cfmp3?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=admin_123
# hikari连接池配置
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
# 这个maxPoolSize的配置并没有生效,我看spring boot的源码,按理应该是这样配,但是实际并没有生效,它根本就没有加载到这个配置,如果有清楚的大神,还望告知
spring.datasource.hikari.maxPoolSize=15