作者:唯爱WE创丶 | 来源:互联网 | 2023-05-19 04:55
怎么在MyBatis中通过自定义Generator 生成注释?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
数据库建库建表的代码:
CREATE SCHEMA `db_test` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ;
CREATE TABLE `db_test`.`t_user` (
`id` INT NOT NULL AUTO_INCREMENT COMMENT '用户 ID',
`username` VARCHAR(30) NULL COMMENT '用户名称',
`password` VARCHAR(20) NULL COMMENT '用户密码',
`birthday` DATE NULL COMMENT '用户生日',
PRIMARY KEY (`id`),
UNIQUE INDEX `username_UNIQUE` (`username` ASC)
) COMMENT = '用户';
开开心心,执行命令,开始生成代码:
java -jar mybatis-generator-core-1.3.7.jar -configfile generatorConfig.xml -overwrite
然后查看生成的 Java 实体类:
看着这个注释,让我有点纠结啊 —— 为什么不是数据库中每个字段对应的注释呢?查找相关资料,得知 MBG 生成的是由 org.mybatis.generator.api.CommentGenerator
来控制的。这是一个接口,MBG 的默认实现类是做 org.mybatis.generator.internal.DefaultCommentGenerator
。当你在 generatorConfig.xml
中配置了 commentGenerator
标签,那么默认状态下,生成注释的工作,将由 DefaultCommentGenerator
来完成。 所以我们来查看下这个 DefaultCommentGenerator
的源码:
public class DefaultCommentGenerator implements CommentGenerator {
// 属性,即配置在 commentGenerator 标签之内的 Property 标签
private Properties properties;
// 是否不生成日期
private boolean suppressDate;
// 是否不生成注释
private boolean suppressAllComments;
// 是否添加数据库内的注释
private boolean addRemarkComments;
// 日期格式化
private SimpleDateFormat dateFormat;
public DefaultCommentGenerator() {
super();
properties = new Properties();
suppressDate = false;
suppressAllComments = false;
addRemarkComments = false;
}
@Override
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);
}
}
// 其他代码
...
}
addRemarkComments
这个属性,看来就是用来生成数据库注释用的 —— 好开心,那把它设置为 true
试试:
...
运行命令:
java -jar mybatis-generator-core-1.3.7.jar -configfile generatorConfig.xml -overwrite
数据库注释倒是拿到了,但是生成的一堆其他信息,看着实在是太扎眼了。查看源码,发现这些内容已经写死在 DefaultCommentGenerator
中了,没有办法自定义。
自己动手丰衣足食,我们为啥不自己写个类实现 CommentGenerator
接口,然后自定义自己想要的注释呢。查看 commentGenerator
的 DTD,发现正好 commentGenerator
有个 type
属性,可以用来指定自己的注释实现类:
查看 CommentGenerator
接口,发现里面的方法非常多,不仅包含了生成 Java 实体注释对应的方法,还包括了生成 XML 中注释的方法。所以我们先写一个默认的实现类,实现CommentGenerator
接口,但不做任何操作 —— 因为 DefaultCommentGenerator
本文已经存在了,为了避免混淆,就叫它SimpleCommentGenerator
吧。然后定义我们自己的注释类,MySQLCommentGenerator
,继承 SimpleCommentGenerator
,重写我们需要的方法:
public class MySQLCommentGenerator extends SimpleCommentGenerator {
private 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(" */");
}
}
因为我们现在要使用到我们自己自定义的 CommentGenerator
,所以我们 通过代码的方式来操作 MBG:
public class Generator {
public static void main( String[] args ) throws Exception {
List warnings = new ArrayList<>();
File configFile = new File("generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(true);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}
}
然后配置 generatorConfig.xml
设置我们自己的注释生成器:
...
...
完整的 Maven 项目在 我的 GitHub。现在,我们运行主类 Generator
,成功生成了数据库中的注释:
想来应该是 JDBC 连接 MySQL 的时候需要添加什么属性才能获取表的注释,上网查询,发现是 useInformationSchema
,需要将其设置为 true
(看来是 MBG 给自己的 DefaultCommentGenerator
开了小灶):
...
...
然后再次运行主类 Generator
:
看完上述内容,你们掌握怎么在MyBatis中通过自定义Generator 生成注释的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注编程笔记行业资讯频道,感谢各位的阅读!