Skip to content

Commit

Permalink
Merge pull request #901 from erengong/master
Browse files Browse the repository at this point in the history
WeekendSqlsUtils新增select方法;generator新增配置,强制不生成注解
  • Loading branch information
abel533 authored Apr 6, 2024
2 parents a883d0e + 2d7a33d commit 0ca737b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public class MapperCommentGenerator implements CommentGenerator {
private String endingDelimiter = "";
//强制生成注解
private boolean forceAnnotation;
//强制不生成注解
private boolean forceNonAnnotation;
//是否生成swagger注解
private boolean needsSwagger;
//逻辑删除字段
Expand Down Expand Up @@ -93,6 +95,10 @@ public void addConfigurationProperties(Properties properties) {
if (StringUtility.stringHasValue(forceAnnotation)) {
this.forceAnnotation = "TRUE".equalsIgnoreCase(forceAnnotation);
}
String forceNonAnnotation = properties.getProperty("forceNonAnnotation");
if (StringUtility.stringHasValue(forceNonAnnotation)) {
this.forceNonAnnotation = "TRUE".equalsIgnoreCase(forceNonAnnotation);
}
String needsSwagger = properties.getProperty("needsSwagger");
if (StringUtility.stringHasValue(needsSwagger)) {
this.needsSwagger = "TRUE".equalsIgnoreCase(needsSwagger);
Expand Down Expand Up @@ -175,13 +181,15 @@ public void addFieldComment(Field field, IntrospectedTable introspectedTable, In
+ column
+ introspectedColumn.getContext().getEndingDelimiter();
}
if (!column.equals(introspectedColumn.getJavaProperty())) {
//@Column
field.addAnnotation("@Column(name = \"" + getDelimiterName(column) + "\")");
} else if (StringUtility.stringHasValue(beginningDelimiter) || StringUtility.stringHasValue(endingDelimiter)) {
field.addAnnotation("@Column(name = \"" + getDelimiterName(column) + "\")");
} else if (forceAnnotation) {
field.addAnnotation("@Column(name = \"" + getDelimiterName(column) + "\")");
if (!forceNonAnnotation) {
if (!column.equals(introspectedColumn.getJavaProperty())) {
//@Column
field.addAnnotation("@Column(name = \"" + getDelimiterName(column) + "\")");
} else if (StringUtility.stringHasValue(beginningDelimiter) || StringUtility.stringHasValue(endingDelimiter)) {
field.addAnnotation("@Column(name = \"" + getDelimiterName(column) + "\")");
} else if (forceAnnotation) {
field.addAnnotation("@Column(name = \"" + getDelimiterName(column) + "\")");
}
}

// 添加逻辑删除注解
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public class MapperPlugin extends FalseMethodPlugin {
private CommentGeneratorConfiguration commentCfg;
//强制生成注解
private boolean forceAnnotation;
//强制不生成注解
private boolean forceNonAnnotation;

//是否需要生成Data注解
private boolean needsData = false;
Expand Down Expand Up @@ -222,16 +224,18 @@ private void processEntityClass(TopLevelClass topLevelClass, IntrospectedTable i
+ context.getEndingDelimiter();
}
//是否忽略大小写,对于区分大小写的数据库,会有用
if (caseSensitive && !topLevelClass.getType().getShortName().equals(tableName)) {
topLevelClass.addAnnotation("@Table(name = \"" + getDelimiterName(tableName) + "\")");
} else if (!topLevelClass.getType().getShortName().equalsIgnoreCase(tableName)) {
topLevelClass.addAnnotation("@Table(name = \"" + getDelimiterName(tableName) + "\")");
} else if (StringUtility.stringHasValue(schema)
|| StringUtility.stringHasValue(beginningDelimiter)
|| StringUtility.stringHasValue(endingDelimiter)) {
topLevelClass.addAnnotation("@Table(name = \"" + getDelimiterName(tableName) + "\")");
} else if (forceAnnotation) {
topLevelClass.addAnnotation("@Table(name = \"" + getDelimiterName(tableName) + "\")");
if (!forceNonAnnotation) {
if (caseSensitive && !topLevelClass.getType().getShortName().equals(tableName)) {
topLevelClass.addAnnotation("@Table(name = \"" + getDelimiterName(tableName) + "\")");
} else if (!topLevelClass.getType().getShortName().equalsIgnoreCase(tableName)) {
topLevelClass.addAnnotation("@Table(name = \"" + getDelimiterName(tableName) + "\")");
} else if (StringUtility.stringHasValue(schema)
|| StringUtility.stringHasValue(beginningDelimiter)
|| StringUtility.stringHasValue(endingDelimiter)) {
topLevelClass.addAnnotation("@Table(name = \"" + getDelimiterName(tableName) + "\")");
} else if (forceAnnotation) {
topLevelClass.addAnnotation("@Table(name = \"" + getDelimiterName(tableName) + "\")");
}
}
if (generateColumnConsts) {
for (IntrospectedColumn introspectedColumn : introspectedTable.getAllColumns()) {
Expand Down Expand Up @@ -428,6 +432,7 @@ public void setProperties(Properties properties) {
}
this.caseSensitive = Boolean.parseBoolean(this.properties.getProperty("caseSensitive"));
this.forceAnnotation = getPropertyAsBoolean("forceAnnotation");
this.forceNonAnnotation = getPropertyAsBoolean("forceNonAnnotation");
this.beginningDelimiter = getProperty("beginningDelimiter", "");
this.endingDelimiter = getProperty("endingDelimiter", "");
this.schema = getProperty("schema");
Expand Down Expand Up @@ -461,6 +466,10 @@ public void setProperties(Properties properties) {
if (StringUtility.stringHasValue(forceAnnotation)) {
commentCfg.addProperty("forceAnnotation", forceAnnotation);
}
String forceNonAnnotation = getProperty("forceNonAnnotation");
if (StringUtility.stringHasValue(forceNonAnnotation)) {
commentCfg.addProperty("forceNonAnnotation", forceNonAnnotation);
}
commentCfg.addProperty("needsSwagger", this.needsSwagger + "");
}
this.generateColumnConsts = getPropertyAsBoolean("generateColumnConsts");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

package tk.mybatis.mapper.weekend;

import tk.mybatis.mapper.weekend.reflection.Reflections;

/**
* {@link WeekendSqls} 的工具类,提供一系列静态方法,减少泛型参数的指定,使代码更简洁、清晰
*
Expand Down Expand Up @@ -276,4 +278,7 @@ public static <T> WeekendSqls<T> orNotLike(Fn<T, Object> fn, String value) {
return WeekendSqls.<T>custom().orNotLike(fn, value);
}

public static <T> String[] select(Fn<T, Object>... fn) {
return Reflections.fnToFieldNames(fn);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import java.beans.Introspector;
import java.lang.invoke.SerializedLambda;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
Expand Down Expand Up @@ -58,4 +60,12 @@ public static String fnToFieldName(Fn fn) {
throw new ReflectionOperationException(e);
}
}

public static String[] fnToFieldNames(Fn... fns) {
List<String> list = new ArrayList<>();
for (Fn fn : fns) {
list.add(fnToFieldName(fn));
}
return list.toArray(new String[0]);
}
}

0 comments on commit 0ca737b

Please sign in to comment.