Metrics
0
Watch 630 Star 1.9K Fork 623

abel533 / MapperJavaMIT

Merged
!17 增加lombokEqualsAndHashCodeCallSuper配置

calvinit:EqualsAndHashCode-callSuperabel533:master

calvinit Created on: 2019-02-14 14:04
question
Reviewer: 8363_free   Tester: 8363_free

增加lombokEqualsAndHashCodeCallSuper配置,当使用lombok扩展的EqualsAndHashCode注解时,可通过此配置(true)为此注解添加“callSuper = true”,这对于有继承父类的实体类,如增加支持动态表名时,有用。

1 comments, 2 participants 970685_chf_ybg 8363_free

Show action logs Hide action logs
abel533 merged Pull Request 2019-05-30 21:54
abel533 test passed 2019-05-30 21:33
calvinit assigned tester abel533 2019-02-14 14:04
calvinit assigned reviewer abel533 2019-02-14 14:04
calvinit added label question 2019-02-14 14:04
8363_free
/free 2019-05-30 21:33 owner

感谢PR。

Sign in and comment

2019-02-14

(5)
generator/src/main/java/tk/mybatis/mapper/generator/MapperPlugin.java
@@ -65,6 +65,8 @@ public class MapperPlugin extends FalseMethodPlugin {
private boolean needsAccessors = false;
//是否需要生成EqualsAndHashCode注解
private boolean needsEqualsAndHashCode = false;
//是否需要生成EqualsAndHashCode注解,并且“callSuper = true”
private boolean needsEqualsAndHashCodeAndCallSuper = false;
//是否生成字段名常量
private boolean generateColumnConsts = false;
//是否生成默认的属性的静态方法
@@ -113,40 +115,46 @@ public class MapperPlugin extends FalseMethodPlugin {
* @param introspectedTable
*/
private void processEntityClass(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
//引入JPA注解
// 引入JPA注解
topLevelClass.addImportedType("javax.persistence.*");
//lombok扩展开始
//如果需要Data,引入包,代码增加注解
// lombok扩展开始
// 如果需要Data,引入包,代码增加注解
if (this.needsData) {
topLevelClass.addImportedType("lombok.Data");
topLevelClass.addAnnotation("@Data");
}
//如果需要Getter,引入包,代码增加注解
// 如果需要Getter,引入包,代码增加注解
if (this.needsGetter) {
topLevelClass.addImportedType("lombok.Getter");
topLevelClass.addAnnotation("@Getter");
}
//如果需要Setter,引入包,代码增加注解
// 如果需要Setter,引入包,代码增加注解
if (this.needsSetter) {
topLevelClass.addImportedType("lombok.Setter");
topLevelClass.addAnnotation("@Setter");
}
//如果需要ToString,引入包,代码增加注解
// 如果需要ToString,引入包,代码增加注解
if (this.needsToString) {
topLevelClass.addImportedType("lombok.ToString");
topLevelClass.addAnnotation("@ToString");
}
//如果需要Getter,引入包,代码增加注解
// 如果需要EqualsAndHashCode,并且“callSuper = true”,引入包,代码增加注解
if (this.needsEqualsAndHashCodeAndCallSuper) {
topLevelClass.addImportedType("lombok.EqualsAndHashCode");
topLevelClass.addAnnotation("@EqualsAndHashCode(callSuper = true)");
} else {
// 如果需要EqualsAndHashCode,引入包,代码增加注解
if (this.needsEqualsAndHashCode) {
topLevelClass.addImportedType("lombok.EqualsAndHashCode");
topLevelClass.addAnnotation("@EqualsAndHashCode");
}
}
// 如果需要Accessors,引入包,代码增加注解
if (this.needsAccessors) {
topLevelClass.addImportedType("lombok.experimental.Accessors");
topLevelClass.addAnnotation("@Accessors(chain = true)");
}
//如果需要Getter,引入包,代码增加注解
if (this.needsEqualsAndHashCode) {
topLevelClass.addImportedType("lombok.EqualsAndHashCode");
topLevelClass.addAnnotation("@EqualsAndHashCode");
}
//lombok扩展结束
// lombok扩展结束
// region swagger扩展
if (this.needsSwagger) {
//导包
@@ -161,13 +169,13 @@ public class MapperPlugin extends FalseMethodPlugin {
}
// endregion swagger扩展
String tableName = introspectedTable.getFullyQualifiedTableNameAtRuntime();
//如果包含空格,或者需要分隔符,需要完善
// 如果包含空格,或者需要分隔符,需要完善
if (StringUtility.stringContainsSpace(tableName)) {
tableName = context.getBeginningDelimiter()
+ tableName
+ context.getEndingDelimiter();
}
//是否忽略大小写,对于区分大小写的数据库,会有用
// 是否忽略大小写,对于区分大小写的数据库,会有用
if (caseSensitive && !topLevelClass.getType().getShortName().equals(tableName)) {
topLevelClass.addAnnotation("@Table(name = \"" + getDelimiterName(tableName) + "\")");
} else if (!topLevelClass.getType().getShortName().equalsIgnoreCase(tableName)) {
@@ -216,7 +224,7 @@ public class MapperPlugin extends FalseMethodPlugin {
}
if (introspectedColumn.getDefaultValue() != null) {
String defaultValue = introspectedColumn.getDefaultValue();
//去除前后'',如 '123456' -> 123456
// 去除前后'',如 '123456' -> 123456
if (defaultValue.startsWith("'") && defaultValue.endsWith("'")) {
if (defaultValue.length() == 2) {
defaultValue = "";
@@ -224,7 +232,7 @@ public class MapperPlugin extends FalseMethodPlugin {
defaultValue = defaultValue.substring(1, defaultValue.length() - 1);
}
}
//暂不支持时间类型默认值识别,不同数据库表达式不同
// 暂不支持时间类型默认值识别,不同数据库表达式不同
if ("Boolean".equals(shortName)) {
if ("0".equals(defaultValue)) {
defaultValue = "false";
@@ -232,7 +240,7 @@ public class MapperPlugin extends FalseMethodPlugin {
defaultValue = "true";
}
}
//通过 new 方法转换
// 通过 new 方法转换
defaultMethod.addBodyLine(String.format("instance.%s = new %s(\"%s\");", introspectedColumn.getJavaProperty(), shortName, defaultValue));
}
@@ -337,20 +345,23 @@ public class MapperPlugin extends FalseMethodPlugin {
this.beginningDelimiter = getProperty("beginningDelimiter", "");
this.endingDelimiter = getProperty("endingDelimiter", "");
this.schema = getProperty("schema");
//lombok扩展
// lombok扩展
String lombok = getProperty("lombok");
if (lombok != null && !"".equals(lombok)) {
this.needsData = lombok.contains("Data");
//@Data 优先级高于 @Getter @Setter @RequiredArgsConstructor @ToString @EqualsAndHashCode
// @Data 优先级高于 @Getter @Setter @RequiredArgsConstructor @ToString @EqualsAndHashCode
this.needsGetter = !this.needsData && lombok.contains("Getter");
this.needsSetter = !this.needsData && lombok.contains("Setter");
this.needsToString = !this.needsData && lombok.contains("ToString");
this.needsEqualsAndHashCode = !this.needsData && lombok.contains("EqualsAndHashCode");
// 配置lombok扩展EqualsAndHashCode注解是否添加“callSuper = true”
String lombokEqualsAndHashCodeCallSuper = getProperty("lombokEqualsAndHashCodeCallSuper", "false");
this.needsEqualsAndHashCodeAndCallSuper = this.needsEqualsAndHashCode && "TRUE".equalsIgnoreCase(lombokEqualsAndHashCodeCallSuper);
this.needsAccessors = lombok.contains("Accessors");
}
//swagger扩展
// swagger扩展
String swagger = getProperty("swagger", "false");
if ("true".equalsIgnoreCase(swagger)) {
if ("TRUE".equalsIgnoreCase(swagger)) {
this.needsSwagger = true;
}
if (useMapperCommentGenerator) {
generator/src/test/resources/generatorConfig.xml
@@ -41,8 +41,10 @@
<property name="generateDefaultInstanceMethod" value="true"/>
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<!--使用 Data 时,Getter,Setter 无效-->
<property name="lombok" value="Getter,Setter,Data,ToString,Accessors,EqualsAndHashCode"/>
<!--使用Data注解时,Getter,Setter,ToString,EqualsAndHashCode注解无效-->
<!--<property name="lombok" value="Data,Accessors"/>-->
<property name="lombok" value="Getter,Setter,ToString,Accessors,EqualsAndHashCode"/>
<property name="lombokEqualsAndHashCodeCallSuper" value="true"/>
<property name="swagger" value="true"/>
</plugin>

Help Search

Gitee_sixth 5th_float_left_close