Skip to content

Commit 1bb4daf

Browse files
committed
区分注解属性.
1 parent 6d759d9 commit 1bb4daf

File tree

4 files changed

+164
-47
lines changed

4 files changed

+164
-47
lines changed

mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/builder/Entity.java

+13-15
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
3333
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
3434
import com.baomidou.mybatisplus.generator.function.ConverterFileName;
35-
import com.baomidou.mybatisplus.generator.model.AnnotationAttributes;
35+
import com.baomidou.mybatisplus.generator.model.ClassAnnotationAttributes;
3636
import com.baomidou.mybatisplus.generator.util.ClassUtils;
3737
import lombok.Getter;
3838
import org.jetbrains.annotations.NotNull;
@@ -240,7 +240,7 @@ private Entity() {
240240
* @since 3.5.10
241241
*/
242242
@Getter
243-
private final List<AnnotationAttributes> classAnnotations = new ArrayList<>();
243+
private final List<ClassAnnotationAttributes> classAnnotations = new ArrayList<>();
244244

245245
/**
246246
* <p>
@@ -371,12 +371,12 @@ public Map<String, Object> renderData(@NotNull TableInfo tableInfo) {
371371
if (!kotlin) {
372372
// 原先kt模板没有处理这些,作为兼容项
373373
if (chain) {
374-
this.classAnnotations.add(new AnnotationAttributes("@Accessors(chain = true)", "lombok.experimental.Accessors"));
374+
this.classAnnotations.add(new ClassAnnotationAttributes("@Accessors(chain = true)", "lombok.experimental.Accessors"));
375375
}
376376
if (lombok && defaultLombok) {
377377
// 原先lombok默认只有这两个
378-
this.classAnnotations.add(new AnnotationAttributes("@Getter", "lombok.Getter"));
379-
this.classAnnotations.add(new AnnotationAttributes("@Setter", "lombok.Setter"));
378+
this.classAnnotations.add(new ClassAnnotationAttributes("@Getter", "lombok.Getter"));
379+
this.classAnnotations.add(new ClassAnnotationAttributes("@Setter", "lombok.Setter"));
380380
}
381381
}
382382
if (tableInfo.isConvert()) {
@@ -388,23 +388,21 @@ public Map<String, Object> renderData(@NotNull TableInfo tableInfo) {
388388
}
389389
//@TableName("${schemaName}${table.name}")
390390
String displayName = String.format("@TableName(\"%s%s\")", schemaName, tableInfo.getName());
391-
this.classAnnotations.add(new AnnotationAttributes(TableName.class, displayName));
391+
this.classAnnotations.add(new ClassAnnotationAttributes(TableName.class, displayName));
392392
}
393393
if (globalConfig.isSwagger()) {
394394
//@ApiModel(value = "${entity}对象", description = "${table.comment!}")
395395
String displayName = String.format("@ApiModel(value = \"%s对象\", description = \"%s\")", tableInfo.getEntityName(), comment);
396-
this.classAnnotations.add(new AnnotationAttributes("@ApiModel",
396+
this.classAnnotations.add(new ClassAnnotationAttributes(
397397
displayName, "io.swagger.annotations.ApiModel", "io.swagger.annotations.ApiModelProperty"));
398398
}
399399
if (globalConfig.isSpringdoc()) {
400400
//@Schema(name = "${entity}", description = "${table.comment!}")
401401
String displayName = String.format("@Schema(name = \"%s\", description = \"%s\")", tableInfo.getEntityName(), comment);
402-
this.classAnnotations.add(new AnnotationAttributes(displayName, "io.swagger.v3.oas.annotations.media.Schema"));
402+
this.classAnnotations.add(new ClassAnnotationAttributes(displayName, "io.swagger.v3.oas.annotations.media.Schema"));
403403
}
404404
this.classAnnotations.forEach(attributes -> {
405-
if (attributes.getDisplayNameFunction() != null) {
406-
attributes.setDisplayName(attributes.getDisplayNameFunction().apply(tableInfo));
407-
}
405+
attributes.handleDisplayName(tableInfo);
408406
importPackages.addAll(attributes.getImportPackages());
409407
});
410408
data.put("entityClassAnnotations", this.classAnnotations.stream()
@@ -499,16 +497,16 @@ public Builder enableLombok() {
499497
}
500498

501499
/**
502-
* 开启lombok模型 (这里会把注解属性都加入进去,无论是否启用kotlin)
500+
* 开启lombok模型 (这里会把注解属性都加入进去,无论是否启用{@link GlobalConfig#isKotlin()})
503501
*
504502
* @param attributes 注解属性集合
505503
* @return this
506504
* @since 3.5.10
507505
*/
508-
public Builder enableLombok(@NotNull AnnotationAttributes... attributes) {
506+
public Builder enableLombok(@NotNull ClassAnnotationAttributes... attributes) {
509507
this.entity.lombok = true;
510508
this.entity.defaultLombok = false;
511-
for (AnnotationAttributes attribute : attributes) {
509+
for (ClassAnnotationAttributes attribute : attributes) {
512510
this.addClassAnnotation(attribute);
513511
}
514512
return this;
@@ -767,7 +765,7 @@ public Builder disable() {
767765
* @return this
768766
* @since 3.5.10
769767
*/
770-
public Builder addClassAnnotation(@NotNull AnnotationAttributes attributes) {
768+
public Builder addClassAnnotation(@NotNull ClassAnnotationAttributes attributes) {
771769
this.entity.classAnnotations.add(attributes);
772770
return this;
773771
}

mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/model/AnnotationAttributes.java

+5-32
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package com.baomidou.mybatisplus.generator.model;
1717

18-
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
1918
import lombok.Getter;
2019
import lombok.Setter;
2120
import lombok.ToString;
@@ -24,22 +23,17 @@
2423
import java.util.Arrays;
2524
import java.util.HashSet;
2625
import java.util.Set;
27-
import java.util.function.Function;
2826

2927
/**
28+
* 注解属性
29+
*
3030
* @author nieqiurong
3131
* @since 3.5.10
3232
*/
3333
@Getter
3434
@ToString
3535
public class AnnotationAttributes {
3636

37-
/**
38-
* 显示名称处理函数(最终需要转换成{@link #displayName})
39-
*/
40-
@Setter
41-
private Function<TableInfo, String> displayNameFunction;
42-
4337
/**
4438
* 显示名称
4539
*/
@@ -51,16 +45,13 @@ public class AnnotationAttributes {
5145
*/
5246
private final Set<String> importPackages = new HashSet<>();
5347

54-
public AnnotationAttributes(@NotNull Class<?> annotationClass) {
55-
this.displayName = "@" + annotationClass.getSimpleName();
56-
this.importPackages.add(annotationClass.getName());
48+
public AnnotationAttributes() {
5749
}
5850

59-
public AnnotationAttributes(@NotNull Class<?> annotationClass, @NotNull Function<TableInfo,String> displayNameFunction) {
60-
this.displayNameFunction = displayNameFunction;
51+
public AnnotationAttributes(@NotNull Class<?> annotationClass) {
52+
this.displayName = "@" + annotationClass.getSimpleName();
6153
this.importPackages.add(annotationClass.getName());
6254
}
63-
6455
public AnnotationAttributes(@NotNull Class<?> annotationClass, @NotNull String displayName, String... extraPkg) {
6556
this.displayName = displayName;
6657
this.importPackages.add(annotationClass.getName());
@@ -69,29 +60,11 @@ public AnnotationAttributes(@NotNull Class<?> annotationClass, @NotNull String d
6960
}
7061
}
7162

72-
public AnnotationAttributes(@NotNull Class<?> annotationClass, @NotNull Function<TableInfo,String> displayNameFunction, String... extraPkg) {
73-
this.displayNameFunction = displayNameFunction;
74-
this.importPackages.add(annotationClass.getName());
75-
if (extraPkg != null && extraPkg.length > 0) {
76-
this.importPackages.addAll(Arrays.asList(extraPkg));
77-
}
78-
}
79-
8063
public AnnotationAttributes(@NotNull String displayName, @NotNull String... importPackages) {
8164
this.displayName = displayName;
8265
this.importPackages.addAll(Arrays.asList(importPackages));
8366
}
8467

85-
public AnnotationAttributes(@NotNull String importPackage, @NotNull Function<TableInfo, String> displayNameFunction) {
86-
this.displayNameFunction = displayNameFunction;
87-
this.importPackages.add(importPackage);
88-
}
89-
90-
public AnnotationAttributes(@NotNull Set<String> importPackages, @NotNull Function<TableInfo, String> displayNameFunction) {
91-
this.displayNameFunction = displayNameFunction;
92-
this.importPackages.addAll(importPackages);
93-
}
94-
9568
/**
9669
* 添加导包
9770
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright (c) 2011-2024, baomidou ([email protected]).
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.baomidou.mybatisplus.generator.model;
17+
18+
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
19+
import lombok.Getter;
20+
import lombok.NoArgsConstructor;
21+
import lombok.Setter;
22+
import lombok.ToString;
23+
import org.jetbrains.annotations.NotNull;
24+
25+
import java.util.Arrays;
26+
import java.util.Set;
27+
import java.util.function.Function;
28+
29+
/**
30+
* 类注解属性
31+
*
32+
* @author nieqiurong
33+
* @since 3.5.10
34+
*/
35+
@Setter
36+
@Getter
37+
@ToString
38+
@NoArgsConstructor
39+
public class ClassAnnotationAttributes extends AnnotationAttributes {
40+
41+
/**
42+
* 根据{@link TableInfo}信息加工处理显示注解
43+
* <p>显示名称处理函数(最终需要转换成{@link #setDisplayName(String)})<p/>
44+
*/
45+
private Function<TableInfo, String> displayNameFunction;
46+
47+
public ClassAnnotationAttributes(@NotNull Class<?> annotationClass) {
48+
super(annotationClass);
49+
}
50+
51+
public ClassAnnotationAttributes(@NotNull Class<?> annotationClass, @NotNull Function<TableInfo, String> displayNameFunction, String... extraPkg) {
52+
super(annotationClass);
53+
this.displayNameFunction = displayNameFunction;
54+
if (extraPkg != null && extraPkg.length > 0) {
55+
super.getImportPackages().addAll(Arrays.asList(extraPkg));
56+
}
57+
}
58+
59+
public ClassAnnotationAttributes(@NotNull Class<?> annotationClass, @NotNull String displayName, String... extraPkg) {
60+
super(annotationClass, displayName, extraPkg);
61+
}
62+
63+
public ClassAnnotationAttributes(@NotNull String displayName, @NotNull String... importPackages) {
64+
super(displayName, importPackages);
65+
}
66+
67+
public ClassAnnotationAttributes(@NotNull String importPackage, @NotNull Function<TableInfo, String> displayNameFunction) {
68+
super.getImportPackages().add(importPackage);
69+
this.displayNameFunction = displayNameFunction;
70+
}
71+
72+
public ClassAnnotationAttributes(@NotNull Set<String> importPackages, @NotNull Function<TableInfo, String> displayNameFunction) {
73+
super.getImportPackages().addAll(importPackages);
74+
this.displayNameFunction = displayNameFunction;
75+
}
76+
77+
public void handleDisplayName(TableInfo tableInfo) {
78+
if (displayNameFunction != null) {
79+
super.setDisplayName(displayNameFunction.apply(tableInfo));
80+
}
81+
}
82+
83+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.baomidou.mybatisplus.generator.model;
2+
3+
import com.baomidou.mybatisplus.annotation.TableName;
4+
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
5+
import org.junit.jupiter.api.Assertions;
6+
import org.junit.jupiter.api.Test;
7+
import org.mockito.Mockito;
8+
9+
import java.util.Set;
10+
11+
public class ClassAnnotationAttributesTest {
12+
13+
@Test
14+
void test() {
15+
var classAnnotationAttributes = new ClassAnnotationAttributes(TableName.class);
16+
Assertions.assertNull(classAnnotationAttributes.getDisplayNameFunction());
17+
Assertions.assertEquals("@TableName", classAnnotationAttributes.getDisplayName());
18+
Assertions.assertTrue(classAnnotationAttributes.getImportPackages().contains(TableName.class.getName()));
19+
20+
classAnnotationAttributes = new ClassAnnotationAttributes(TableName.class,(tableInfo -> "@Test"));
21+
classAnnotationAttributes.handleDisplayName(Mockito.mock(TableInfo.class));
22+
Assertions.assertNotNull(classAnnotationAttributes.getDisplayNameFunction());
23+
Assertions.assertEquals("@Test", classAnnotationAttributes.getDisplayName());
24+
Assertions.assertTrue(classAnnotationAttributes.getImportPackages().contains(TableName.class.getName()));
25+
26+
classAnnotationAttributes = new ClassAnnotationAttributes(TableName.class,(tableInfo -> "@Test"), "com.baomidou.test");
27+
classAnnotationAttributes.handleDisplayName(Mockito.mock(TableInfo.class));
28+
Assertions.assertNotNull(classAnnotationAttributes.getDisplayNameFunction());
29+
Assertions.assertEquals("@Test", classAnnotationAttributes.getDisplayName());
30+
Assertions.assertTrue(classAnnotationAttributes.getImportPackages().contains(TableName.class.getName()));
31+
Assertions.assertTrue(classAnnotationAttributes.getImportPackages().contains("com.baomidou.test"));
32+
33+
classAnnotationAttributes = new ClassAnnotationAttributes(TableName.class, "@Test");
34+
Assertions.assertNull(classAnnotationAttributes.getDisplayNameFunction());
35+
Assertions.assertEquals("@Test", classAnnotationAttributes.getDisplayName());
36+
Assertions.assertTrue(classAnnotationAttributes.getImportPackages().contains(TableName.class.getName()));
37+
38+
classAnnotationAttributes = new ClassAnnotationAttributes(TableName.class, "@Test", "com.baomidou.test");
39+
Assertions.assertNull(classAnnotationAttributes.getDisplayNameFunction());
40+
Assertions.assertEquals("@Test", classAnnotationAttributes.getDisplayName());
41+
Assertions.assertTrue(classAnnotationAttributes.getImportPackages().contains(TableName.class.getName()));
42+
Assertions.assertTrue(classAnnotationAttributes.getImportPackages().contains("com.baomidou.test"));
43+
44+
classAnnotationAttributes = new ClassAnnotationAttributes("@Test", "com.baomidou.test");
45+
Assertions.assertNull(classAnnotationAttributes.getDisplayNameFunction());
46+
Assertions.assertEquals("@Test", classAnnotationAttributes.getDisplayName());
47+
Assertions.assertTrue(classAnnotationAttributes.getImportPackages().contains("com.baomidou.test"));
48+
49+
classAnnotationAttributes = new ClassAnnotationAttributes("com.baomidou.test", (tableInfo -> "@Test"));
50+
classAnnotationAttributes.handleDisplayName(Mockito.mock(TableInfo.class));
51+
Assertions.assertNotNull(classAnnotationAttributes.getDisplayNameFunction());
52+
Assertions.assertEquals("@Test", classAnnotationAttributes.getDisplayName());
53+
Assertions.assertTrue(classAnnotationAttributes.getImportPackages().contains("com.baomidou.test"));
54+
55+
classAnnotationAttributes = new ClassAnnotationAttributes(Set.of("com.baomidou.test1","com.baomidou.test2"), (tableInfo -> "@Test"));
56+
classAnnotationAttributes.handleDisplayName(Mockito.mock(TableInfo.class));
57+
Assertions.assertNotNull(classAnnotationAttributes.getDisplayNameFunction());
58+
Assertions.assertEquals("@Test", classAnnotationAttributes.getDisplayName());
59+
Assertions.assertTrue(classAnnotationAttributes.getImportPackages().contains("com.baomidou.test1"));
60+
Assertions.assertTrue(classAnnotationAttributes.getImportPackages().contains("com.baomidou.test2"));
61+
}
62+
63+
}

0 commit comments

Comments
 (0)