mirror of
https://gitee.com/y_project/RuoYi-Cloud.git
synced 2026-01-27 12:11:56 +08:00
优化代码生成
This commit is contained in:
@@ -0,0 +1,459 @@
|
||||
package com.ruoyi.common.datasource.mybatis.gen;
|
||||
|
||||
import org.mybatis.generator.api.CommentGenerator;
|
||||
import org.mybatis.generator.api.IntrospectedColumn;
|
||||
import org.mybatis.generator.api.IntrospectedTable;
|
||||
import org.mybatis.generator.api.MyBatisGenerator;
|
||||
import org.mybatis.generator.api.dom.java.*;
|
||||
import org.mybatis.generator.api.dom.kotlin.KotlinFile;
|
||||
import org.mybatis.generator.api.dom.xml.TextElement;
|
||||
import org.mybatis.generator.api.dom.xml.XmlElement;
|
||||
import org.mybatis.generator.config.MergeConstants;
|
||||
import org.mybatis.generator.config.PropertyRegistry;
|
||||
import org.mybatis.generator.internal.util.StringUtility;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Date;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.mybatis.generator.internal.util.StringUtility.isTrue;
|
||||
|
||||
/**
|
||||
* 抄录自{@link org.mybatis.generator.internal.DefaultCommentGenerator},并作个性化修改
|
||||
*
|
||||
* @author Alan Scipio
|
||||
* created on 2024/2/18
|
||||
*/
|
||||
public class MyCommentGenerator implements CommentGenerator {
|
||||
|
||||
private final Properties properties = new Properties();
|
||||
|
||||
private boolean suppressDate;
|
||||
|
||||
private boolean suppressAllComments;
|
||||
|
||||
/** If suppressAllComments is true, this option is ignored. */
|
||||
private boolean addRemarkComments;
|
||||
|
||||
private SimpleDateFormat dateFormat;
|
||||
|
||||
private FullyQualifiedJavaType generatedImport =
|
||||
new FullyQualifiedJavaType("jakarta.annotation.Generated");
|
||||
|
||||
public MyCommentGenerator() {
|
||||
super();
|
||||
suppressDate = false;
|
||||
suppressAllComments = false;
|
||||
addRemarkComments = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a suitable comment to warn users that the element was generated, and
|
||||
* when it was generated.
|
||||
*
|
||||
* @param xmlElement the xml element
|
||||
*/
|
||||
@Override
|
||||
public void addComment(XmlElement xmlElement) {
|
||||
if (suppressAllComments) {
|
||||
return;
|
||||
}
|
||||
|
||||
xmlElement.addElement(new TextElement("<!--"));
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(" WARNING - ");
|
||||
sb.append(MergeConstants.NEW_ELEMENT_TAG);
|
||||
xmlElement.addElement(new TextElement(sb.toString()));
|
||||
xmlElement.addElement(
|
||||
new TextElement(" This element is automatically generated by MyBatis Generator,"
|
||||
+ " do not modify."));
|
||||
|
||||
String s = getDateString();
|
||||
if (s != null) {
|
||||
sb.setLength(0);
|
||||
sb.append(" This element was generated on ");
|
||||
sb.append(s);
|
||||
sb.append('.');
|
||||
xmlElement.addElement(new TextElement(sb.toString()));
|
||||
}
|
||||
|
||||
xmlElement.addElement(new TextElement("-->"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addConfigurationProperties(Properties props) {
|
||||
this.properties.putAll(props);
|
||||
|
||||
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));
|
||||
|
||||
if (isTrue(properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_USE_LEGACY_GENERATED_ANNOTATION))) {
|
||||
generatedImport = new FullyQualifiedJavaType("javax.annotation.Generated");
|
||||
}
|
||||
|
||||
String dateFormatString = properties.getProperty(PropertyRegistry.COMMENT_GENERATOR_DATE_FORMAT);
|
||||
if (StringUtility.stringHasValue(dateFormatString)) {
|
||||
dateFormat = new SimpleDateFormat(dateFormatString);
|
||||
} else {
|
||||
dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method adds the custom javadoc tag for. You may do nothing if you do not
|
||||
* wish to include the Javadoc tag - however, if you do not include the Javadoc
|
||||
* tag then the Java merge capability of the eclipse plugin will break.
|
||||
*
|
||||
* @param javaElement the java element
|
||||
* @param markAsDoNotDelete the mark as do not delete
|
||||
*/
|
||||
protected void addJavadocTag(JavaElement javaElement, boolean markAsDoNotDelete) {
|
||||
javaElement.addJavaDocLine(" *");
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(" * ");
|
||||
sb.append(MergeConstants.NEW_ELEMENT_TAG);
|
||||
if (markAsDoNotDelete) {
|
||||
sb.append(" do_not_delete_during_merge");
|
||||
}
|
||||
String s = getDateString();
|
||||
if (s != null) {
|
||||
sb.append(' ');
|
||||
sb.append(s);
|
||||
}
|
||||
javaElement.addJavaDocLine(sb.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a formatted date string to include in the Javadoc tag and XML
|
||||
* comments. You may return null if you do not want the date in these
|
||||
* documentation elements.
|
||||
*
|
||||
* @return a string representing the current timestamp, or null
|
||||
*/
|
||||
protected String getDateString() {
|
||||
if (suppressDate) {
|
||||
return null;
|
||||
} else if (dateFormat != null) {
|
||||
return dateFormat.format(new Date());
|
||||
} else {
|
||||
return new Date().toString();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {
|
||||
if (suppressAllComments) {
|
||||
return;
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
innerClass.addJavaDocLine("/**");
|
||||
sb.append(introspectedTable.getFullyQualifiedTable());
|
||||
innerClass.addJavaDocLine(sb.toString());
|
||||
|
||||
addJavadocTag(innerClass, false);
|
||||
|
||||
innerClass.addJavaDocLine(" */");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) {
|
||||
if (suppressAllComments) {
|
||||
return;
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
innerClass.addJavaDocLine("/**");
|
||||
sb.append(introspectedTable.getFullyQualifiedTable());
|
||||
innerClass.addJavaDocLine(sb.toString());
|
||||
|
||||
addJavadocTag(innerClass, markAsDoNotDelete);
|
||||
|
||||
innerClass.addJavaDocLine(" */");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
|
||||
if (suppressAllComments || !addRemarkComments) {
|
||||
return;
|
||||
}
|
||||
|
||||
topLevelClass.addJavaDocLine("/**");
|
||||
topLevelClass.addJavaDocLine(" * This class was generated by MyBatis Generator.");
|
||||
topLevelClass.addJavaDocLine(" *");
|
||||
|
||||
|
||||
|
||||
topLevelClass.addJavaDocLine(" * <ul>");
|
||||
//表名
|
||||
String s = " * <li> Table: " + introspectedTable.getFullyQualifiedTable() + " </li>";
|
||||
topLevelClass.addJavaDocLine(s);
|
||||
|
||||
//表注释
|
||||
String remarks = introspectedTable.getRemarks() == null ? "" : introspectedTable.getRemarks();
|
||||
StringBuilder remarksSb = new StringBuilder();
|
||||
if (addRemarkComments && StringUtility.stringHasValue(remarks)) {
|
||||
remarksSb.append(" * <li> Remarks: ");
|
||||
remarksSb.append(remarks);
|
||||
remarksSb.append(" </li>");
|
||||
topLevelClass.addJavaDocLine(remarksSb.toString());
|
||||
}
|
||||
topLevelClass.addJavaDocLine(" * </ul>");
|
||||
topLevelClass.addJavaDocLine(" *");
|
||||
|
||||
//作者
|
||||
topLevelClass.addJavaDocLine(" * @author ryas");
|
||||
if (!suppressDate && !suppressAllComments) {
|
||||
topLevelClass.addJavaDocLine(" * created on " + getDateString());
|
||||
}
|
||||
|
||||
topLevelClass.addJavaDocLine(" */");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addEnumComment(InnerEnum innerEnum, IntrospectedTable introspectedTable) {
|
||||
if (suppressAllComments) {
|
||||
return;
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
innerEnum.addJavaDocLine("/**");
|
||||
sb.append(introspectedTable.getFullyQualifiedTable());
|
||||
innerEnum.addJavaDocLine(sb.toString());
|
||||
|
||||
addJavadocTag(innerEnum, false);
|
||||
|
||||
innerEnum.addJavaDocLine(" */");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addFieldComment(Field field, IntrospectedTable introspectedTable,
|
||||
IntrospectedColumn introspectedColumn) {
|
||||
if (suppressAllComments) {
|
||||
return;
|
||||
}
|
||||
|
||||
field.addJavaDocLine("/**");
|
||||
|
||||
String remarks = introspectedTable.getRemarks() == null ? "" : introspectedTable.getRemarks();
|
||||
if (addRemarkComments && StringUtility.stringHasValue(remarks)) {
|
||||
String[] remarkLines = remarks.split(System.lineSeparator());
|
||||
for (String remarkLine : remarkLines) {
|
||||
field.addJavaDocLine(" * " + remarkLine);
|
||||
}
|
||||
}
|
||||
|
||||
String s = ""
|
||||
+ introspectedTable.getFullyQualifiedTable()
|
||||
+ '.'
|
||||
+ introspectedColumn.getActualColumnName();
|
||||
field.addJavaDocLine(s);
|
||||
|
||||
addJavadocTag(field, false);
|
||||
|
||||
field.addJavaDocLine(" */");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addFieldComment(Field field, IntrospectedTable introspectedTable) {
|
||||
if (suppressAllComments) {
|
||||
return;
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
field.addJavaDocLine("/**");
|
||||
sb.append(introspectedTable.getFullyQualifiedTable());
|
||||
field.addJavaDocLine(sb.toString());
|
||||
|
||||
addJavadocTag(field, false);
|
||||
|
||||
field.addJavaDocLine(" */");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {
|
||||
if (suppressAllComments) {
|
||||
return;
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
method.addJavaDocLine("/**");
|
||||
sb.append(introspectedTable.getFullyQualifiedTable());
|
||||
method.addJavaDocLine(sb.toString());
|
||||
|
||||
addJavadocTag(method, false);
|
||||
|
||||
method.addJavaDocLine(" */");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addGetterComment(Method method, IntrospectedTable introspectedTable,
|
||||
IntrospectedColumn introspectedColumn) {
|
||||
if (suppressAllComments) {
|
||||
return;
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
method.addJavaDocLine("/**");
|
||||
sb.append(introspectedTable.getFullyQualifiedTable());
|
||||
sb.append('.');
|
||||
sb.append(introspectedColumn.getActualColumnName());
|
||||
method.addJavaDocLine(sb.toString());
|
||||
|
||||
sb.setLength(0);
|
||||
sb.append(" * @return the value of ");
|
||||
sb.append(introspectedTable.getFullyQualifiedTable());
|
||||
sb.append('.');
|
||||
sb.append(introspectedColumn.getActualColumnName());
|
||||
method.addJavaDocLine(sb.toString());
|
||||
|
||||
addJavadocTag(method, false);
|
||||
|
||||
method.addJavaDocLine(" */");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addSetterComment(Method method, IntrospectedTable introspectedTable,
|
||||
IntrospectedColumn introspectedColumn) {
|
||||
if (suppressAllComments) {
|
||||
return;
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
method.addJavaDocLine("/**");
|
||||
sb.append(introspectedTable.getFullyQualifiedTable());
|
||||
sb.append('.');
|
||||
sb.append(introspectedColumn.getActualColumnName());
|
||||
method.addJavaDocLine(sb.toString());
|
||||
|
||||
method.addJavaDocLine(" *");
|
||||
|
||||
Parameter parm = method.getParameters().getFirst();
|
||||
sb.setLength(0);
|
||||
sb.append(" * @param ");
|
||||
sb.append(parm.getName());
|
||||
sb.append(" the value for ");
|
||||
sb.append(introspectedTable.getFullyQualifiedTable());
|
||||
sb.append('.');
|
||||
sb.append(introspectedColumn.getActualColumnName());
|
||||
method.addJavaDocLine(sb.toString());
|
||||
|
||||
addJavadocTag(method, false);
|
||||
|
||||
method.addJavaDocLine(" */");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable,
|
||||
Set<FullyQualifiedJavaType> imports) {
|
||||
// imports.add(generatedImport);
|
||||
// String comment = "Source Table: " + introspectedTable.getFullyQualifiedTable().toString();
|
||||
// method.addAnnotation(getGeneratedAnnotation(comment));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addGeneralMethodAnnotation(Method method, IntrospectedTable introspectedTable,
|
||||
IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> imports) {
|
||||
// imports.add(generatedImport);
|
||||
// String comment = "Source field: "
|
||||
// + introspectedTable.getFullyQualifiedTable().toString() + "."
|
||||
// + introspectedColumn.getActualColumnName();
|
||||
// method.addAnnotation(getGeneratedAnnotation(comment));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable,
|
||||
Set<FullyQualifiedJavaType> imports) {
|
||||
// imports.add(generatedImport);
|
||||
// String comment = "Source Table: " + introspectedTable.getFullyQualifiedTable().toString();
|
||||
// field.addAnnotation(getGeneratedAnnotation(comment));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addFieldAnnotation(Field field, IntrospectedTable introspectedTable,
|
||||
IntrospectedColumn introspectedColumn, Set<FullyQualifiedJavaType> imports) {
|
||||
// imports.add(generatedImport);
|
||||
// String comment = "Source field: "
|
||||
// + introspectedTable.getFullyQualifiedTable().toString() + "."
|
||||
// + introspectedColumn.getActualColumnName();
|
||||
// field.addAnnotation(getGeneratedAnnotation(comment));
|
||||
|
||||
if (!suppressAllComments && addRemarkComments) {
|
||||
String remarks = introspectedColumn.getRemarks();
|
||||
if (addRemarkComments && StringUtility.stringHasValue(remarks)) {
|
||||
field.addJavaDocLine("/**");
|
||||
String[] remarkLines = remarks.split(System.lineSeparator());
|
||||
for (String remarkLine : remarkLines) {
|
||||
field.addJavaDocLine(" * " + remarkLine);
|
||||
}
|
||||
field.addJavaDocLine(" */");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addClassAnnotation(InnerClass innerClass, IntrospectedTable introspectedTable,
|
||||
Set<FullyQualifiedJavaType> imports) {
|
||||
// imports.add(generatedImport);
|
||||
// String comment = "Source Table: " + introspectedTable.getFullyQualifiedTable().toString();
|
||||
// innerClass.addAnnotation(getGeneratedAnnotation(comment));
|
||||
}
|
||||
|
||||
private String getGeneratedAnnotation(String comment) {
|
||||
StringBuilder buffer = new StringBuilder();
|
||||
buffer.append("@Generated(");
|
||||
if (suppressAllComments) {
|
||||
buffer.append('\"');
|
||||
} else {
|
||||
buffer.append("value=\"");
|
||||
}
|
||||
|
||||
buffer.append(MyBatisGenerator.class.getName());
|
||||
buffer.append('\"');
|
||||
|
||||
if (!suppressDate && !suppressAllComments) {
|
||||
buffer.append(", date=\"");
|
||||
buffer.append(DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(ZonedDateTime.now()));
|
||||
buffer.append('\"');
|
||||
}
|
||||
|
||||
if (!suppressAllComments) {
|
||||
buffer.append(", comments=\"");
|
||||
buffer.append(comment);
|
||||
buffer.append('\"');
|
||||
}
|
||||
|
||||
buffer.append(')');
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addFileComment(KotlinFile kotlinFile) {
|
||||
if (suppressAllComments) {
|
||||
return;
|
||||
}
|
||||
|
||||
kotlinFile.addFileCommentLine("/*");
|
||||
kotlinFile.addFileCommentLine(" * Auto-generated file. Created by MyBatis Generator");
|
||||
if (!suppressDate) {
|
||||
kotlinFile.addFileCommentLine(" * Generation date: "
|
||||
+ DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(ZonedDateTime.now()));
|
||||
}
|
||||
kotlinFile.addFileCommentLine(" */");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,6 +5,7 @@ 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 org.mybatis.generator.internal.util.StringUtility;
|
||||
|
||||
import java.util.List;
|
||||
@@ -15,6 +16,8 @@ import java.util.List;
|
||||
*/
|
||||
public class RyasMyBatisDynamicPlugin extends PluginAdapter {
|
||||
|
||||
private String modelClassName;
|
||||
|
||||
// 校验插件配置的正确性
|
||||
@Override
|
||||
public boolean validate(List<String> warnings) {
|
||||
@@ -26,6 +29,13 @@ public class RyasMyBatisDynamicPlugin extends PluginAdapter {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
|
||||
// 获取模型类的完整名称
|
||||
modelClassName = topLevelClass.getType().getShortName();
|
||||
return true;
|
||||
}
|
||||
|
||||
// int insert(UnitInfo row)
|
||||
@Override
|
||||
public boolean clientInsertMethodGenerated(Method method, Interface interfaze, IntrospectedTable introspectedTable) {
|
||||
@@ -82,7 +92,7 @@ public class RyasMyBatisDynamicPlugin extends PluginAdapter {
|
||||
//从头插入,所以需要倒着
|
||||
bodyLines.addFirst("}");
|
||||
bodyLines.addFirst("row.setCommonForInsert(SecurityUtilsExt.getUserIdStr());");
|
||||
bodyLines.addFirst("for (UnitInfo row : records) {");
|
||||
bodyLines.addFirst("for (" + modelClassName + " row : records) {");
|
||||
}
|
||||
|
||||
private void setForUpdate(Method method, Interface interfaze) {
|
||||
|
||||
Reference in New Issue
Block a user