博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mybatis generator 生成中文注释
阅读量:6068 次
发布时间:2019-06-20

本文共 18175 字,大约阅读时间需要 60 分钟。

 mybatis generator默认生成 的注释太奇葩了,完全不能拿到生产去用,不过幸亏提供了接口可以自己扩展。长话短说,要生成如下的domain,

package com.demo.domain;/** test_mybatis   */public class TestMybatis {    /** 主键 */    private Integer id;    /** 字段说明 */    private String name;    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }}
id, name
delete from test_mybatis where id = #{id,jdbcType=INTEGER}
SELECT LAST_INSERT_ID()
insert into test_mybatis (name) values (#{name,jdbcType=VARCHAR})
SELECT LAST_INSERT_ID()
insert into test_mybatis
name,
#{name,jdbcType=VARCHAR},
update test_mybatis
name = #{name,jdbcType=VARCHAR},
where id = #{id,jdbcType=INTEGER}
update test_mybatis set name = #{name,jdbcType=VARCHAR} where id = #{id,jdbcType=INTEGER}

 

同时mapper.xml中也不要注释,可通过更改org.mybatis.generator.internal.DefaultCommentGenerator,并重新编译实现,如下:

/** *    Copyright 2006-2016 the original author or authors. * *    Licensed under the Apache License, Version 2.0 (the "License"); *    you may not use this file except in compliance with the License. *    You may obtain a copy of the License at * *       http://www.apache.org/licenses/LICENSE-2.0 * *    Unless required by applicable law or agreed to in writing, software *    distributed under the License is distributed on an "AS IS" BASIS, *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *    See the License for the specific language governing permissions and *    limitations under the License. */package org.mybatis.generator.internal;import static org.mybatis.generator.internal.util.StringUtility.isTrue;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.CompilationUnit;import org.mybatis.generator.api.dom.java.Field;import org.mybatis.generator.api.dom.java.InnerClass;import org.mybatis.generator.api.dom.java.InnerEnum;import org.mybatis.generator.api.dom.java.JavaElement;import org.mybatis.generator.api.dom.java.Method;import org.mybatis.generator.api.dom.java.Parameter;import org.mybatis.generator.api.dom.java.TopLevelClass;import org.mybatis.generator.api.dom.xml.TextElement;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;/** * The Class DefaultCommentGenerator. * * @author Jeff Butler */public class DefaultCommentGenerator implements CommentGenerator {    /** The properties. */    private Properties properties;        /** The suppress date. */    private boolean suppressDate;        /** The suppress all comments. */    private boolean suppressAllComments;    /** The addition of table remark's comments.     * If suppressAllComments is true, this option is ignored*/    private boolean addRemarkComments;        private SimpleDateFormat dateFormat;    /**     * Instantiates a new default comment generator.     */    public DefaultCommentGenerator() {        super();        properties = new Properties();        suppressDate = false;        suppressAllComments = false;        addRemarkComments = false;    }    /* (non-Javadoc)     * @see org.mybatis.generator.api.CommentGenerator#addJavaFileComment(org.mybatis.generator.api.dom.java.CompilationUnit)     */    public void addJavaFileComment(CompilationUnit compilationUnit) {        // add no file level comments by default    }    /**     * Adds a suitable comment to warn users that the element was generated, and when it was generated.     *     * @param xmlElement     *            the xml element     */    public void addComment(XmlElement xmlElement) {        if (suppressAllComments) {            return;        }//        xmlElement.addElement(new TextElement("
")); //$NON-NLS-1$ } /* (non-Javadoc) * @see org.mybatis.generator.api.CommentGenerator#addRootComment(org.mybatis.generator.api.dom.xml.XmlElement) */ public void addRootComment(XmlElement rootElement) { // add no document level comments by default } /* (non-Javadoc) * @see org.mybatis.generator.api.CommentGenerator#addConfigurationProperties(java.util.Properties) */ public void addConfigurationProperties(Properties properties) { this.properties.putAll(properties); suppressDate = isTrue(properties .getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_DATE)); suppressAllComments = isTrue(properties .getProperty(PropertyRegistry.COMMENT_GENERATOR_SUPPRESS_ALL_COMMENTS)); addRemarkComments = isTrue(properties .getProperty(PropertyRegistry.COMMENT_GENERATOR_ADD_REMARK_COMMENTS)); String dateFormatString = properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_DATE_FORMAT); if (StringUtility.stringHasValue(dateFormatString)) { dateFormat = new SimpleDateFormat(dateFormatString); } } /** * 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 * @param markAsDoNotDelete * the mark as do not delete */ protected void addJavadocTag(JavaElement javaElement, boolean markAsDoNotDelete) { javaElement.addJavaDocLine(" *"); //$NON-NLS-1$ StringBuilder sb = new StringBuilder(); sb.append(" * "); //$NON-NLS-1$ sb.append(MergeConstants.NEW_ELEMENT_TAG); if (markAsDoNotDelete) { sb.append(" do_not_delete_during_merge"); //$NON-NLS-1$ } 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() { if (suppressDate) { return null; } else if (dateFormat != null) { return dateFormat.format(new Date()); } else { return new Date().toString(); } } /* (non-Javadoc) * @see org.mybatis.generator.api.CommentGenerator#addClassComment(org.mybatis.generator.api.dom.java.InnerClass, org.mybatis.generator.api.IntrospectedTable) */ public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) { if (suppressAllComments) { return; } innerClass.addJavaDocLine("/**" + introspectedTable.getFullyQualifiedTable() + " " + introspectedTable.getRemarks() + "*/"); //$NON-NLS-1$// StringBuilder sb = new StringBuilder();//// innerClass.addJavaDocLine("/**"); //$NON-NLS-1$// innerClass// .addJavaDocLine(" * This class was generated by MyBatis Generator."); //$NON-NLS-1$//// sb.append(" * This class corresponds to the database table "); //$NON-NLS-1$// sb.append(introspectedTable.getFullyQualifiedTable());// innerClass.addJavaDocLine(sb.toString());//// addJavadocTag(innerClass, false);//// innerClass.addJavaDocLine(" */"); //$NON-NLS-1$ } /* (non-Javadoc) * @see org.mybatis.generator.api.CommentGenerator#addTopLevelClassComment(org.mybatis.generator.api.dom.java.TopLevelClass, org.mybatis.generator.api.IntrospectedTable) */ @Override public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { if (suppressAllComments || !addRemarkComments) { return; } topLevelClass.addJavaDocLine("/** " + introspectedTable.getFullyQualifiedTable() + " " + introspectedTable.getRemarks() + " */"); //$NON-NLS-1$// StringBuilder sb = new StringBuilder();//// topLevelClass.addJavaDocLine("/**"); //$NON-NLS-1$//// String remarks = introspectedTable.getRemarks();// if (addRemarkComments && StringUtility.stringHasValue(remarks)) {// topLevelClass.addJavaDocLine(" * Database Table Remarks:");// String[] remarkLines = remarks.split(System.getProperty("line.separator")); //$NON-NLS-1$// for (String remarkLine : remarkLines) {// topLevelClass.addJavaDocLine(" * " + remarkLine); //$NON-NLS-1$// }// }// topLevelClass.addJavaDocLine(" *"); //$NON-NLS-1$//// topLevelClass// .addJavaDocLine(" * This class was generated by MyBatis Generator."); //$NON-NLS-1$//// sb.append(" * This class corresponds to the database table "); //$NON-NLS-1$// sb.append(introspectedTable.getFullyQualifiedTable());// topLevelClass.addJavaDocLine(sb.toString());//// addJavadocTag(topLevelClass, true);//// topLevelClass.addJavaDocLine(" */"); //$NON-NLS-1$ } /* (non-Javadoc) * @see org.mybatis.generator.api.CommentGenerator#addEnumComment(org.mybatis.generator.api.dom.java.InnerEnum, org.mybatis.generator.api.IntrospectedTable) */ public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) { if (suppressAllComments) { return; } StringBuilder sb = new StringBuilder(); innerEnum.addJavaDocLine("/**"); //$NON-NLS-1$ innerEnum .addJavaDocLine(" * This enum was generated by MyBatis Generator."); //$NON-NLS-1$ sb.append(" * This enum corresponds to the database table "); //$NON-NLS-1$ sb.append(introspectedTable.getFullyQualifiedTable()); innerEnum.addJavaDocLine(sb.toString()); addJavadocTag(innerEnum, false); innerEnum.addJavaDocLine(" */"); //$NON-NLS-1$ } /* (non-Javadoc) * @see org.mybatis.generator.api.CommentGenerator#addFieldComment(org.mybatis.generator.api.dom.java.Field, org.mybatis.generator.api.IntrospectedTable, org.mybatis.generator.api.IntrospectedColumn) */ public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { if (suppressAllComments) { return; } field.addJavaDocLine("/** "+introspectedColumn.getRemarks()+" */");// field.addJavaDocLine("/**"); //$NON-NLS-1$//// String remarks = introspectedColumn.getRemarks();// if (addRemarkComments && StringUtility.stringHasValue(remarks)) {// field.addJavaDocLine(" * Database Column Remarks:");// String[] remarkLines = remarks.split(System.getProperty("line.separator")); //$NON-NLS-1$// for (String remarkLine : remarkLines) {// field.addJavaDocLine(" * " + remarkLine); //$NON-NLS-1$// }// }//// field.addJavaDocLine(" *"); //$NON-NLS-1$// field// .addJavaDocLine(" * This field was generated by MyBatis Generator."); //$NON-NLS-1$//// StringBuilder sb = new StringBuilder();// sb.append(" * This field corresponds to the database column "); //$NON-NLS-1$// sb.append(introspectedTable.getFullyQualifiedTable());// sb.append('.');// sb.append(introspectedColumn.getActualColumnName());// field.addJavaDocLine(sb.toString());//// addJavadocTag(field, false);//// field.addJavaDocLine(" */"); //$NON-NLS-1$ } /* (non-Javadoc) * @see org.mybatis.generator.api.CommentGenerator#addFieldComment(org.mybatis.generator.api.dom.java.Field, org.mybatis.generator.api.IntrospectedTable) */ public void addFieldComment(Field field, IntrospectedTable introspectedTable) { if (suppressAllComments) { return; } StringBuilder sb = new StringBuilder(); field.addJavaDocLine("/**"); //$NON-NLS-1$ field .addJavaDocLine(" * This field was generated by MyBatis Generator."); //$NON-NLS-1$ sb.append(" * This field corresponds to the database table "); //$NON-NLS-1$ sb.append(introspectedTable.getFullyQualifiedTable()); field.addJavaDocLine(sb.toString()); addJavadocTag(field, false); field.addJavaDocLine(" */"); //$NON-NLS-1$ } /* (non-Javadoc) * @see org.mybatis.generator.api.CommentGenerator#addGeneralMethodComment(org.mybatis.generator.api.dom.java.Method, org.mybatis.generator.api.IntrospectedTable) */ public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) { if (suppressAllComments) { return; } StringBuilder sb = new StringBuilder(); method.addJavaDocLine("/**"); //$NON-NLS-1$ method .addJavaDocLine(" * This method was generated by MyBatis Generator."); //$NON-NLS-1$ sb.append(" * This method corresponds to the database table "); //$NON-NLS-1$ sb.append(introspectedTable.getFullyQualifiedTable()); method.addJavaDocLine(sb.toString()); addJavadocTag(method, false); method.addJavaDocLine(" */"); //$NON-NLS-1$ } /* (non-Javadoc) * @see org.mybatis.generator.api.CommentGenerator#addGetterComment(org.mybatis.generator.api.dom.java.Method, org.mybatis.generator.api.IntrospectedTable, org.mybatis.generator.api.IntrospectedColumn) */ public void addGetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { if (suppressAllComments) { return; }// StringBuilder sb = new StringBuilder();//// method.addJavaDocLine("/**"); //$NON-NLS-1$// method// .addJavaDocLine(" * This method was generated by MyBatis Generator."); //$NON-NLS-1$//// sb.append(" * This method returns the value of the database column "); //$NON-NLS-1$// sb.append(introspectedTable.getFullyQualifiedTable());// sb.append('.');// sb.append(introspectedColumn.getActualColumnName());// method.addJavaDocLine(sb.toString());//// method.addJavaDocLine(" *"); //$NON-NLS-1$//// sb.setLength(0);// sb.append(" * @return the value of "); //$NON-NLS-1$// sb.append(introspectedTable.getFullyQualifiedTable());// sb.append('.');// sb.append(introspectedColumn.getActualColumnName());// method.addJavaDocLine(sb.toString());//// addJavadocTag(method, false);//// method.addJavaDocLine(" */"); //$NON-NLS-1$ } /* (non-Javadoc) * @see org.mybatis.generator.api.CommentGenerator#addSetterComment(org.mybatis.generator.api.dom.java.Method, org.mybatis.generator.api.IntrospectedTable, org.mybatis.generator.api.IntrospectedColumn) */ public void addSetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) { if (suppressAllComments) { return; }// StringBuilder sb = new StringBuilder();//// method.addJavaDocLine("/**"); //$NON-NLS-1$// method// .addJavaDocLine(" * This method was generated by MyBatis Generator."); //$NON-NLS-1$//// sb.append(" * This method sets the value of the database column "); //$NON-NLS-1$// sb.append(introspectedTable.getFullyQualifiedTable());// sb.append('.');// sb.append(introspectedColumn.getActualColumnName());// method.addJavaDocLine(sb.toString());//// method.addJavaDocLine(" *"); //$NON-NLS-1$//// Parameter parm = method.getParameters().get(0);// sb.setLength(0);// sb.append(" * @param "); //$NON-NLS-1$// sb.append(parm.getName());// sb.append(" the value for "); //$NON-NLS-1$// sb.append(introspectedTable.getFullyQualifiedTable());// sb.append('.');// sb.append(introspectedColumn.getActualColumnName());// method.addJavaDocLine(sb.toString());//// addJavadocTag(method, false);//// method.addJavaDocLine(" */"); //$NON-NLS-1$ } /* (non-Javadoc) * @see org.mybatis.generator.api.CommentGenerator#addClassComment(org.mybatis.generator.api.dom.java.InnerClass, org.mybatis.generator.api.IntrospectedTable, boolean) */ public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) { if (suppressAllComments) { return; } innerClass.addJavaDocLine("/**" + introspectedTable.getFullyQualifiedTable() + " " + introspectedTable.getRemarks() + "*/"); //$NON-NLS-1$// StringBuilder sb = new StringBuilder();//// innerClass.addJavaDocLine("/**"); //$NON-NLS-1$// innerClass// .addJavaDocLine(" * This class was generated by MyBatis Generator."); //$NON-NLS-1$//// sb.append(" * This class corresponds to the database table "); //$NON-NLS-1$// sb.append(introspectedTable.getFullyQualifiedTable());// innerClass.addJavaDocLine(sb.toString());//// addJavadocTag(innerClass, markAsDoNotDelete);//// innerClass.addJavaDocLine(" */"); //$NON-NLS-1$ }}

重新使用maven编译,替换官方的即可。附上已经替换的版本。

转载地址:http://yhygx.baihongyu.com/

你可能感兴趣的文章
java.io.Serializable浅析
查看>>
我的友情链接
查看>>
多线程之线程池任务管理通用模板
查看>>
CSS3让长单词与URL地址自动换行——word-wrap属性
查看>>
CodeForces 580B Kefa and Company
查看>>
开发规范浅谈
查看>>
Spark Streaming揭秘 Day29 深入理解Spark2.x中的Structured Streaming
查看>>
鼠标增强软件StrokeIt使用方法
查看>>
本地连接linux虚拟机的方法
查看>>
某公司面试java试题之【二】,看看吧,说不定就是你将要做的题
查看>>
BABOK - 企业分析(Enterprise Analysis)概要
查看>>
Linux 配置vnc,开启linux远程桌面
查看>>
NLog文章系列——如何优化日志性能
查看>>
Hadoop安装测试简单记录
查看>>
CentOS6.4关闭触控板
查看>>
ThreadPoolExecutor线程池运行机制分析-线程复用原理
查看>>
React Native 极光推送填坑(ios)
查看>>
Terratest:一个用于自动化基础设施测试的开源Go库
查看>>
修改Windows远程终端默认端口,让服务器更安全
查看>>
扩展器必须,SAS 2.0未必(SAS挺进中端存储系统之三)
查看>>