mybatis-plus生成代码

添加依赖

复制代码
    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus-boot-starter</artifactId>
      <version>3.3.0</version>
    </dependency>

    <!--mybatis plus 代码生成器 https://github.com/baomidou/generator-->
    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus-generator</artifactId>
      <version>3.4.1</version>
    </dependency>
    <!-- 用于代码生成    -->
    <dependency>
      <groupId>org.apache.velocity</groupId>
      <artifactId>velocity-engine-core</artifactId>
      <version>2.3</version>
    </dependency>

添加拦截器

复制代码
package com.uzx.compliance.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * MyBatis-Plus 配置(分页、乐观锁等)。
 *
 * 主要是注册 {@link PaginationInnerInterceptor},保证 Page.total 能正确返回总条数。
 */
@Configuration
public class MybatisPlusConfig {

    /**
     * 新的分页插件,支持多种拦截器组合。
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 分页拦截器,使用 MySQL 方言
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
        paginationInnerInterceptor.setMaxLimit(1000L);
        interceptor.addInnerInterceptor(paginationInnerInterceptor);

        // 乐观锁拦截器(如暂时不用,也可以保留不影响分页)
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return interceptor;
    }
}

生成代码工具类

复制代码
package fi.usdx.util;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

public class CodeGeneratorUtil {


  public static void main(String[] args) {
    // 代码生成器
    AutoGenerator mpg = new AutoGenerator();
    //mpg.setTemplateEngine(new FreemarkerTemplateEngine());

    // 全局配置
    GlobalConfig gc = new GlobalConfig();
    // 生成文件的输出目录
    System.out.println(System.getProperty("user.dir") + "/temp");
    gc.setOutputDir(System.getProperty("user.dir") + "/temp");
    // 指定生成的主键的ID类型
    gc.setIdType(null);
    // 开发人员
    gc.setAuthor("");
    // 是否打开输出目录
    gc.setOpen(false);
    // 是否在xml中添加二级缓存配置
    gc.setEnableCache(false);
    // 关闭 swagger2 模式
    gc.setSwagger2(false);
    // 实体命名方式
    gc.setEntityName("%sEntity");
    // mapper 命名方式
    gc.setMapperName("%sMapper");
    // Mapper xml 命名方式
    gc.setXmlName("%sMapper");
    // service 命名方式
    gc.setServiceName("%sService");
    // service impl 命名方式
    gc.setServiceImplName("%sServiceImpl");
    // controller 命名方式
    gc.setControllerName("%sController");
    gc.setBaseResultMap(true);// 生成resultMap
    gc.setBaseColumnList(true);// XML中生成基础列
    mpg.setGlobalConfig(gc);

    // 数据源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setDriverName("com.mysql.jdbc.Driver");
    dsc.setUrl(
        "jdbc:mysql://43.207.185.242:13306/usdx?useUnicode=true&characterEncoding=UTF-8&useLegacyDatetimeCode=false&useTimezone=true&serverTimezone=UTC&allowPublicKeyRetrieval=true&verifyServerCertificate=false&useSSL=false");
    dsc.setUsername("zzg");
    dsc.setPassword("baqrYDmmG6YdEj2zdPQvcTSN!");

//    dsc.setUrl(
//        "jdbc:mysql://localhost:3306/usdx?useUnicode=true&characterEncoding=UTF-8&useLegacyDatetimeCode=false&useTimezone=true"
//            + "&serverTimezone=UTC&allowPublicKeyRetrieval=true&verifyServerCertificate=false&useSSL=false");
//    dsc.setUsername("root");
//    dsc.setPassword("123456");

//    dsc.setUrl("jdbc:mysql://127.0.0.1:3306/sun?serverTimezone=UTC&useUnicode=true&useSSL=false&characterEncoding=utf8");
//    dsc.setUsername("root");
//    dsc.setPassword("cjl1234");

    // 设置数据源
    mpg.setDataSource(dsc);
    // 包配置
    PackageConfig pc = new PackageConfig();
    // 父包名
    pc.setParent("fi.usdx");
    // 模块名
//    pc.setModuleName("");
    // Entity包名
    pc.setEntity("entity");
    // Mapper包名
    pc.setMapper("mapper");
    // Controller包名
    pc.setController("controller");
    // Service包名
    pc.setService("service");
    mpg.setPackageInfo(pc);

    // 策略配置
    StrategyConfig strategy = new StrategyConfig();
    // underline_to_camel:转驼峰,no_change:无变化
    // 数据库表映射到实体的命名策略
    strategy.setNaming(NamingStrategy.underline_to_camel);
    // 数据库表字段映射到实体的命名策略
    strategy.setColumnNaming(NamingStrategy.underline_to_camel);
    // 【实体】是否为lombok模型(默认 false)
    strategy.setEntityLombokModel(true);
    // 实体增加  @TableId 注解
    strategy.setEntityTableFieldAnnotationEnable(true);

    // 生成 @RestController 控制器
    strategy.setRestControllerStyle(true);
    // 实体父类
    //strategy.setSuperEntityClass(BaseEntity.class);
    // 实体父类中的字段,多个用逗号分隔
    //strategy.setSuperEntityColumns("id")
    // 表名称
    strategy.setInclude(("big_swap_monitor_record").split(","));
    // 驼峰转连字符
    strategy.setControllerMappingHyphenStyle(true);
    // 表前缀
    //strategy.setTablePrefix("t_");
    mpg.setStrategy(strategy);

    mpg.execute();
  }

}

结果展示

  1. entitiy

    Data
    @EqualsAndHashCode(callSuper = false)
    @TableName("config")
    public class ConfigEntity implements Serializable {

    复制代码
     private static final long serialVersionUID = 1L;
    
     /**
      * 自增主键
      */
     @TableId(value = "id", type = IdType.AUTO)
     private Long id;
    
     /**
      * key
      */
     @TableField("`key`")
     private String key;
    
     /**
      * value
      */
     @TableField("value")
     private String value;
    
     /**
      * 乐观锁预留
      */
     @TableField("`version`")
     private Integer version;
    
     /**
      * 逻辑删除,冗余字段
      */
     @TableField("deleted")
     private Boolean deleted;
    
     @TableField("deleted_at")
     private LocalDateTime deletedAt;
    
     @TableField(value = "updated_at",updateStrategy= FieldStrategy.NEVER)
     private LocalDateTime updatedAt;
    
     @TableField("created_at")
     private LocalDateTime createdAt;

    }

  2. mapper

    public interface ConfigMapper extends BaseMapper {

    }

  3. service

    public interface ConfigService extends IService {}

  4. serviceimple

    @Service
    public class ConfigServiceImpl extends ServiceImpl<ConfigMapper, ConfigEntity> implements ConfigService {

    }

相关推荐
学长毕设9 分钟前
基于SpringBoot的儿童慈善捐赠管理系统-附源码
java·spring boot·后端
慧一居士18 分钟前
@RestControllerAdvice 的注解作用和使用
java
ComPDFKit32 分钟前
RAG 回答只显示 PDF 文件名?用 Python 保留引用页码
开发语言·python·pdf·rag
FRing0342 分钟前
vue3干货总结2(vue3生命周期、父子通信、模版引用、provide和inject)
开发语言·前端·vue.js·vue3
学长毕设1 小时前
基于SpringBoot非遗文化展示与传播平台设计与实现-附源码
java·spring boot·后端
金銀銅鐵3 小时前
用 Swing 重做操作数栈演示
java·jvm·后端
琳少_3 小时前
java程序开发笔记
java·开发语言·数据库·笔记
七夜zippoe3 小时前
Agent 告警设计:分级告警、智能降噪与值班排班
开发语言·ai·agent·告警·智能降噪·值班排班
叼烟扛炮5 小时前
[LangChain] 学习笔记(15):为什么你的重试没用——429、抖动、fallback 与熔断
java·服务器·前端
严肃小白兔5 小时前
PHP 8.6 正式发布:部分函数应用、原生 I/O 轮询与新特性实战解析
开发语言·php