设为首页 加入收藏

TOP

把Mybatis Generator生成的代码加上想要的注释(二)
2023-07-25 21:34:17 】 浏览:52
Tags:Mybatis Generator 成的代 加上想
ct
="src/main/resources"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!-- 生成 Mapper 接口的位置 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.jd.bulk" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- 设置数据库的表名和实体类名 --> <table tableName="worker" domainObjectName="Worker"/> </context> </generatorConfiguration>

 

2.3 创建main方法,运行Generator

public class Generator {
public static void main(String[] args) throws Exception {
List<String> warnings = new ArrayList<>(2);
ConfigurationParser cp = new ConfigurationParser(warnings);
File configFile = new File("src/main/resources/generatorConfig.xml");
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(true);
MyBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}
}

 

运行main方法,生成默认注释如下,并不是我们想要的注释,所以一般会配置为注释不生成:

2.4 实现CommentGenerator接口

重写以下方法,自定义注释

public class MySQLCommentGenerator implements CommentGenerator {
private final Properties properties;
public MySQLCommentGenerator() {
properties = new Properties();
}
@Override
public void addConfigurationProperties(Properties properties) {
// 获取自定义的 properties
this.properties.putAll(properties);
}
/**
* 重写给实体类加的注释
*/
@Override
public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
String author = properties.getProperty("author");
String dateFormat = properties.getProperty("dateFormat", "yyyy-MM-dd");
SimpleDateFormat dateFormatter = new SimpleDateFormat(dateFormat);
// 获取表注释
String remarks = introspectedTable.getRemarks();
topLevelClass.addJavaDocLine("/**");
topLevelClass.addJavaDocLine(" * " + remarks);
topLevelClass.addJavaDocLine(" *");
topLevelClass.addJavaDocLine(" * @author " + author);
topLevelClass.addJavaDocLine(" * @date " + dateFormatter.format(new Date()));
topLevelClass.addJavaDocLine(" */");
}
/**
* 重写给实体类字段加的注释
*/
@Override
public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
// 获取列注释
String remarks = introspectedColumn.getRemarks();
field.addJavaDocLine("/**");
field.addJavaDocLine(" * " + remarks);
field.addJavaDocLine(" */");
}
/**
* 重写给实体类get方法加的注释
*/
@Override
public void addGetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
// 获取表注释
String remarks = introspectedColumn.getRemarks();
method.addJavaDocLine("/**");
method.addJavaDocLine(" * " + method.getName());
method.addJavaDocLine(" */");
}

 

2.5 修改generatorConfig.xml配置

将generatorConfig.xml文件中的commentGenerator做如下修改,type属性选择自己的实现类

<commentGenerator type="com.generator.MySQLCommentGenerator">
<property na
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇day22-web开发会话技术04 下一篇java 基础——函数(方法)

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目