mybatis逆向工程插件优化
This commit is contained in:
+218
@@ -0,0 +1,218 @@
|
|||||||
|
package com.example.workutils.biz.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.Field;
|
||||||
|
import org.mybatis.generator.api.dom.java.InnerClass;
|
||||||
|
import org.mybatis.generator.api.dom.java.InnerEnum;
|
||||||
|
import org.mybatis.generator.api.dom.java.Method;
|
||||||
|
import org.mybatis.generator.api.dom.java.TopLevelClass;
|
||||||
|
import org.mybatis.generator.api.dom.xml.XmlElement;
|
||||||
|
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自定义MyBatis Generator注释生成器
|
||||||
|
* 生成简洁的中文注释:
|
||||||
|
* - 字段注释:只保留数据库字段备注
|
||||||
|
* - 方法注释:根据方法名生成中文说明
|
||||||
|
* - XML注释:不生成任何注释
|
||||||
|
*/
|
||||||
|
public class CustomCommentGenerator implements CommentGenerator {
|
||||||
|
|
||||||
|
private Properties properties;
|
||||||
|
private boolean addRemarkComments;
|
||||||
|
|
||||||
|
public CustomCommentGenerator() {
|
||||||
|
super();
|
||||||
|
properties = new Properties();
|
||||||
|
addRemarkComments = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addConfigurationProperties(Properties properties) {
|
||||||
|
this.properties.putAll(properties);
|
||||||
|
String addRemark = properties.getProperty("addRemarkComments");
|
||||||
|
addRemarkComments = addRemark == null || "true".equalsIgnoreCase(addRemark);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 为字段添加注释 - 只保留数据库备注
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
|
||||||
|
if (addRemarkComments && introspectedColumn.getRemarks() != null && !introspectedColumn.getRemarks().isEmpty()) {
|
||||||
|
field.addJavaDocLine("/**");
|
||||||
|
field.addJavaDocLine(" * " + introspectedColumn.getRemarks());
|
||||||
|
field.addJavaDocLine(" */");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 为字段添加注释(无列信息时)- 不添加注释
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void addFieldComment(Field field, IntrospectedTable introspectedTable) {
|
||||||
|
// 不添加注释
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 为类添加注释 - 添加表备注
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {
|
||||||
|
if (addRemarkComments && introspectedTable.getRemarks() != null && !introspectedTable.getRemarks().isEmpty()) {
|
||||||
|
innerClass.addJavaDocLine("/**");
|
||||||
|
innerClass.addJavaDocLine(" * " + introspectedTable.getRemarks());
|
||||||
|
innerClass.addJavaDocLine(" */");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) {
|
||||||
|
addClassComment(innerClass, introspectedTable);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 为枚举添加注释 - 不添加
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) {
|
||||||
|
// 不添加注释
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 为Getter方法添加注释
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void addGetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
|
||||||
|
if (addRemarkComments && introspectedColumn.getRemarks() != null && !introspectedColumn.getRemarks().isEmpty()) {
|
||||||
|
method.addJavaDocLine("/**");
|
||||||
|
method.addJavaDocLine(" * 获取" + introspectedColumn.getRemarks());
|
||||||
|
method.addJavaDocLine(" */");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 为Setter方法添加注释
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void addSetterComment(Method method, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
|
||||||
|
if (addRemarkComments && introspectedColumn.getRemarks() != null && !introspectedColumn.getRemarks().isEmpty()) {
|
||||||
|
method.addJavaDocLine("/**");
|
||||||
|
method.addJavaDocLine(" * 设置" + introspectedColumn.getRemarks());
|
||||||
|
method.addJavaDocLine(" */");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 为普通方法添加注释 - 根据方法名生成中文说明
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {
|
||||||
|
String methodName = method.getName();
|
||||||
|
String comment = getMethodComment(methodName);
|
||||||
|
|
||||||
|
if (comment != null) {
|
||||||
|
method.addJavaDocLine("/**");
|
||||||
|
method.addJavaDocLine(" * " + comment);
|
||||||
|
method.addJavaDocLine(" */");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据方法名获取中文注释
|
||||||
|
*/
|
||||||
|
private String getMethodComment(String methodName) {
|
||||||
|
if (methodName.startsWith("deleteBy")) {
|
||||||
|
return "根据" + extractCondition(methodName, "deleteBy") + "删除";
|
||||||
|
}
|
||||||
|
if (methodName.startsWith("insert")) {
|
||||||
|
if (methodName.equals("insert")) {
|
||||||
|
return "插入记录";
|
||||||
|
}
|
||||||
|
if (methodName.equals("insertSelective")) {
|
||||||
|
return "选择性插入记录(只插入非空字段)";
|
||||||
|
}
|
||||||
|
return "插入记录";
|
||||||
|
}
|
||||||
|
if (methodName.startsWith("updateBy")) {
|
||||||
|
return "根据" + extractCondition(methodName, "updateBy") + "更新";
|
||||||
|
}
|
||||||
|
if (methodName.startsWith("selectBy")) {
|
||||||
|
return "根据" + extractCondition(methodName, "selectBy") + "查询";
|
||||||
|
}
|
||||||
|
if (methodName.startsWith("countBy")) {
|
||||||
|
return "根据" + extractCondition(methodName, "countBy") + "统计数量";
|
||||||
|
}
|
||||||
|
if (methodName.equals("selectAll")) {
|
||||||
|
return "查询所有记录";
|
||||||
|
}
|
||||||
|
if (methodName.equals("selectByPrimaryKey")) {
|
||||||
|
return "根据主键查询";
|
||||||
|
}
|
||||||
|
if (methodName.equals("updateByPrimaryKey")) {
|
||||||
|
return "根据主键更新";
|
||||||
|
}
|
||||||
|
if (methodName.equals("updateByPrimaryKeySelective")) {
|
||||||
|
return "根据主键选择性更新(只更新非空字段)";
|
||||||
|
}
|
||||||
|
if (methodName.equals("deleteByPrimaryKey")) {
|
||||||
|
return "根据主键删除";
|
||||||
|
}
|
||||||
|
if (methodName.startsWith("select")) {
|
||||||
|
return "查询记录";
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 提取条件部分
|
||||||
|
* 例如:deleteByPrimaryKey -> 主键
|
||||||
|
*/
|
||||||
|
private String extractCondition(String methodName, String prefix) {
|
||||||
|
String condition = methodName.substring(prefix.length());
|
||||||
|
if (condition.equals("PrimaryKey")) {
|
||||||
|
return "主键";
|
||||||
|
}
|
||||||
|
if (condition.equals("Id")) {
|
||||||
|
return "ID";
|
||||||
|
}
|
||||||
|
if (condition.equals("Key")) {
|
||||||
|
return "键";
|
||||||
|
}
|
||||||
|
return condition;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 为顶级类添加注释 - 添加表备注作为类注释
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
|
||||||
|
if (addRemarkComments) {
|
||||||
|
String remarks = introspectedTable.getRemarks();
|
||||||
|
if (remarks != null && !remarks.isEmpty()) {
|
||||||
|
topLevelClass.addJavaDocLine("/**");
|
||||||
|
topLevelClass.addJavaDocLine(" * " + remarks);
|
||||||
|
topLevelClass.addJavaDocLine(" */");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* XML元素注释 - 不添加任何注释
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void addComment(XmlElement xmlElement) {
|
||||||
|
// 不添加注释
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* XML根节点注释 - 不添加
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void addRootComment(XmlElement rootElement) {
|
||||||
|
// 不添加注释
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
package com.example.workutils.biz.mybatis.generator;
|
||||||
|
|
||||||
|
import org.mybatis.generator.api.IntrospectedColumn;
|
||||||
|
import org.mybatis.generator.api.IntrospectedTable;
|
||||||
|
import org.mybatis.generator.api.PluginAdapter;
|
||||||
|
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
|
||||||
|
import org.mybatis.generator.api.dom.java.Interface;
|
||||||
|
import org.mybatis.generator.api.dom.java.Method;
|
||||||
|
import org.mybatis.generator.api.dom.java.TopLevelClass;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lombok插件:为PO类添加@Data注解,禁用getter/setter方法生成
|
||||||
|
*/
|
||||||
|
public class LombokPlugin extends PluginAdapter {
|
||||||
|
|
||||||
|
private static final FullyQualifiedJavaType DATA_ANNOTATION =
|
||||||
|
new FullyQualifiedJavaType("lombok.Data");
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean validate(List<String> warnings) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 为模型类添加@Data注解
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
|
||||||
|
// 添加 @Data 注解
|
||||||
|
topLevelClass.addAnnotation("@Data");
|
||||||
|
topLevelClass.addImportedType(DATA_ANNOTATION);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 禁用 Getter 方法生成
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean modelGetterMethodGenerated(Method method, TopLevelClass topLevelClass,
|
||||||
|
IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable,
|
||||||
|
ModelClassType modelClassType) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 禁用 Setter 方法生成
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean modelSetterMethodGenerated(Method method, TopLevelClass topLevelClass,
|
||||||
|
IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable,
|
||||||
|
ModelClassType modelClassType) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
package com.example.workutils.biz.mybatis.generator;
|
||||||
|
|
||||||
|
import org.mybatis.generator.api.IntrospectedTable;
|
||||||
|
import org.mybatis.generator.api.PluginAdapter;
|
||||||
|
import org.mybatis.generator.api.dom.java.Interface;
|
||||||
|
import org.mybatis.generator.api.dom.java.Method;
|
||||||
|
import org.mybatis.generator.api.dom.xml.XmlElement;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自定义插件:只保留Selective版本的方法
|
||||||
|
* 移除 insert 和 updateByPrimaryKey(全字段版本)
|
||||||
|
* 保留 insertSelective 和 updateByPrimaryKeySelective(选择性版本)
|
||||||
|
*/
|
||||||
|
public class SelectiveOnlyPlugin extends PluginAdapter {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean validate(List<String> warnings) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ==================== Java接口方法 ====================
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean clientInsertMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
|
||||||
|
// 禁用 insert(全字段插入)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean clientInsertSelectiveMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
|
||||||
|
// 保留 insertSelective
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean clientUpdateByPrimaryKeyWithoutBLOBsMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
|
||||||
|
// 禁用 updateByPrimaryKey(全字段更新)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean clientUpdateByPrimaryKeySelectiveMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
|
||||||
|
// 保留 updateByPrimaryKeySelective
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean clientDeleteByPrimaryKeyMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
|
||||||
|
// 禁用 deleteByPrimaryKey(根据主键删除)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ==================== XML元素 ====================
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean sqlMapInsertElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
|
||||||
|
// 禁用 insert XML元素(全字段插入)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean sqlMapUpdateByPrimaryKeyWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
|
||||||
|
// 禁用 updateByPrimaryKey XML元素(全字段更新)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean sqlMapDeleteByPrimaryKeyElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
|
||||||
|
// 禁用 deleteByPrimaryKey XML元素(根据主键删除)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,18 +11,14 @@
|
|||||||
<property name="searchString" value="Example$" />
|
<property name="searchString" value="Example$" />
|
||||||
<property name="replaceString" value="Criteria" />
|
<property name="replaceString" value="Criteria" />
|
||||||
</plugin>
|
</plugin>
|
||||||
|
<!-- 只保留Selective版本的方法 -->
|
||||||
|
<plugin type="com.example.workutils.biz.mybatis.generator.SelectiveOnlyPlugin" />
|
||||||
|
<!-- 使用Lombok @Data注解,不生成getter/setter -->
|
||||||
|
<plugin type="com.example.workutils.biz.mybatis.generator.LombokPlugin" />
|
||||||
|
|
||||||
<commentGenerator>
|
<commentGenerator type="com.example.workutils.biz.mybatis.generator.CustomCommentGenerator">
|
||||||
<!-- 1. 关闭所有默认的注释生成(英文说明、生成器标记) -->
|
<!-- 开启数据库字段的中文注释(保留表/字段备注) -->
|
||||||
<property name="suppressAllComments" value="false"/>
|
|
||||||
<!-- 2. 开启数据库字段的中文注释(保留表/字段备注) -->
|
|
||||||
<property name="addRemarkComments" value="true"/>
|
<property name="addRemarkComments" value="true"/>
|
||||||
<!-- 3. 去掉日期信息 -->
|
|
||||||
<property name="suppressDate" value="true"/>
|
|
||||||
<!-- 4. 【关键】禁用 Legacy 模式,让它只生成字段备注 -->
|
|
||||||
<property name="useLegacyCommentGenerator" value="false"/>
|
|
||||||
<!-- 5. 去掉 @mbg.generated 标记 -->
|
|
||||||
<property name="disableCommentAnnotation" value="true"/>
|
|
||||||
</commentGenerator>
|
</commentGenerator>
|
||||||
|
|
||||||
<!--数据库连接信息。 -->
|
<!--数据库连接信息。 -->
|
||||||
|
|||||||
Reference in New Issue
Block a user