MybatisGenerator+lombok 川长思鸟来 2021-10-15 06:01 531阅读 0赞 先继承Mybatis的插件适配器, 写一个自己的插件 package org.mybatis.generator.plugins; 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.Interface; import org.mybatis.generator.api.dom.java.Method; import org.mybatis.generator.api.dom.java.TopLevelClass; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; /** * @author Liweizhou 2018/6/6 */ public class LombokPlugin extends PluginAdapter { @Override public boolean validate(List<String> list) { return true; } @Override public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { //添加domain的import topLevelClass.addImportedType("lombok.Data"); topLevelClass.addImportedType("lombok.Builder"); topLevelClass.addImportedType("lombok.NoArgsConstructor"); topLevelClass.addImportedType("lombok.AllArgsConstructor"); //添加domain的注解 topLevelClass.addAnnotation("@Data"); topLevelClass.addAnnotation("@Builder"); topLevelClass.addAnnotation("@NoArgsConstructor"); topLevelClass.addAnnotation("@AllArgsConstructor"); //添加domain的注释 topLevelClass.addJavaDocLine("/**"); topLevelClass.addJavaDocLine("* Created by Mybatis Generator on " + date2Str(new Date())); topLevelClass.addJavaDocLine("*/"); return true; } @Override public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) { //Mapper文件的注释 interfaze.addJavaDocLine("/**"); interfaze.addJavaDocLine("* Created by Mybatis Generator on " + date2Str(new Date())); interfaze.addJavaDocLine("*/"); return true; } @Override public boolean modelSetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) { //不生成getter return false; } @Override public boolean modelGetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) { //不生成setter return false; } private String date2Str(Date date) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); return sdf.format(date); } } 首先在本机编译这份java文件, 生成的字节码文件(.class), 先到maven仓库的这个目录 org\\mybatis\\generator\\mybatis-generator-core\\1.3.5 (版本无所谓, 根据自己情况修改) 找到这个mybatis-generator-core-1.3.5.jar文件 把字节码塞到 jar包对应的包文件夹下面 ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L21pbmd3dWxpcG8_size_16_color_FFFFFF_t_70] mybatis-generator.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="postgresql" targetRuntime="MyBatis3"> <plugin type="org.mybatis.generator.plugins.LombokPlugin" > <property name="hasLombok" value="true"/> </plugin> <!--关闭注释 --> <commentGenerator> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--数据库连接信息 --> <jdbcConnection driverClass="org.postgresql.Driver" connectionURL="" userId="" password=""> </jdbcConnection> <!--生成的model 包路径 --> <javaModelGenerator targetPackage="com.lwz.dao.generator.model" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> </javaModelGenerator> <!--生成xml mapper文件 路径 --> <sqlMapGenerator targetPackage="mapper/generator" targetProject="src/main/resources"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!-- 生成的Dao接口 的包路径 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.lwz.dao.generator.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!--对应数据库表名 --> <table tableName="example"> </table> </context> </generatorConfiguration> /** * Created by Mybatis Generator on 2018/06/06 */ @Data @Builder @NoArgsConstructor @AllArgsConstructor public class Example{ [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L21pbmd3dWxpcG8_size_16_color_FFFFFF_t_70]: /images/20211015/2ecd9db5c81b4eaaad7e3edf4dd80d04.png
还没有评论,来说两句吧...