Reading catalogue
General mapper refers to the package of tk.mybatis. This is a general mapper, which means that the auto generated dao layer needs to inherit the mapper class provided by this framework. And the org.mybatis we used before is common at the beginning.
That is to say, in the beginning, we will use this to generate dao interface (without inheriting other classes), mapper file (with many methods to add, delete, modify and check) and entity class. We feel very troublesome, so we come out with a general mapper. We just need to inherit the mapper class. There is an automatic way to add, delete, modify and query, similar to jpa. The code is simple. Let's just talk about how to generate automatically.
There are many pitfalls, for example, spring boot 2.0 or above will cause problems, and 1.5 will do. Follow the tutorial. It's bound to succeed.
pom file
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion><groupId>com.example</groupId> <artifactId>tk-mybatis</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>tk-mybatis</name> <description>Demo project <span style="color: #0000ff;">for</span> Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.10.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!--mysql drive--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- mybatis Reverse generation tool --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> <scope>compile</scope> <optional><span style="color: #0000ff;">true</span></optional> </dependency> <!--mapper --> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>1.2.4</version> </dependency> <!--Database connection pool--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <!--generator Location of profile--> <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile> <verbose><span style="color: #0000ff;">true</span></verbose> <overwrite><span style="color: #0000ff;">true</span></overwrite> </configuration> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.36</version> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>3.4.0</version> </dependency> </dependencies> </plugin> </plugins> </build>
</project>
generatorConfig.properties
# Generator generator.targetProject=src/main/java generator.mappers=com.example.demo.baseMapper.IBaseMapper generator.javaModel-targetPackage=com.example.demo.domain generator.sqlMap-targetPackage=com.example.demo.dao generator.javaClient-targetPackage=com.example.demo.dao
#mysql
driverClass=com.mysql.jdbc.Driver
connectionURL=jdbc:mysql://localhost:3306/video?serverTimezone=UTC
userId=root
password=root
generatorConfig.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <! -- root node -- > <generatorConfiguration> <! -- specify external attribute element -- > <properties resource="generatorConfig.properties"/> <! -- you must specify the environment in which to generate a set of objects -- > <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat"> <! -- separator -- > <property name="autoDelimitKeywords" value="true"/> <property name="beginningDelimiter" value="`"/> <property name="endingDelimiter" value="`"/><!-- currency mapper Plug-in unit --> <plugin type="tk.mybatis.mapper.generator.MapperPlugin"> <!-- Here MyMapper Note that it must not be scanned--> <property name="mappers" value="${generator.mappers}"/> </plugin> <!-- Comment rule--> <commentGenerator> <property name="suppressAllComments" value="false"/> <property name="suppressDate" value="true"/> </commentGenerator> <jdbcConnection driverClass="${driverClass}"<span style="color: #000000;"> connectionURL</span>="${connectionURL}"<span style="color: #000000;"> userId</span>="${userId}"<span style="color: #000000;"> password</span>="${password}"> </jdbcConnection> <javaModelGenerator targetPackage="${generator.javaModel-targetPackage}" targetProject="${generator.targetProject}"/> <sqlMapGenerator targetPackage="${generator.sqlMap-targetPackage}" targetProject="${generator.targetProject}"/> <javaClientGenerator targetPackage="${generator.javaClient-targetPackage}" targetProject="${generator.targetProject}"<span style="color: #000000;"> type</span>="XMLMAPPER"/> <table tableName="bgm"> <!--mysql To configure--> <generatedKey column="id" sqlStatement="Mysql" /> <!--oracle To configure--> <!--<generatedKey column="id" sqlStatement="select SEQ_{1}.nextval from dual" identity="false" type="pre"/>--> </table> </context>
</generatorConfiguration>
IBaseMapper (let the generated mapper interface inherit this class)
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
/**
- Created by
- on 2018/11/1 22:04.
*/
public interface IBaseMapper<T> extends Mapper<T>, MySqlMapper<T> {
}
It's ok.