1. 程式人生 > >spring boot配置Mybatis

spring boot配置Mybatis

先在pom.xml中加上mybatis的依賴:

<dependency>
	<groupId>org.mybatis.spring.boot</groupId>
	<artifactId>mybatis-spring-boot-starter</artifactId>
	<version>1.3.0</version>
</dependency>
<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
	<version>8.0.11</version>
</dependency>
<!--<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>druid</artifactId>
	<version>1.0.29</version>
</dependency>-->

還有<plugin mybatis外掛>也需要加到pom.xml裡的<plugins>下:
見https://blog.csdn.net/tonghuawanli/article/details/76999282

然後在application.properties中,加上資料庫的配置:

# MySQL配置
spring.datasource.url=jdbc:mysql://***:3306/**?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=
spring.datasource.password=
# spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.maxWait=60000
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.filters=stat,log4j
spring.datasource.connectionProperties="druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000"

mybatis.mapper-locations=classpath:/mappers/*.xml

在resources下新建generatorConfig.xml,內容見:https://blog.csdn.net/tonghuawanli/article/details/76999282,
在generatorConfig.xml中配置生成的檔案位置之類的。

之後通過Mybatis外掛就可以生成對應的類、Mapper等等。