MyBatis-generator生成的实体带注释
创始人
2025-05-29 00:49:03

在 Gradle项目使用MyBatis-generator自动生成MyBatis映射代码 知道了如何生成对应代码,但是还有个问题,生成的实体类并没有带上注释,改如何处理:

方法1: 自定义

CustomerCommentGenerator 


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方法进行处理:

MyBatisGenerator

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);}
}

效果:

方法2:反编译逆向工程源码后修改DefaultCommentGenerator类

这个同自定义,反编译逆向工程源码后修改DefaultCommentGenerator类里面addFieldComment方法

public void addFieldComment(Field field, IntrospectedTable introspectedTable,IntrospectedColumn introspectedColumn) {if (suppressAllComments) {return;}field.addJavaDocLine("/** " + introspectedColumn.getRemarks()  + " */");}

然后修改版本号,再上传。

这样的好处就是可以直接点击 mbGrenerator 按钮生成。

总结:

        第一种方式比较常用,也方便。第二种方式更适合本地用。生成了注释就比较方便,不用手动加。

相关内容

热门资讯

苗族的传统节日 贵州苗族节日有... 【岜沙苗族芦笙节】岜沙,苗语叫“分送”,距从江县城7.5公里,是世界上最崇拜树木并以树为神的枪手部落...
北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...
长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...
世界上最漂亮的人 世界上最漂亮... 此前在某网上,选出了全球265万颜值姣好的女性。从这些数量庞大的女性群体中,人们投票选出了心目中最美...