参考资料:
Tk Mapper :
Mybatis Generate 官网: http://mybatis.org/generator/index.html
说明:
TKMapper 可以帮助我们降低Mapper使用复杂度。避免简单的增删改还需要编写SQL语句的情况,使用者知识需要调用对应的增删改查接口即可。对于使用者而言只是需要编写复杂数据处理相关的sql语句。
TK Mapper提供了一个构件插件支持,可以帮助我们快速的生成实体类、映射接口、映射XML 。在开发的时候只是需要根据需要添加对应代码即可。
1.在pom.xml中配置插件
<properties>
...
<mysql.version>8.0.20</mysql.version>
<targetJavaProject>${basedir}/src/main/java</targetJavaProject>
<targetResourcesProject>${basedir}/src/main/resources</targetResourcesProject>
<targetModelPackage>com.xuetang9.framework.ssmtkmapper.entity</targetModelPackage>
<targetMapperPackage>com.xuetang9.framework.ssmtkmapper.mapper</targetMapperPackage>
<targetXMLPackage>com.xuetang9.framework.ssmtkmapper.mapper</targetXMLPackage>
</properties>
...
<plugins>
<plugin>
<!--如果版本不兼容,请使用1.3.5-->
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<configurationFile>src/main/resources/generator-config.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<executions>
<execution>
<id>generator</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.5.3</version>
</dependency>
</dependencies>
</plugin>
</plugins>
2.配置生成使用的配置
<?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">
<generatorConfiguration>
<properties resource="database.properties"/>
<context id="mysql"
targetRuntime="tk.mybatis.mapper.generator.TkMyBatis3SimpleImpl"
defaultModelType="flat">
<!--分隔符设置-->
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<!--设置生成的Mapper继承自哪个接口-->
<property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>
</plugin>
<!--配置数据库的连接-->
<jdbcConnection driverClass="${jdbc.driver}"
connectionURL="${jdbc.url}"
userId="${jdbc.username}"
password="${jdbc.password}">
<!--不生成其他数据库的表-->
<property name="nullCatalogMeansCurrent" value="true"/>
</jdbcConnection>
<!--配置实体类生成的位置-->
<javaModelGenerator targetPackage="${targetModelPackage}" targetProject="${targetJavaProject}"/>
<!--配置sql映射文件的位置-->
<sqlMapGenerator targetPackage="${targetXMLPackage}" targetProject="${targetResourcesProject}"/>
<!--配置Mapper接口的位置-->
<javaClientGenerator targetPackage="${targetMapperPackage}" targetProject="${targetJavaProject}" type="XMLMAPPER"/>
<table tableName="%" mapperName="{0}Mapper">
<generatedKey column="id" sqlStatement="mysql" identity="false"/>
</table>
</context>
</generatorConfiguration>
网友评论