1、项目简介
官方项目地址:https://github.com/tqlab/mybatis-plugin
本文讲解的是Mybatis代码生成插件之tqlab-mybatis-plugin。该项目是基于mybatis-generator封装的一个maven插件,通过sql模板自动生成mybatis Mapper和DO文件,减少CRUD代码的开发工作量,目前已在多个大型项目中使用,支持单库、多库以及分库分表规则。
2、Maven依赖
<dependency>
<groupId>com.tqlab.plugin</groupId>
<artifactId>tqlab-mybatis-plugin</artifactId>
<version>1.0.10</version>
</dependency>
3、配置示例
3.1、单库配置
<build>
<plugins>
<plugin>
<groupId>com.tqlab.plugin</groupId>
<artifactId>tqlab-mybatis-plugin</artifactId>
<version>1.0.10</version>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<phase>deploy</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<outputDirectory>${project.basedir}</outputDirectory>
<!-- db config -->
<jdbcURL>jdbc:mysql://localhost/testdb?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull</jdbcURL>
<jdbcUserId>user</jdbcUserId>
<jdbcPassword>password</jdbcPassword>
<database>testdb</database>
<dbName>mysql</dbName>
<!-- db config end -->
<!-- <sqlScript>${project.basedir}/src/main/resources/mysql.sql</sqlScript> -->
<packages>info.lijun.dal</packages>
<sqlTemplatePath>${project.basedir}/src/main/resources/sqltemplate/</sqlTemplatePath>
<overwrite>true</overwrite>
<useCache>false</useCache>
<generateJdbcConfig>false</generateJdbcConfig>
<generateSpringConfig>true</generateSpringConfig>
</configuration>
</plugin>
</plugins>
</build>
3.2、多库配置
<build>
<plugins>
<plugin>
<groupId>com.tqlab.plugin</groupId>
<artifactId>tqlab-mybatis-plugin</artifactId>
<version>1.0.10</version>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<phase>deploy</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<outputDirectory>${project.basedir}</outputDirectory>
<databaseConfig>
<config>
<!-- db config -->
<jdbcURL>jdbc:mysql://localhost/testdb1?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull</jdbcURL>
<jdbcUserId>user</jdbcUserId>
<jdbcPassword>password</jdbcPassword>
<database>testdb1</database>
<dbName>mysql</dbName>
<!-- db config end -->
<!-- <sqlScript>${project.basedir}/src/main/resources/mysql.sql</sqlScript> -->
<packages>info.lijun.dal</packages>
<sqlTemplatePath>${project.basedir}/src/main/resources/sqltemplate/</sqlTemplatePath>
<generateJdbcConfig>false</generateJdbcConfig>
<generateSpringConfig>true</generateSpringConfig>
<overwrite>true</overwrite>
</config>
<config>
<!-- db config -->
<jdbcURL>jdbc:mysql://localhost/testdb2?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull</jdbcURL>
<jdbcUserId>user</jdbcUserId>
<jdbcPassword>password</jdbcPassword>
<database>testdb2</database>
<dbName>mysql</dbName>
<!-- db config end -->
<!-- <sqlScript>${project.basedir}/src/main/resources/mysql.sql</sqlScript> -->
<packages>com.taobao.bns.dal</packages>
<sqlTemplatePath>${project.basedir}/src/main/resources/sqltemplate/</sqlTemplatePath>
<generateJdbcConfig>false</generateJdbcConfig>
<generateSpringConfig>true</generateSpringConfig>
<useCache>false</useCache>
</config>
</databaseConfig>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</build>
4、参数说明
属性 | 描述 | 默认值 | 必填 |
---|---|---|---|
outputDirectory | 输出目录 | ${project.build.directory}/generated-sources/mybatis-generator | |
sqlScript | 初始SQL脚本文件路径 | false | |
jdbcURL | Database url | true | |
jdbcUserId | Database user | false | |
jdbcPassword | Database password | false | |
tableNames | 需要生成Mapper的表名,逗号隔开 | 默认为全表 | false |
tablePrefix | 表名前缀 | false | |
doSuffix | DO文件后缀 | false | |
doRootClass | DO文件基类 | false | |
database | Database | true | |
dbName | 数据库名称,支持mysql, hsqldb | true | |
packages | 包名,例如 info.lijun.dal. | true | |
overwrite | 是否覆盖 | false | false |
sqlTemplatePath | Sql模板文件路径 | true | |
useCache | 是否使用缓存 | false | false |
providerEnable | 是否使用Provider | true | false |
selectKeyEnable | 是否生成SelectKey注解 | true | false |
generateSpringConfig | 是否生成Spring配置文件 | false | false |
generateOsgiConfig | 是否生成Spring OSGI配置文件 | false | false |
properties | 额外属性 | false |
5、SQL模板示例
<?xml version="1.0" encoding="UTF-8"?>
<table xmlns="http://schema.tqlab.com/mybatis"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://schema.tqlab.com/mybatis http://schema.tqlab.com/mybatis/tqlab-mybatis-plugin.xsd"
name="star">
<operation id="deleteById">
<sql>
<![CDATA[
delete from star where id=#{id,jdbcType=INTEGER};
]]>
</sql>
</operation>
<operation id="count" resultType="java.lang.Integer">
<sql>
<![CDATA[
select count(*) from star;
]]>
</sql>
</operation>
<operation id="sum" resultType="java.lang.Integer">
<sql>
<![CDATA[
select sum(id) from star;
]]>
</sql>
</operation>
<operation id="selectAll" many="true">
<sql>
<![CDATA[
select * from star;
]]>
</sql>
</operation>
<operation id="selectById" many="false">
<sql>
<![CDATA[
select * from star where id=#{id,jdbcType=INTEGER};
]]>
</sql>
</operation>
<operation id="selectWithPagination">
<comment>
demo
</comment>
<sql>
<![CDATA[
select limit #{start,jdbcType=INTEGER} #{size,jdbcType=INTEGER} * from star;
]]>
</sql>
</operation>
<operation id="selectComplex1" many="true">
<result objectName="StarMovies">
<property cloumn="id" javaProperty="id" javaType="java.lang.Integer" />
<property cloumn="firstname" javaProperty="firstname"
javaType="java.lang.String" />
<property cloumn="lastname" javaProperty="lastname"
javaType="java.lang.String" />
<property cloumn="movieid" javaProperty="movieid" javaType="java.lang.Integer" />
<property cloumn="title" javaProperty="title" javaType="java.lang.String" />
</result>
<sql>
<![CDATA[
select a.*, b.* from star a, movies b where a.id = b.starid
]]>
</sql>
</operation>
<operation id="selectComplex2" many="true">
<result objectName="StarMovies2">
<property cloumn="star_id" javaProperty="id" javaType="java.lang.Integer" />
<property cloumn="name" javaProperty="firstname" javaType="java.lang.String" />
<property cloumn="lastname" javaProperty="lastname"
javaType="java.lang.String" />
<property cloumn="movieid" javaProperty="movieid" javaType="java.lang.Integer" />
<property cloumn="title" javaProperty="title" javaType="java.lang.String" />
</result>
<sql>
<![CDATA[
select a.id as star_id, a.firstname as name, a.lastname,
b.movieid, b.title from star a, movies b
where a.id = b.starid
]]>
</sql>
</operation>
</table>