1 大胆的想法
在基础篇中可以看到,生成的 Mapper 和持久化类(实体)中包含了大量的无意义的英文注释,对于实体外的其他类,当然要去掉那些无用的注释。而对于实体类,则有一个大胆的想法——根据 DDL 里面的列注释内容来生成注释。
user 表 DDL
CREATE TABLE `user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '自增主键',
`name` varchar(100) NOT NULL DEFAULT '' COMMENT '用户名',
`password` varchar(100) NOT NULL DEFAULT '' COMMENT '密码',
`create_time` bigint(20) NOT NULL DEFAULT '0' COMMENT '创建时间',
`update_time` bigint(20) NOT NULL DEFAULT '0' COMMENT '最后更新时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8
理想中的实体 User.java
package org.example.user.domain;
/**
* @author: 青城山上学编程
* @dateTime: 2022/10/09 11:47
*/
public class User {
/**
* 自增主键
*/
private Long id;
/**
* 用户名
*/
private String name;
/**
* 密码
*/
private String password;
/**
* 创建时间
*/
private Long createTime;
/**
* 最后更新时间
*/
private Long updateTime;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Long getCreateTime() {
return createTime;
}
public void setCreateTime(Long createTime) {
this.createTime = createTime;
}
public Long getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Long updateTime) {
this.updateTime = updateTime;
}
}
要想达到这个目的,需要经历以下两个步骤。
2 实现注释生成器
CustomCommentGenerator.java
package com.zwj.mybatis.generator;
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.*;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.internal.util.StringUtility;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.Set;
/**
* 自定义注释生成器
*/
public class CustomCommentGenerator implements CommentGenerator {
private final Properties properties;
public CustomCommentGenerator() {
properties = new Properties();
}
@Override
public void addConfigurationProperties(Properties properties) {
// 获取自定义的 properties
this.properties.putAll(properties);
}
@Override
public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
if (StringUtility.stringHasValue(introspectedColumn.getRemarks())) {
field.addJavaDocLine("/**");
field.addJavaDocLine(" * " + introspectedColumn.getRemarks());
field.addJavaDocLine(" */");
}
}
@Override
public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
String author = properties.getProperty("author");
String dateFormat = properties.getProperty("dateFormat");
SimpleDateFormat dateFormatter = new SimpleDateFormat(dateFormat);
topLevelClass.addJavaDocLine("/**");
// 获取表注释
if (StringUtility.stringHasValue(introspectedTable.getRemarks())) {
topLevelClass.addJavaDocLine(" * " + introspectedTable.getRemarks());
topLevelClass.addJavaDocLine(" *");
}
topLevelClass.addJavaDocLine(" * @author: " + author);
topLevelClass.addJavaDocLine(" * @dateTime: " + dateFormatter.format(new Date()));
topLevelClass.addJavaDocLine(" */");
}
@Override
public void addFieldComment(Field field, IntrospectedTable introspectedTable) {
}
@Override
public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {
}
@Override
public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) {
}
@Override
public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) {
}
@Override
public void addGetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
}
@Override
public void addSetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
}
@Override
public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {
}
@Override
public void addJavaFileComment(CompilationUnit compilationUnit) {
}
@Override
public void addComment(XmlElement xmlElement) {
}
@Override
public void addRootComment(XmlElement rootElement) {
}
@Override
public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable, Set<FullyQualifiedJavaType> imports) {
}
@Override
public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> imports) {
}
@Override
public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable, Set<FullyQualifiedJavaType> imports) {
}
@Override
public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> imports) {
}
@Override
public void addClassAnnotation(InnerClass innerClass, IntrospectedTable introspectedTable, Set<FullyQualifiedJavaType> imports) {
}
}
3 在生成配置中使用注释生成器
generatorConfig.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="Mysql" targetRuntime="MyBatis3" defaultModelType="flat">
<!-- 表名前后加上''的分隔符 -->
<property name="beginningDelimiter" value="`" />
<property name="endingDelimiter" value="`" />
<!-- 配置注释 -->
<commentGenerator type="com.zwj.mybatis.generator.CustomCommentGenerator">
<!-- 定义生成实体类注解所需参数 -->
<property name="author" value="青城山上学编程" />
<property name="dateFormat" value="yyyy/MM/dd HH:mm" />
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://192.168.1.188:3307/user-center"
userId="root" password="123456" />
<!-- 对于生成的pojo所在包 -->
<javaModelGenerator targetPackage="org.example.user.domain" targetProject="src\main\java" />
<!-- 对于生成的mapper所在目录 -->
<sqlMapGenerator targetPackage="org.example.user.dao" targetProject="src\main\resources" />
<!-- 配置dao对应的映射 -->
<javaClientGenerator targetPackage="org.example.user.dao" targetProject="src\main\java"
type="ANNOTATEDMAPPER" />
<table tableName="%">
<!--insert 方法返回主键-->
<generatedKey column="id" sqlStatement="MySql" identity="true" />
</table>
</context>
</generatorConfiguration>
使用 <commentGenerator> 配置自定义注释生成器,type 为注释生成器的全路径名。配置完成后,重新运行 main 方法,生成的实体就有注释了。