Mybatis代码生成插件

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
jdbcURLDatabase urltrue
jdbcUserIdDatabase userfalse
jdbcPasswordDatabase passwordfalse
tableNames需要生成Mapper的表名,逗号隔开默认为全表false
tablePrefix表名前缀false
doSuffixDO文件后缀false
doRootClassDO文件基类false
databaseDatabasetrue
dbName数据库名称,支持mysql, hsqldbtrue
packages包名,例如 info.lijun.dal.true
overwrite是否覆盖falsefalse
sqlTemplatePathSql模板文件路径true
useCache是否使用缓存falsefalse
providerEnable是否使用Providertruefalse
selectKeyEnable是否生成SelectKey注解truefalse
generateSpringConfig是否生成Spring配置文件falsefalse
generateOsgiConfig是否生成Spring OSGI配置文件falsefalse
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>

更多示例:https://github.com/tqlab/mybatis-plugin/blob/master/demo/src/main/resources/sqltemplate/hsqldb/table.star.xml

发表评论