mybatis-generator 整合lombok

<plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.7</version>
                <configuration>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>8.0.22</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
<dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.7</version>
        </dependency>

generatorConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>


    <context id="MySql" defaultModelType="flat" targetRuntime="MyBatis3Simple">

   
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />

<plugin type="org.mybatis.generator.plugins.MyBatisPlugin" > <property name="hasLombok" value="true"/> </plugin> <!-- 生成没有注释的bean--> <commentGenerator> <property name="suppressAllComments" value="true"/> </commentGenerator> <!-- 配置数据库连接信息--> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/prss?serverTimezone=Asia/Shanghai" userId="root" password="000000"> </jdbcConnection> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 指定javaBean生成的位置--> <javaModelGenerator targetPackage="com.sdt.schedulingstrategy.bean" targetProject=".\src\main\java"> <property name="enableSubPackages" value="true" /> <!-- 去掉数据库中字段的前后空格--> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- 指定mapper接口的位置--> <sqlMapGenerator targetPackage="com.sdt.schedulingstrategy.mapper" targetProject=".\src\main\java"> <!-- 是否允许子包--> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 指定mappper.xml文件生成的位置--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.sdt.schedulingstrategy.mapper" targetProject=".\src\main\java"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- `bus_app``bus_app_cert``bus_app_key` `bus_ca_cert``bus_device``bus_device_group``bus_device_log` `bus_device_resource``bus_key``bus_ra_cert``bus_resource_quotas``bus_ukey_info``bus_user_cert` --> <!-- 指定每个表的生成策略--> <table tableName="bus_app" domainObjectName="App"></table> </context> </generatorConfiguration>
package org.mybatis.generator.plugins;
// TODO 一定要注意包名,不要改动
import java.util.Date; import java.util.List; import org.apache.commons.lang3.time.DateFormatUtils; import org.mybatis.generator.api.IntrospectedColumn; import org.mybatis.generator.api.IntrospectedTable; import org.mybatis.generator.api.PluginAdapter; import org.mybatis.generator.api.dom.java.Field; import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType; import org.mybatis.generator.api.dom.java.Interface; import org.mybatis.generator.api.dom.java.Method; import org.mybatis.generator.api.dom.java.TopLevelClass; import org.mybatis.generator.internal.util.StringUtility; /** * @ClassName: MyBatisPlugin * @Description: mybatis-generator注释插件 * @author buyl * @date 2019年5月30日 上午22:04:55 * @Version V1.0 */ public class MyBatisPlugin extends PluginAdapter { @Override public boolean validate(List<String> list) { return true; } @Override public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { boolean hasLombok = Boolean.parseBoolean(getProperties().getProperty("hasLombok", "false")); System.out.println("hasLombok" + hasLombok); if (hasLombok) { // 添加domain的import /* * * @ToString @NoArgsConstructor @AllArgsConstructor * * * */ topLevelClass.addImportedType("lombok.Data"); // 添加domain的注解 topLevelClass.addAnnotation("@Data"); topLevelClass.addImportedType("lombok.ToString"); // 添加domain的注解 topLevelClass.addAnnotation("@ToString"); topLevelClass.addImportedType("lombok.NoArgsConstructor"); // 添加domain的注解 topLevelClass.addAnnotation("@NoArgsConstructor"); topLevelClass.addImportedType("lombok.AllArgsConstructor"); // 添加domain的注解 topLevelClass.addAnnotation("@AllArgsConstructor"); } topLevelClass.addJavaDocLine("/**"); String remarks = introspectedTable.getRemarks(); if (StringUtility.stringHasValue(remarks)) { String[] remarkLines = remarks.split(System.getProperty("line.separator")); for (String remarkLine : remarkLines) { topLevelClass.addJavaDocLine(" * " + remarkLine); } } StringBuilder sb = new StringBuilder(); sb.append(" * ").append(introspectedTable.getFullyQualifiedTable()); topLevelClass.addJavaDocLine(sb.toString()); sb.setLength(0); sb.append(" * @author ").append("lzy"); topLevelClass.addJavaDocLine(sb.toString()); sb.setLength(0); sb.append(" * @date "); sb.append(getDateString()); topLevelClass.addJavaDocLine(sb.toString()); topLevelClass.addJavaDocLine(" */"); return true; } @Override public boolean modelFieldGenerated(Field field, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) { field.addJavaDocLine("/**"); String remarks = introspectedColumn.getRemarks(); if (StringUtility.stringHasValue(remarks)) { String[] remarkLines = remarks.split(System.getProperty("line.separator")); for (String remarkLine : remarkLines) { field.addJavaDocLine(" * " + remarkLine); } } field.addJavaDocLine(" */"); return true; } @Override public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { // 添加Mapper的import interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Mapper")); // 添加Mapper的注解 interfaze.addAnnotation("@Mapper"); return true; } @Override public boolean modelSetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) { // 不生成getter boolean hasLombok = Boolean.parseBoolean(getProperties().getProperty("hasLombok", "false")); return !hasLombok; } @Override public boolean modelGetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) { // 不生成setter boolean hasLombok = Boolean.parseBoolean(getProperties().getProperty("hasLombok", "false")); return !hasLombok; } protected String getDateString() { return DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"); } }

mybatis-generator 整合lombok

 

 将编译后的class文件添加到mybatis-generator-core-1.3.7.jar中

首先将这个jar包解压,然后将class文件添加进去,然后运行下面的命令打成jar包,然后放在pod依赖的位置

mybatis-generator 整合lombok

 

mybatis-generator 整合lombok

上一篇:# 2021NC多校05-D(DP+二维前缀和)


下一篇:Paper Reading:ION