欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring boot配置Mybatis

程序员文章站 2022-04-19 19:01:58
...

先在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>5.1.41</version>
</dependency>
<!--<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>druid</artifactId>
	<version>1.0.29</version>
</dependency>-->

<plugin mybatis插件>
见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等等。