关于 mybatis-generator自定义注释生成 使用DefaultCommentGenerator重写来完成
項目里新建表時model,mapper以及mapper.xml基本都是用Mybatis Generator(以下簡稱為MBG)自動生成的,但是MBG自動生成的model的注釋實在有點非人類,至少中國人是完全接受不了的,在配置中禁用掉注釋吧,倒是簡單了,可是生成的model類光禿禿的,啥都沒有,字段方法沒有注釋,使用很不方便,別人看也不知道這個字段是啥含義,到最后還是要自己添加,一張表多點幾十個字段,特么添加累死了,不能這么干,得想法子,百度了一下網上,基本有兩種方法,第一種,就是直接修改MGB的源代碼,第二種就是自己寫一個類實現CommentGenerator接口,先說自己寫一個新類實現CommentGenerator接口的方法來使自動生成的model類含有中文注釋吧.
一:首先你得有一個maven項目,這個我就不多踢
二:通過mybatis generator實現自動生產model,mapper以及mapper.xml,具體參考 Intellij IDEA 14中使用MyBatis-generator 自動生成MyBatis代碼?
三:通過查看你會發現,注解類是在mybatis-generator-core jar包中 ?org.mybatis.generator.internal包下的DefaultCommentGenerator類。那么這里有兩種方法,一種是重寫DefaultCommentGenerator類,另外一種中修改jar包中的源碼,但很明顯第一種方法比較簡單,這里也只介紹第一種方法的寫法。
3.1在源代碼中新建一個類MyCommentGenerator,實現CommentGenerator接口,類的代碼如下:
package org.mybatis.generator;/*** Created by 草帽boy on 2017/2/16.* mybatis generator 自定義comment生成器.* 基于MBG 1.3.2.* @author ZhangAY 2016-02-19*/ import java.text.SimpleDateFormat; import java.util.Date; import java.util.Properties;import org.mybatis.generator.api.CommentGenerator; import org.mybatis.generator.api.IntrospectedColumn; import org.mybatis.generator.api.IntrospectedTable; import org.mybatis.generator.api.dom.java.*; import org.mybatis.generator.api.dom.xml.XmlElement; import org.mybatis.generator.config.MergeConstants; import org.mybatis.generator.config.PropertyRegistry; import org.mybatis.generator.internal.util.StringUtility;public class MyCommentGenerator implements CommentGenerator {private Properties properties;private Properties systemPro;private boolean suppressDate;private boolean suppressAllComments;private String currentDateStr;public MyCommentGenerator() {super();properties = new Properties();systemPro = System.getProperties();suppressDate = false;suppressAllComments = false;currentDateStr = (new SimpleDateFormat("yyyy-MM-dd")).format(new Date());}public void addJavaFileComment(CompilationUnit compilationUnit) {// add no file level comments by defaultreturn;}/*** Adds a suitable comment to warn users that the element was generated, and* when it was generated.*/public void addComment(XmlElement xmlElement) {return;}public void addRootComment(XmlElement rootElement) {// add no document level comments by defaultreturn;}public void addConfigurationProperties(Properties properties) {this.properties.putAll(properties);suppressDate = StringUtility.isTrue(properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_DATE));suppressAllComments = StringUtility.isTrue(properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_ALL_COMMENTS));}/*** This method adds the custom javadoc tag for. You may do nothing if you do* not wish to include the Javadoc tag - however, if you do not include the* Javadoc tag then the Java merge capability of the eclipse plugin will* break.** @param javaElement the java element*/protected void addJavadocTag(JavaElement javaElement, boolean markAsDoNotDelete) {javaElement.addJavaDocLine(" *");StringBuilder sb = new StringBuilder();sb.append(" * ");sb.append(MergeConstants.NEW_ELEMENT_TAG);if (markAsDoNotDelete) {sb.append(" do_not_delete_during_merge");}String s = getDateString();if (s != null) {sb.append(' ');sb.append(s);}javaElement.addJavaDocLine(sb.toString());}/*** This method returns a formated date string to include in the Javadoc tag* and XML comments. You may return null if you do not want the date in* these documentation elements.** @return a string representing the current timestamp, or null*/protected String getDateString() {String result = null;if (!suppressDate) {result = currentDateStr;}return result;}public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {if (suppressAllComments) {return;}StringBuilder sb = new StringBuilder();innerClass.addJavaDocLine("/**");sb.append(" * ");sb.append(introspectedTable.getFullyQualifiedTable());sb.append(" ");sb.append(getDateString());innerClass.addJavaDocLine(sb.toString());innerClass.addJavaDocLine(" */");}public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) {if (suppressAllComments) {return;}StringBuilder sb = new StringBuilder();innerEnum.addJavaDocLine("/**");// addJavadocTag(innerEnum, false);sb.append(" * ");sb.append(introspectedTable.getFullyQualifiedTable());innerEnum.addJavaDocLine(sb.toString());innerEnum.addJavaDocLine(" */");}public void addFieldComment(Field field, IntrospectedTable introspectedTable,IntrospectedColumn introspectedColumn) {if (suppressAllComments) {return;}StringBuilder sb = new StringBuilder();field.addJavaDocLine("/**");sb.append(" * ");sb.append(introspectedColumn.getRemarks());field.addJavaDocLine(sb.toString());// addJavadocTag(field, false); field.addJavaDocLine(" */");}public void addFieldComment(Field field, IntrospectedTable introspectedTable) {if (suppressAllComments) {return;}StringBuilder sb = new StringBuilder();field.addJavaDocLine("/**");sb.append(" * ");sb.append(introspectedTable.getFullyQualifiedTable());field.addJavaDocLine(sb.toString());field.addJavaDocLine(" */");}public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {}public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {if (suppressAllComments) {return;}// method.addJavaDocLine("/**");// addJavadocTag(method, false);// method.addJavaDocLine(" */"); }public void addGetterComment(Method method, IntrospectedTable introspectedTable,IntrospectedColumn introspectedColumn) {if (suppressAllComments) {return;}method.addJavaDocLine("/**");StringBuilder sb = new StringBuilder();sb.append(" * ");sb.append(introspectedColumn.getRemarks());method.addJavaDocLine(sb.toString());sb.setLength(0);sb.append(" * @return ");sb.append(introspectedColumn.getActualColumnName());sb.append(" ");sb.append(introspectedColumn.getRemarks());method.addJavaDocLine(sb.toString());// addJavadocTag(method, false); method.addJavaDocLine(" */");}public void addSetterComment(Method method, IntrospectedTable introspectedTable,IntrospectedColumn introspectedColumn) {if (suppressAllComments) {return;}method.addJavaDocLine("/**");StringBuilder sb = new StringBuilder();sb.append(" * ");sb.append(introspectedColumn.getRemarks());method.addJavaDocLine(sb.toString());Parameter parm = method.getParameters().get(0);sb.setLength(0);sb.append(" * @param ");sb.append(parm.getName());sb.append(" ");sb.append(introspectedColumn.getRemarks());method.addJavaDocLine(sb.toString());// addJavadocTag(method, false); method.addJavaDocLine(" */");}public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) {if (suppressAllComments) {return;}StringBuilder sb = new StringBuilder();innerClass.addJavaDocLine("/**");sb.append(" * ");sb.append(introspectedTable.getFullyQualifiedTable());innerClass.addJavaDocLine(sb.toString());sb.setLength(0);sb.append(" * @author ");sb.append(systemPro.getProperty("user.name"));sb.append(" ");sb.append(currentDateStr);// addJavadocTag(innerClass, markAsDoNotDelete); innerClass.addJavaDocLine(" */");} }3.2.再新建一個類StartUp,用于運行項目,也就是運行StartUp類 就會直接生成model,mapper以及mapper.xml,類的代碼如下:
package org.mybatis.generator;/*** Created by 草帽boy on 2017/2/16.* 啟動文件,只需要點擊運行就行*/ import java.io.File; import java.io.IOException; import java.net.URISyntaxException; import java.sql.SQLException; import java.util.ArrayList; import java.util.List;import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.exception.InvalidConfigurationException; import org.mybatis.generator.exception.XMLParserException; import org.mybatis.generator.internal.DefaultShellCallback;public class StartUp {public static void main(String[] args) throws URISyntaxException {try {List<String> warnings = new ArrayList<String>();boolean overwrite = true;//直接獲取generatorConfig.xml的文件路徑 根據具體情況查看File configFile = new File("E:\\IDEAWorkPlace\\GraduationDesign\\CourseDesignManageSystem\\20170122\\CourseDesignManage\\src\\main\\resources\\generatorConfig.xml");ConfigurationParser cp = new ConfigurationParser(warnings);Configuration config = cp.parseConfiguration(configFile);DefaultShellCallback callback = new DefaultShellCallback(overwrite);MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);myBatisGenerator.generate(null);} catch (SQLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();} catch (InvalidConfigurationException e) {e.printStackTrace();} catch (XMLParserException e) {e.printStackTrace();}} }3.3 另外需要簡單修改一下generatorConfig.xml中的注釋配置
<commentGenerator type="org.mybatis.generator.MyCommentGenerator"><!-- <property name="suppressDate" value="true"/><!– 是否去除自動生成的注釋 true:是 : false:否 –><property name="suppressAllComments" value="false"/>--></commentGenerator> commentGenerator 的type是你剛剛重構的MyCommentGenerator類的位置3.4其中我的文件結構如下:
四:點擊運行StartUp,你會發現model,mapper以及mapper.xml都已經產生,其中實體類的 運行的結果如下
更多參考:
mybatis-generator自定義注釋生成
Mybatis Generator的model生成中文注釋,支持oracle和mysql(通過實現CommentGenerator接口的方法來實現)
?
轉載于:https://www.cnblogs.com/luffyu/p/6408321.html
總結
以上是生活随笔為你收集整理的关于 mybatis-generator自定义注释生成 使用DefaultCommentGenerator重写来完成的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 全面介绍Windows内存管理机制及C+
- 下一篇: Qt之系统托盘(QSystemTrayI