Springboot mybatis generate 自动生成实体类和Mapper
一、运行
运行MyBatisGeneratorApp类即可实现自动生成实体类、mapper接口、mapper.xml
二、配置步骤
1、pom文件引入依赖
<!-- MyBatis --><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.2</version></dependency><!-- MyBatis Generator --><dependency><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-core</artifactId><version>1.3.5</version></dependency><!-- Lombok --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.20</version><scope>provided</scope></dependency><!-- Mysql --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version></dependency>
2、自定义 lombok 插件类 LombokPlugin.java
packagecom.example.autoentity.utils;importorg.mybatis.generator.api.IntrospectedColumn;importorg.mybatis.generator.api.IntrospectedTable;importorg.mybatis.generator.api.PluginAdapter;importorg.mybatis.generator.api.dom.java.Field;importorg.mybatis.generator.api.dom.java.Interface;importorg.mybatis.generator.api.dom.java.Method;importorg.mybatis.generator.api.dom.java.TopLevelClass;importorg.mybatis.generator.internal.util.StringUtility;importjava.text.SimpleDateFormat;importjava.util.Date;importjava.util.List;/**
* @Author: haoyun
* @Date: 2023-06-14 09:49
* @Description: 自定义lombok类
*/publicclassLombokPluginextendsPluginAdapter{@Overridepublicbooleanvalidate(List<String> list){returntrue;}/**
* 为实体添加lombok的注解
* @param topLevelClass
* @param introspectedTable
* @return
*/@OverridepublicbooleanmodelBaseRecordClassGenerated(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(newDate()));
topLevelClass.addJavaDocLine("*/");returntrue;}/**
* 为实体类字段添加注释
* @param field
* @param topLevelClass
* @param introspectedColumn
* @param introspectedTable
* @param modelClassType
* @return
*/@OverridepublicbooleanmodelFieldGenerated(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(" */");returntrue;}/**
* mapper.xml的注释
* @param interfaze
* @param topLevelClass
* @param introspectedTable
* @return
*/@OverridepublicbooleanclientGenerated(Interface interfaze,TopLevelClass topLevelClass,IntrospectedTable introspectedTable){//Mapper文件的注释
interfaze.addJavaDocLine("/**");
interfaze.addJavaDocLine("* Created by Mybatis Generator on "+date2Str(newDate()));
interfaze.addJavaDocLine("*/");returntrue;}@OverridepublicbooleanmodelSetterMethodGenerated(Method method,TopLevelClass topLevelClass,IntrospectedColumn introspectedColumn,IntrospectedTable introspectedTable,ModelClassType modelClassType){//不生成getterreturnfalse;}@OverridepublicbooleanmodelGetterMethodGenerated(Method method,TopLevelClass topLevelClass,IntrospectedColumn introspectedColumn,IntrospectedTable introspectedTable,ModelClassType modelClassType){//不生成setterreturnfalse;}privateStringdate2Str(Date date){SimpleDateFormat sdf =newSimpleDateFormat("yyyy/MM/dd");return sdf.format(date);}}
3、添加 generatorConfig.xml 配置文件
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPEgeneratorConfigurationPUBLIC"-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN""http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"><generatorConfiguration><contextid="default"targetRuntime="MyBatis3"><propertyname="autoDelimitKeywords"value="true"/><!-- 生成的Java文件的编码 --><propertyname="javaFileEncoding"value="utf-8"/><!-- beginningDelimiter和endingDelimiter:指明数据库的用于标记数据库对象名的符号,比如ORACLE就是双引号,MYSQL默认是`反引号; --><propertyname="beginningDelimiter"value="`"/><propertyname="endingDelimiter"value="`"/><!-- 格式化java代码 --><propertyname="javaFormatter"value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/><!-- 格式化XML代码 --><propertyname="xmlFormatter"value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/><plugintype="org.mybatis.generator.plugins.SerializablePlugin"/><plugintype="org.mybatis.generator.plugins.ToStringPlugin"/><!--lombok 自动生成lombok注解配置和根据数据库注释为实体添加注释, 需要自己编译文件才可以实现--><plugintype="com.example.autoentity.utils.LombokPlugin"/><commentGenerator><!-- 是否去除自动生成的注释 true:是 : false:否 --><propertyname="suppressAllComments"value="true"/><!-- 是否取消注释 --><propertyname="suppressDate"value="true"/><!-- 是否生成注释代时间戳--><!-- 注解采用数据库的标注,suppressAllComments 必须设置为 false 才会生效 --><propertyname="addRemarkComments"value="true"/></commentGenerator><!-- 配置数据库连接信息 --><jdbcConnectiondriverClass="com.mysql.jdbc.Driver"connectionURL="jdbc:mysql://ip:port/rjwm"userId="root"password="password"/><!-- 配置实体类生成规则 --><javaModelGeneratortargetPackage="com.example.autoentity.entity"targetProject="src/main/java"><propertyname="enableSubPackages"value="false"/><propertyname="trimStrings"value="true"/></javaModelGenerator><!-- 配置映射文件生成规则 --><sqlMapGeneratortargetPackage="mapper"targetProject="src/main/resources"/><!-- 配置 DAO 接口生成规则 --><javaClientGeneratortype="XMLMAPPER"targetPackage="com.example.autoentity.employee.mapper"targetProject="src/main/java"/><!-- 配置数据库表和实体类的映射关系 --><tabletableName="user"domainObjectName="User"/></context></generatorConfiguration>
4、Generator 启动类 MyBatisGeneratorApp.java
packagecom.example.autoentity.utils;importorg.mybatis.generator.api.MyBatisGenerator;importorg.mybatis.generator.config.Configuration;importorg.mybatis.generator.config.xml.ConfigurationParser;importorg.mybatis.generator.exception.InvalidConfigurationException;importorg.mybatis.generator.exception.XMLParserException;importorg.mybatis.generator.internal.DefaultShellCallback;importjava.io.File;importjava.io.IOException;importjava.sql.SQLException;importjava.util.ArrayList;importjava.util.List;publicclassMyBatisGeneratorApp{publicstaticvoidmain(String[] args){try{List<String> warnings =newArrayList<>();boolean overwrite =true;File configFile =newFile("C:\\autoentity\\src\\main\\resources\\MyBatis-Generator\\generatorConfig.xml");ConfigurationParser cp =newConfigurationParser(warnings);Configuration config = cp.parseConfiguration(configFile);DefaultShellCallback callback =newDefaultShellCallback(overwrite);MyBatisGenerator myBatisGenerator =newMyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);System.out.println("Entity classes generated successfully!");}catch(IOException|XMLParserException|InvalidConfigurationException|SQLException|InterruptedException e){
e.printStackTrace();}}}
本文转载自: https://blog.csdn.net/LSW1737554365/article/details/131397165
版权归原作者 信息化战略 所有, 如有侵权,请联系我们删除。
版权归原作者 信息化战略 所有, 如有侵权,请联系我们删除。