Springboot整合tk-mybatis
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.29</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<configurationFile>
${basedir}/src/main/resources/generator/generatorConfig.xml
</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.29</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.0.0</version>
</dependency>
</dependencies>
</plugin>
《可不加》
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
《可不加》
/*启动类扫描mybatis的通用接口*/
import tk.mybatis.spring.annotation.MapperScan;
@MapperScan("com.itmuch.mapper")
配置mysql 版本<5.X
spring:
# mysql
datasource:
url: jdbc:mysql://127.0.0.1:3306/数据库?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
driver-class-name: com.mysql.jdbc.Driver
hikari:
username: root
password: root
config.properties内容
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/user_center?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
jdbc.user=root
jdbc.password=root
#模块名称
modelName=user
#表名
tableName=user
generatorConfig.xml内容
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<properties resource="generator/config.properties"/>
<context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>
<property name="caseSensitive" value="true"/>
</plugin>
<jdbcConnection driverClass="${jdbc.driverClass}"
connectionURL="${jdbc.url}"
userId="${jdbc.user}"
password="${jdbc.password}">
</jdbcConnection>
<!--实体-->
<javaModelGenerator targetPackage="com.itmuch.entity.${modelName}"
targetProject="src/main/java"/>
<!--mapper-->
<sqlMapGenerator targetPackage="com.itmuch.mapper.${modelName}"
targetProject="src/main/resources"/>
<!--mapper接口-->
<javaClientGenerator targetPackage="com.itmuch.mapper.${modelName}"
targetProject="src/main/java"
type="XMLMAPPER"/>
<table tableName="${tableName}">
<generatedKey column="id" sqlStatement="JDBC"/>
</table>
</context>
</generatorConfiguration>
能力是有限的,努力是无限的。