本文主要介紹如何對mybatis generator plugin 插件擴展,實現自定義實體類的註釋。實現方式可通過修改源代碼,或者自己單獨開一個項目,通過繼承generator的類,然後項目中引入。本文主要介紹後者。
項目源碼傳送門
一、新建maven項目
1、添加項目依賴
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.4.0</version>
</dependency>
1、新建類,繼承 DefaultCommentGenerator,重寫對應方法
public class CustomComment extends DefaultCommentGenerator {
/**
* 類註釋
* mysql 必須配置useInformationSchema,插件才能獲取到表的註釋
* <property name="useInformationSchema" value="true" />
* oracle 須配置remarksReporting,插件才能獲取到表的註釋
* <property name="remarksReporting" value="true"></property>
*/
@Override
public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
String remarks = introspectedTable.getRemarks();
if ( StringUtility.stringHasValue(remarks)){
topLevelClass.addJavaDocLine("/** ");
topLevelClass.addJavaDocLine(" * @Description: " + remarks);
topLevelClass.addJavaDocLine(" * @WARNING This class was generated by MyBatis Generator, do not modify.");
topLevelClass.addJavaDocLine(" */");
}
}
/**
* 屬性註釋
*/
@Override
public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
String remarks = introspectedColumn.getRemarks();
if ( StringUtility.stringHasValue(remarks)){
field.addJavaDocLine("/** "+ remarks+ " */");
}
}
}
以上插件就完成了,在使用前,通過maven package,打包到自己本地倉庫,然後接下來看如何使用。
首先在pom文件中引入剛剛創建的插件,注意是在plugin中引用
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
<configurationFile>src/main/resources/gen/generatorConfiguration.xml</configurationFile>
</configuration>
<dependencies>
<dependency>
<artifactId>mybatis-generator-plugin-ext</artifactId>
<groupId>mybatis.generator.plugin</groupId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</plugin>
在要使用的項目中,配置generator.xml,增加如下配置,在commnetGenerator節點中type屬性指向剛剛自定義插件中創建的類的全路徑
<commentGenerator type="mybatis.generator.plugin.plugins.CustomComment">
<!-- 是否去除自動生成的註釋 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
同時還需要格外注意,如果想獲取表的註釋(類的註釋)則需要對 jdbcConnection節點添加如下屬性,否則將會獲取不到
<jdbcConnection driverClass="${jdbc.driver}" connectionURL="${jdbc.url}" userId="${jdbc.username}" password="${jdbc.password}">
<!-- 設置 useInformationSchema 屬性爲 true 針對MySQL獲取表註釋-->
<property name="useInformationSchema" value="true" />
<!-- 針對oracle 獲取表註解信息-->
<property name="remarksReporting" value="true"></property>
</jdbcConnection>
以上便介紹完擴展,與使用的方法。
另外本人還同時對MySQL與oracle擴展了一些常用的方法,如下,源碼已經放到碼雲傳送門,歡迎交流,fork,start,pr
int deleteByPrimaryKey(String id);
int insert(Person record);
Person selectByPrimaryKey(String id);
List<Person> selectAll();
int updateByPrimaryKey(Person record);
Person selectOne(Person record);
List<Person> selectList(Person record);
int insertBatch(List<Person> list);
int deleteBatchIds(Collection<Object> record);
List<Person> selectBatchIds(Collection<Object> record);