使用过Mybatis3的朋友,相信应该听说过MBG插件,配合maven集成到eclipse,一条命令生成数据表对应的model,dao,mapper,mapper.xml文件,方便自不必说,但原生插件中生成的model中并没有显示数据表对应的中文注释,虽说自动生成了实体,但代码可读性大大降低。
结合网上查阅资料,形成此文,以便大家能生成带有数据表中文注释的model。
1、git clone https://github.com/backkoms/mybatis-generator-comments.git,编译打包,install到本地或delopy私服库中均可
2、使用MBG插件的项目中pom.xml配置
代码语言:javascript复制<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>utf8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>
${basedir}/src/test/resources/conf/generatorConfig.xml
</configurationFile>
<overwrite>true</overwrite>
</configuration>
<dependencies>
<dependency>
<groupId>com.zhishi.mybatis</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.0.1</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
3、关键配置文件:generatorConfig.xml,配置如下
代码语言:javascript复制<?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>
<!-- jdbc路径 -->
<classPathEntry
location="C:devReporsitorymysqlmysql-connector-java5.1.38mysql-connector-java-5.1.38.jar" />
<context id="context" targetRuntime="MyBatis3">
<plugin type="org.mybatis.generator.plugins.RenameExampleClassPlugin">
<property name="searchString" value="Example$" />
<property name="replaceString" value="Criteria" />
</plugin>
<plugin type="org.mybatis.generator.plugins.CachePlugin">
<property name="cache_eviction" value="LRU" />
<property name="cache_flushInterval" value="60000" />
<property name="cache_readOnly" value="true" />
<property name="cache_size" value="1560" />
</plugin>
<plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"></plugin>
<plugin type="org.mybatis.generator.plugins.SerializablePlugin">
<property name="suppressJavaInterface" value="false" />
</plugin>
<!-- 数据库表注释生成-->
<commentGenerator
type="org.mybatis.generator.internal.CustomeCommentGenerator">
<property name="javaFileEncoding" value="UTF-8" />
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="false" />
<property name="suppressDate" value="false" />
</commentGenerator>
<!-- 请填写connectionURL、userId、password -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://192.168.1.118:3306/dbname" userId="root"
password="root" />
<!-- 生成持久化对象 -->
<javaModelGenerator targetPackage="com.test.domain"
targetProject="src/test/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成mapper.xml文件 -->
<sqlMapGenerator targetPackage="mapper/mybatis"
targetProject="src/test/resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成Mapper接口 -->
<javaClientGenerator targetPackage="com.test.dal.mapper.credit"
targetProject="src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 需要生成的数据库表 -->
<table tableName="tb_trx_bank" domainObjectName="TrxBank">
<generatedKey column="tid" sqlStatement=" select replace(uuid(),'-','')" /><!-- 主键为uuid的情况,自动生成-->
</table>
</context>
</generatorConfiguration>
4、eclipse中Run as --> maven build .... -->Goals中输入命令行:mybatis-generator:generate -e
5、查看生成的model文件
代码语言:javascript复制public class TrxBank {
/**
* 主键
* 表 : tb_trx_bank
* 对应字段 : tid
*/
private String tid;
/**
* 交易类型
* 表 : tb_trx_bank
* 对应字段 : type
*/
private String type;
......