From 858db7a87f516a75965ca3f3dbc288a33f98fadb Mon Sep 17 00:00:00 2001 From: hewei Date: Tue, 30 Oct 2018 09:43:17 +0800 Subject: [PATCH] LombokPlugin --- pom.xml | 16 ++ .../generator/plugins/LombokPlugin.java | 212 ++++++++++++++++++ .../generator/plugins/LombokPluginTest.java | 68 ++++++ .../resources/scripts/LombokPlugin/init.sql | 87 +++++++ .../LombokPlugin/mybatis-generator.xml | 52 +++++ 5 files changed, 435 insertions(+) create mode 100644 src/main/java/com/itfsw/mybatis/generator/plugins/LombokPlugin.java create mode 100644 src/test/java/com/itfsw/mybatis/generator/plugins/LombokPluginTest.java create mode 100644 src/test/resources/scripts/LombokPlugin/init.sql create mode 100644 src/test/resources/scripts/LombokPlugin/mybatis-generator.xml diff --git a/pom.xml b/pom.xml index ccd9c61..2f1f348 100644 --- a/pom.xml +++ b/pom.xml @@ -89,6 +89,22 @@ commons-beanutils commons-beanutils 1.9.3 + test + + + org.projectlombok + lombok + [1.18.2,) + test + + + + com.sun + tools + 1.8 + system + + ${env.JAVA_HOME}/lib/tools.jar diff --git a/src/main/java/com/itfsw/mybatis/generator/plugins/LombokPlugin.java b/src/main/java/com/itfsw/mybatis/generator/plugins/LombokPlugin.java new file mode 100644 index 0000000..0ca1dd4 --- /dev/null +++ b/src/main/java/com/itfsw/mybatis/generator/plugins/LombokPlugin.java @@ -0,0 +1,212 @@ +/* + * Copyright (c) 2018. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.itfsw.mybatis.generator.plugins; + +import com.itfsw.mybatis.generator.plugins.utils.BasePlugin; +import org.mybatis.generator.api.IntrospectedColumn; +import org.mybatis.generator.api.IntrospectedTable; +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; +import java.util.Properties; + +/** + * --------------------------------------------------------------------------- + * LombokPlugin + * --------------------------------------------------------------------------- + * @author: hewei + * @time:2018/10/29 14:33 + * --------------------------------------------------------------------------- + */ +public class LombokPlugin extends BasePlugin { + public static final String PRO_BUILDER = "@Builder"; // 是否支持 Builder 注解 + public static final String PRO_ALL_ARGS_CONSTRUCTOR = "@AllArgsConstructor"; // 是否支持 AllArgsConstructor 注解 + public static final String PRO_NO_ARGS_CONSTRUCTOR = "@NoArgsConstructor"; // 是否支持 NoArgsConstructor 注解 + + private boolean hasBuilder; + private boolean hasAllArgsConstructor; + private boolean hasNoArgsConstructor; + + /** + * 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html + * @param warnings + * @return + */ + @Override + public boolean validate(List warnings) { + Properties properties = this.getProperties(); + + String builder = properties.getProperty(PRO_BUILDER); + String allArgsConstructor = properties.getProperty(PRO_ALL_ARGS_CONSTRUCTOR); + String noArgsConstructor = properties.getProperty(PRO_NO_ARGS_CONSTRUCTOR); + + this.hasBuilder = builder == null ? false : StringUtility.isTrue(builder); + this.hasAllArgsConstructor = allArgsConstructor == null ? false : StringUtility.isTrue(allArgsConstructor); + this.hasNoArgsConstructor = noArgsConstructor == null ? false : StringUtility.isTrue(noArgsConstructor); + + return super.validate(warnings); + } + + /** + * 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html + * @param topLevelClass + * @param introspectedTable + * @return + */ + @Override + public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { + // @Data + this.addAnnotations(topLevelClass, EnumLombokAnnotations.DATA); + if (topLevelClass.getSuperClass() != null){ + this.addAnnotations(topLevelClass, EnumLombokAnnotations.EQUALS_AND_HASH_CODE_CALL_SUPER); + } + // @Builder + if (this.hasBuilder){ + if (introspectedTable.getRules().generateRecordWithBLOBsClass() || introspectedTable.getRules().generatePrimaryKeyClass()){ + this.addAnnotations(topLevelClass, EnumLombokAnnotations.SUPER_BUILDER); + } else { + this.addAnnotations(topLevelClass, EnumLombokAnnotations.BUILDER); + } + } + if (this.hasNoArgsConstructor){ + this.addAnnotations(topLevelClass, EnumLombokAnnotations.NO_ARGS_CONSTRUCTOR); + } + if (this.hasAllArgsConstructor){ + this.addAnnotations(topLevelClass, EnumLombokAnnotations.ALL_ARGS_CONSTRUCTOR); + } + return super.modelBaseRecordClassGenerated(topLevelClass, introspectedTable); + } + + /** + * 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html + * @param topLevelClass + * @param introspectedTable + * @return + */ + @Override + public boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { + // @Data + this.addAnnotations(topLevelClass, EnumLombokAnnotations.DATA); + + // @Builder + if (this.hasBuilder){ + if (introspectedTable.getRules().generateRecordWithBLOBsClass() || introspectedTable.getRules().generateBaseRecordClass()){ + this.addAnnotations(topLevelClass, EnumLombokAnnotations.SUPER_BUILDER); + } else { + this.addAnnotations(topLevelClass, EnumLombokAnnotations.BUILDER); + } + } + if (this.hasNoArgsConstructor){ + this.addAnnotations(topLevelClass, EnumLombokAnnotations.NO_ARGS_CONSTRUCTOR); + } + if (this.hasAllArgsConstructor){ + this.addAnnotations(topLevelClass, EnumLombokAnnotations.ALL_ARGS_CONSTRUCTOR); + } + return super.modelPrimaryKeyClassGenerated(topLevelClass, introspectedTable); + } + + /** + * 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html + * @param topLevelClass + * @param introspectedTable + * @return + */ + @Override + public boolean modelRecordWithBLOBsClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { + // @Data + this.addAnnotations(topLevelClass, EnumLombokAnnotations.DATA); + if (topLevelClass.getSuperClass() != null){ + this.addAnnotations(topLevelClass, EnumLombokAnnotations.EQUALS_AND_HASH_CODE_CALL_SUPER); + } + + // @Builder + if (this.hasBuilder){ + if (introspectedTable.getRules().generateBaseRecordClass() || introspectedTable.getRules().generatePrimaryKeyClass()){ + this.addAnnotations(topLevelClass, EnumLombokAnnotations.SUPER_BUILDER); + } else { + this.addAnnotations(topLevelClass, EnumLombokAnnotations.BUILDER); + } + } + if (this.hasNoArgsConstructor){ + this.addAnnotations(topLevelClass, EnumLombokAnnotations.NO_ARGS_CONSTRUCTOR); + } + if (this.hasAllArgsConstructor){ + this.addAnnotations(topLevelClass, EnumLombokAnnotations.ALL_ARGS_CONSTRUCTOR); + } + return super.modelRecordWithBLOBsClassGenerated(topLevelClass, introspectedTable); + } + + /** + * 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html + * @param method + * @param topLevelClass + * @param introspectedColumn + * @param introspectedTable + * @param modelClassType + * @return + */ + @Override + public boolean modelGetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) { + return false; + } + + /** + * 具体执行顺序 http://www.mybatis.org/generator/reference/pluggingIn.html + * @param method + * @param topLevelClass + * @param introspectedColumn + * @param introspectedTable + * @param modelClassType + * @return + */ + @Override + public boolean modelSetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) { + return false; + } + + /** + * 添加注解 + * @param topLevelClass + * @param annotations + */ + private void addAnnotations(TopLevelClass topLevelClass, EnumLombokAnnotations annotations){ + topLevelClass.addImportedType(annotations.clazz); + topLevelClass.addAnnotation(annotations.annotations); + } + + /** + * lombok 类型 + */ + public static enum EnumLombokAnnotations { + DATA("@Data", "lombok.Data"), + BUILDER("@Builder", "lombok.Builder"), + SUPER_BUILDER("@SuperBuilder", "lombok.experimental.SuperBuilder"), + ALL_ARGS_CONSTRUCTOR("@AllArgsConstructor", "lombok.AllArgsConstructor"), + EQUALS_AND_HASH_CODE_CALL_SUPER("@EqualsAndHashCode(callSuper = true)", "lombok.EqualsAndHashCode"), + NO_ARGS_CONSTRUCTOR("@NoArgsConstructor", "lombok.NoArgsConstructor"); + + private final String annotations; + private final String clazz; + + EnumLombokAnnotations(String annotations, String clazz) { + this.annotations = annotations; + this.clazz = clazz; + } + } +} diff --git a/src/test/java/com/itfsw/mybatis/generator/plugins/LombokPluginTest.java b/src/test/java/com/itfsw/mybatis/generator/plugins/LombokPluginTest.java new file mode 100644 index 0000000..6aada13 --- /dev/null +++ b/src/test/java/com/itfsw/mybatis/generator/plugins/LombokPluginTest.java @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2018. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.itfsw.mybatis.generator.plugins; + +import com.itfsw.mybatis.generator.plugins.tools.AbstractShellCallback; +import com.itfsw.mybatis.generator.plugins.tools.DBHelper; +import com.itfsw.mybatis.generator.plugins.tools.MyBatisGeneratorTool; +import org.apache.ibatis.session.SqlSession; +import org.junit.BeforeClass; +import org.junit.Test; +import org.mybatis.generator.api.GeneratedJavaFile; +import org.mybatis.generator.api.MyBatisGenerator; + +/** + * --------------------------------------------------------------------------- + * + * --------------------------------------------------------------------------- + * @author: hewei + * @time:2018/10/29 15:45 + * --------------------------------------------------------------------------- + */ +public class LombokPluginTest { + /** + * 初始化 + */ + @BeforeClass + public static void init() throws Exception{ + DBHelper.createDB("scripts/LombokPlugin/init.sql"); + } + + /** + * 测试具体生成 + */ + @Test + public void testGenerate() throws Exception { + // 全局规则增加 DB, tb2 单独规则增加Tt + MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/LombokPlugin/mybatis-generator.xml"); + MyBatisGenerator myBatisGenerator = tool.generate(); + for (GeneratedJavaFile file : myBatisGenerator.getGeneratedJavaFiles()){ + String name = file.getCompilationUnit().getType().getShortName(); + } + } + + @Test + public void test() throws Exception { + MyBatisGeneratorTool tool = MyBatisGeneratorTool.create("scripts/LombokPlugin/mybatis-generator.xml"); + tool.generate(new AbstractShellCallback() { + @Override + public void reloadProject(SqlSession sqlSession, ClassLoader loader, String packagz) throws Exception{ + System.out.println("xxx"); + } + }); + } +} diff --git a/src/test/resources/scripts/LombokPlugin/init.sql b/src/test/resources/scripts/LombokPlugin/init.sql new file mode 100644 index 0000000..d44b036 --- /dev/null +++ b/src/test/resources/scripts/LombokPlugin/init.sql @@ -0,0 +1,87 @@ +/* +Navicat MySQL Data Transfer + +Source Server : localhost +Source Server Version : 50617 +Source Host : localhost:3306 +Source Database : mybatis-generator-plugin + +Target Server Type : MYSQL +Target Server Version : 50617 +File Encoding : 65001 + +Date: 2017-07-03 17:34:11 +*/ + +SET FOREIGN_KEY_CHECKS=0; + +-- ---------------------------- +-- Table structure for tb +-- ---------------------------- +DROP TABLE IF EXISTS `tb`; +CREATE TABLE `tb` ( + `id` bigint(20) NOT NULL COMMENT '注释1', + `field1` varchar(255) DEFAULT NULL COMMENT '注释2', + `field2` int(11) DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; + +-- ---------------------------- +-- Records of tb +-- ---------------------------- +INSERT INTO `tb` VALUES ('1', 'fd1', null); +INSERT INTO `tb` VALUES ('2', null, '2'); +INSERT INTO `tb` VALUES ('3', 'fd3', '3'); + +-- ---------------------------- +-- Table structure for tb_blobs +-- ---------------------------- +DROP TABLE IF EXISTS `tb_blobs`; +CREATE TABLE `tb_blobs` ( + `id` bigint(20) NOT NULL COMMENT '注释1', + `field1` varchar(255) DEFAULT NULL, + `field2` longtext COMMENT '注释2', + `field3` longtext, + PRIMARY KEY (`id`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; + +-- ---------------------------- +-- Records of tb_blobs +-- ---------------------------- +INSERT INTO `tb_blobs` VALUES ('1', 'fd1', null, 'KK1'); +INSERT INTO `tb_blobs` VALUES ('2', null, '2', null); +INSERT INTO `tb_blobs` VALUES ('3', 'fd3', '3', 'KK3'); + +-- ---------------------------- +-- Table structure for tb_keys +-- ---------------------------- +DROP TABLE IF EXISTS `tb_keys`; +CREATE TABLE `tb_keys` ( + `key1` bigint(20) NOT NULL COMMENT '注释1', + `key2` varchar(255) NOT NULL, + `field1` varchar(255) DEFAULT NULL COMMENT '注释2', + `field2` int(11) DEFAULT NULL, + PRIMARY KEY (`key1`,`key2`) +) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; + +-- ---------------------------- +-- Records of tb_keys +-- ---------------------------- +INSERT INTO `tb_keys` VALUES ('1', '2', 'fd1', null); +INSERT INTO `tb_keys` VALUES ('2', '3', null, '2'); +INSERT INTO `tb_keys` VALUES ('3', '4', 'fd2', '3'); + +-- ---------------------------- +-- Table structure for tb_single_blob +-- ---------------------------- +DROP TABLE IF EXISTS `tb_single_blob`; +CREATE TABLE `tb_single_blob` ( + `id` bigint(20) NOT NULL COMMENT '注释1', + `field1` longtext COMMENT '注释2', + `field2` int(11) DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; + +-- ---------------------------- +-- Records of tb_single_blob +-- ---------------------------- \ No newline at end of file diff --git a/src/test/resources/scripts/LombokPlugin/mybatis-generator.xml b/src/test/resources/scripts/LombokPlugin/mybatis-generator.xml new file mode 100644 index 0000000..32ed7ba --- /dev/null +++ b/src/test/resources/scripts/LombokPlugin/mybatis-generator.xml @@ -0,0 +1,52 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+ + \ No newline at end of file