在 Gradle项目使用MyBatis-generator自动生成MyBatis映射代码 知道了如何生成对应代码,但是还有个问题,生成的实体类并没有带上注释,改如何处理:
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.internal.DefaultCommentGenerator;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.InnerClass;
import org.mybatis.generator.api.dom.java.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;/*** mybatis generator生成注释插件*/
public class CustomerCommentGenerator extends DefaultCommentGenerator {private Properties properties;private Properties systemPro;private boolean suppressDate;private boolean suppressAllComments;private String currentDateStr;private String currentTimeStr;public CustomerCommentGenerator() {super();properties = new Properties();systemPro = System.getProperties();suppressDate = false;suppressAllComments = false;currentDateStr = (new SimpleDateFormat("yyyy-MM-dd")).format(new Date());currentTimeStr = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date());}/*** 添加字段注释,主要是处理这个*/public void addFieldComment(Field field, IntrospectedTable introspectedTable,IntrospectedColumn introspectedColumn) {if (suppressAllComments) {return;}field.addJavaDocLine("/** " + introspectedColumn.getRemarks() + " */");}public void addFieldComment(Field field, IntrospectedTable introspectedTable) {}public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {}public void addGetterComment(Method method, IntrospectedTable introspectedTable,IntrospectedColumn introspectedColumn) {}public void addSetterComment(Method method, IntrospectedTable introspectedTable,IntrospectedColumn introspectedColumn) {}public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) {}public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {}}
因为mybasis-generate插件的问题,自定义的,直接用
mbGrenerator 会报错。
因此改用main方法进行处理:
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;public class MyBatisGenerator {public static void main(String[] args) throws Exception {MyBatisGenerator customer = new MyBatisGenerator();System.out.println(customer.getClass().getResource("/").getPath());customer.generator();}private void generator() throws Exception {List warnings = new ArrayList<>();InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("generatorConfigMb.xml");ConfigurationParser cp = new ConfigurationParser(warnings);Configuration config = cp.parseConfiguration(resourceAsStream);DefaultShellCallback callback = new DefaultShellCallback(true);org.mybatis.generator.api.MyBatisGenerator myBatisGenerator = new org.mybatis.generator.api.MyBatisGenerator(config, callback, warnings);myBatisGenerator.generate(null);}
}
这个同自定义,反编译逆向工程源码后修改DefaultCommentGenerator类里面addFieldComment方法
public void addFieldComment(Field field, IntrospectedTable introspectedTable,IntrospectedColumn introspectedColumn) {if (suppressAllComments) {return;}field.addJavaDocLine("/** " + introspectedColumn.getRemarks() + " */");}
然后修改版本号,再上传。
这样的好处就是可以直接点击 mbGrenerator 按钮生成。
第一种方式比较常用,也方便。第二种方式更适合本地用。生成了注释就比较方便,不用手动加。