Skip to content

Commit 984614d

Browse files
author
hewei
committed
LombokPlugin 减少@SuperBuilder使用场景,只在必要条件下才生成
1 parent 8a15fec commit 984614d

File tree

5 files changed

+107
-8
lines changed

5 files changed

+107
-8
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,7 +1465,7 @@ public class Test {
14651465
使用Lombok的使用可以减少很多重复代码的书写,目前项目中已大量使用。
14661466
但Lombok的@Builder对于类的继承支持很不好,最近发现新版(>=1.18.2)已经提供了对@SuperBuilder的支持,所以新增该插件方便简写代码。
14671467

1468-
>warning1: @Builder注解在Lombok 版本 >= 1.18.2 的情况下才能开启,对于存在继承关系的model会自动替换成@SuperBuilder注解。
1468+
>warning1: @Builder注解在Lombok 版本 >= 1.18.2 的情况下才能开启,对于存在继承关系的model会自动替换成@SuperBuilder注解(目前IDEA的插件对于SuperBuilder的还不支持(作者已经安排上更新日程))
14691469
14701470
>warning2: 配合插件IncrementsPlugin(已不推荐使用,请使用新版[IncrementPlugin](#22-增量插件)解决该问题) 并且 @Builder开启的情况下,因为@SuperBuilder的一些限制,
14711471
插件模拟Lombok插件生成了一些附加代码可能在某些编译器上会提示错误,请忽略(Lombok = 1.18.2 已测试)。
@@ -1476,7 +1476,7 @@ public class Test {
14761476
<plugin type="com.itfsw.mybatis.generator.plugins.LombokPlugin">
14771477
<!-- @Data 默认开启,同时插件会对子类自动附加@EqualsAndHashCode(callSuper = true),@ToString(callSuper = true) -->
14781478
<property name="@Data" value="true"/>
1479-
<!-- @Builder 必须在 Lombok 版本 >= 1.18.2 的情况下开启,插件为了在子类Builder中未父类属性赋值,对存在继承关系的类自动替换成@SuperBuilder -->
1479+
<!-- @Builder 必须在 Lombok 版本 >= 1.18.2 的情况下开启,对存在继承关系的类自动替换成@SuperBuilder -->
14801480
<property name="@Builder" value="false"/>
14811481
<!-- @NoArgsConstructor 和 @AllArgsConstructor 使用规则和Lombok一致 -->
14821482
<property name="@AllArgsConstructor" value="false"/>

src/main/java/com/itfsw/mybatis/generator/plugins/LombokPlugin.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,6 @@ public boolean validate(List<String> warnings) {
7171
if (!annotation.matches(LOMBOK_ANNOTATION.pattern())) {
7272
this.warnings.add("itfsw:插件" + LombokPlugin.class.getTypeName() + "不能识别的注解(" + annotation + ")!");
7373
return false;
74-
} else if (annotation.startsWith("@Builder") && annotation.matches(".*\\(.*\\)")) {
75-
this.warnings.add("itfsw:插件" + LombokPlugin.class.getTypeName() + "@Builder 注解不允许配置属性(" + annotation + ")!");
76-
return false;
7774
}
7875
}
7976

@@ -198,12 +195,33 @@ private void addAnnotations(TopLevelClass topLevelClass, IntrospectedTable intro
198195
this.addAnnotation(topLevelClass, "@ToString(callSuper = true)");
199196
}
200197
} else if (annotation.startsWith("@Builder")) {
201-
if (PluginTools.getHook(ILombokPluginHook.class).modelBaseRecordBuilderClassGenerated(topLevelClass, columns, introspectedTable)) {
198+
// TODO 配合IncrementsPlugin,以后删除
199+
boolean checkIncrementsPlugin = true;
200+
if (introspectedTable.getRules().generatePrimaryKeyClass() && topLevelClass.getType().getFullyQualifiedName().equals(introspectedTable.getPrimaryKeyType())) {
201+
checkIncrementsPlugin = PluginTools.getHook(ILombokPluginHook.class).modelPrimaryKeyBuilderClassGenerated(topLevelClass, columns, introspectedTable);
202+
} else if (introspectedTable.getRules().generateBaseRecordClass() && topLevelClass.getType().getFullyQualifiedName().equals(introspectedTable.getBaseRecordType())) {
203+
checkIncrementsPlugin = PluginTools.getHook(ILombokPluginHook.class).modelBaseRecordBuilderClassGenerated(topLevelClass, columns, introspectedTable);
204+
} else if (introspectedTable.getRules().generateRecordWithBLOBsClass() && topLevelClass.getType().getFullyQualifiedName().equals(introspectedTable.getRecordWithBLOBsType())) {
205+
checkIncrementsPlugin = PluginTools.getHook(ILombokPluginHook.class).modelRecordWithBLOBsBuilderClassGenerated(topLevelClass, columns, introspectedTable);
206+
}
207+
208+
if (checkIncrementsPlugin) {
202209
// 有子类或者父类
203-
if (introspectedTable.getRules().generateRecordWithBLOBsClass() || introspectedTable.getRules().generatePrimaryKeyClass() || topLevelClass.getSuperClass() != null) {
210+
int count = 0;
211+
if (introspectedTable.getRules().generatePrimaryKeyClass()) {
212+
count++;
213+
}
214+
if (introspectedTable.getRules().generateBaseRecordClass()) {
215+
count++;
216+
}
217+
if (introspectedTable.getRules().generateRecordWithBLOBsClass()) {
218+
count++;
219+
}
220+
221+
if (topLevelClass.getSuperClass() != null || count >= 2) {
204222
this.addAnnotation(topLevelClass, "@SuperBuilder");
205223
} else {
206-
this.addAnnotation(topLevelClass, "@Builder");
224+
this.addAnnotation(topLevelClass, annotation);
207225
}
208226
}
209227
} else {

src/test/java/com/itfsw/mybatis/generator/plugins/LombokPluginTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,26 @@ public void testGenerateDefault() throws Exception {
133133
}
134134
}
135135

136+
/**
137+
* 测试具体生成(只有keys的特殊情况,尽量使用Builder)
138+
*/
139+
@Test
140+
public void testGenerateWithOnlyKeys() throws Exception {
141+
MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/LombokPlugin/mybatis-generator-with-only-keys.xml");
142+
MyBatisGenerator myBatisGenerator = tool.generate();
143+
144+
for (GeneratedJavaFile file : myBatisGenerator.getGeneratedJavaFiles()) {
145+
CompilationUnit compilationUnit = file.getCompilationUnit();
146+
if (compilationUnit instanceof TopLevelClass) {
147+
TopLevelClass topLevelClass = (TopLevelClass) compilationUnit;
148+
String name = topLevelClass.getType().getShortName();
149+
if (name.equals("TbOnlyKeysKey")){
150+
Assert.assertTrue(topLevelClass.getAnnotations().contains("@Builder"));
151+
}
152+
}
153+
}
154+
}
155+
136156
/**
137157
* 测试 @Data 注解
138158
* @throws Exception

src/test/resources/scripts/LombokPlugin/init.sql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,18 @@ CREATE TABLE `tb_lombok` (
7878

7979
-- ----------------------------
8080
-- Records of tb_lombok
81+
-- ----------------------------
82+
83+
-- ----------------------------
84+
-- Table structure for tb_only_keys
85+
-- ----------------------------
86+
DROP TABLE IF EXISTS `tb_only_keys`;
87+
CREATE TABLE `tb_only_keys` (
88+
`key1` bigint(20) NOT NULL,
89+
`key2` bigint(20) NOT NULL,
90+
PRIMARY KEY (`key1`,`key2`)
91+
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
92+
93+
-- ----------------------------
94+
-- Records of tb_only_keys
8195
-- ----------------------------
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright (c) 2019.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<!DOCTYPE generatorConfiguration
19+
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
20+
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
21+
<generatorConfiguration>
22+
<properties resource="db.properties"/>
23+
<!--导入属性配置 -->
24+
<context id="default" targetRuntime="MyBatis3">
25+
<!-- 插件 -->
26+
<plugin type="com.itfsw.mybatis.generator.plugins.LombokPlugin">
27+
<property name="@Builder" value="true"/>
28+
</plugin>
29+
30+
<!--jdbc的数据库连接 -->
31+
<jdbcConnection driverClass="${driver}" connectionURL="${url}" userId="${username}" password="${password}"/>
32+
<!-- Model模型生成器,用来生成含有主键key的类,记录类 以及查询Example类
33+
targetPackage 指定生成的model生成所在的包名
34+
targetProject 指定在该项目下所在的路径 -->
35+
<javaModelGenerator targetPackage="" targetProject=""/>
36+
<!--Mapper映射文件生成所在的目录 为每一个数据库的表生成对应的SqlMap文件 -->
37+
<sqlMapGenerator targetPackage="" targetProject=""/>
38+
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件 的代码
39+
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
40+
type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象
41+
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 -->
42+
<javaClientGenerator targetPackage="" targetProject="" type="XMLMAPPER"/>
43+
44+
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 要自动生成的表 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
45+
<table tableName="tb_only_keys"/>
46+
</context>
47+
</generatorConfiguration>

0 commit comments

Comments
 (0)