【SpringBoot DB系列】Mybatis-Plus 代码自动生成

news/2024/7/2 21:34:21

【SpringBoot DB系列】Mybatis-Plus 代码自动生成

一个简单的实例工程,介绍利用 mybatis-plus 的代码自动生成插件,根据表结构来生成对应的类和 xml 配置文件

I. 代码生成

本文主要内容来自官方教程,通过实例方式介绍代码生成过程

1. 准备

准备两张表,用于测试

CREATE TABLE `userT0` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(20) NOT NULL DEFAULT '' COMMENT '用户名',
  `pwd` varchar(26) NOT NULL DEFAULT '' COMMENT '密码',
  `isDeleted` tinyint(1) NOT NULL DEFAULT '0',
  `created` varchar(13) NOT NULL DEFAULT '0',
  `updated` varchar(13) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE `story_t0` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `userId` int(20) unsigned NOT NULL DEFAULT '0' COMMENT '作者的userID',
  `name` varchar(20) NOT NULL DEFAULT '' COMMENT '作者名',
  `title` varchar(26) NOT NULL DEFAULT '' COMMENT '密码',
  `story` text COMMENT '故事内容',
  `is_deleted` tinyint(1) NOT NULL DEFAULT '0',
  `create_at` varchar(13) NOT NULL DEFAULT '0',
  `update_at` varchar(13) NOT NULL DEFAULT '0',
  `tag` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `userId` (`userId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

请注意,上面两张表的命名格式并不一样,有的是驼峰,有的是下划线(主要为了演示不同表名,对于生成代码的影响)

2. 配置依赖

首先需要在我们的 xml 文件中,添加相关的依赖

<dependencies>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-generator</artifactId>
        <version>3.3.1.tmp</version>
    </dependency>
    <dependency>
        <groupId>org.freemarker</groupId>
        <artifactId>freemarker</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

    <!-- 下面两个,用于测试生成后的代码,在生成代码时,可以不需要-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.2.0</version>
    </dependency>
</dependencies>

3. 代码生成类

写一个代码生成类方法,主要逻辑如下

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

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir") + "/spring-boot/106-mybatis-plus-generator";
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("YiHui");
        gc.setOpen(false);
        // 覆盖写
        gc.setFileOverride(false);
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://127.0.0.1:3306/story?useUnicode=true&characterEncoding=UTF-8&useSSL=false");
        // dsc.setSchemaName("public");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("");
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        // 不额外指定模块,如果指定为 test,则生成的xml会在 mapper/test/ 目录下
        pc.setModuleName("");
        pc.setParent("com.git.hui.boot.mybatis.plus");
        mpg.setPackageInfo(pc);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };

        // 如果模板引擎是 freemarker
        String templatePath = "/templates/mapper.xml.ftl";

        // 自定义输出配置
        List<FileOutConfig> focList = new ArrayList<>();
        // 自定义配置会被优先输出
        focList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
                return projectPath + "/src/main/resources/mapper/" + pc.getModuleName() + "/" +
                        tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });

        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);

        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setXml(null);
        // 不自动生成controller类
        templateConfig.setController(null);
        mpg.setTemplate(templateConfig);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        // strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        // 公共父类
        // strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!");
        // 写于父类中的公共字段
        // strategy.setSuperEntityColumns("id");

        // 设置需要生成的表名
        strategy.setInclude("userT0", "story_t0");
        strategy.setControllerMappingHyphenStyle(true);
        // strategy.setTablePrefix(pc.getModuleName() + "_");
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }
}

上面的代码,绝大部分都是通用的,下面着重说明需要注意的点

  • GlobalConfig#setOutputDir: 设置代码输出的项目根路径,请根据具体的项目要求进行指定,不包含包名哦
  • GlobalConfig#setFileOverride(true): 设置为 true,则每次生成都会覆盖之前生成的代码,适用于表结构发生变化的场景
    • 注意:会导致之前添加的业务代码被覆盖掉,需要额外注意
    • 通常希望设置为 false,当表结构发生变化时,手动介入
  • DataSourceConfig: 数据源的设置,上面设置的是 mysql 的相关配置
  • PackageConfig: 包信息
    • setParent: java 包路径
    • setModuleName: 设置模块名,如设置为 test,则 xml 在mapper/test/目录下; parent 包自动加上.test
  • FileOutConfig: xml 文件名
  • TemplateConfig: 模板配置
    • 可用默认的代码生成模板,也可以使用自定义的模板
    • 不想生成某个模板类时,设置为 null 即可(如上面的不生成 controller)
  • StrategyConfig: 策略配置
    • 可以指定 db->pojo 字段名的映射规则
    • 可以指定 POJO/Controller 继承自定义的基类

在 IDEA 中,直接右键执行上面的代码,就会生成目标类,如下截图

4. 输出测试

测试我们生成的类,是否可以对 db 进行操作,则有必要写一个启动类

@RestController
@SpringBootApplication
@MapperScan("com.git.hui.boot.mybatis.plus.mapper")
public class Application {
    @Autowired
    private IUserT0Service userT0Service;

    @GetMapping
    public UserT0 hello(int id) {
        return userT0Service.getById(id);
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }
}

请注意上面的@MapperScan注解,其次对应的application.yml配置文件内容如下

spring:
  datasource:
    # 注意指定时区
    url: jdbc:mysql://127.0.0.1:3306/story?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password:

mybatis-plus:
  configuration:
    # 执行的sql语句日志输出
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

在 db 中插入一条数据

INSERT INTO `userT0` (`id`, `name`, `pwd`, `isDeleted`, `created`, `updated`)
VALUES
	(1, '一灰灰', 'yihuihuiblog', 0, '2020-04-06 15', '2020-04-06 15');

访问 url: http://localhost:8080/?id=1

控制台输出如下:

5. 特殊场景说明

上面的代码生成,针对首次执行生成打码时,问题不大;但是后续的业务开发中,总会有一些其他的情况,下面分别说明

a. 表结构修改

当表的结构发生变化时,我们需要一般需要重新生成对应的 Entity,这个时候,需要GlobalConfig#setFileOverride(true)

b. 继承公用 POJO

我们可以定义一个通用的 PO 类,希望所有的表生成的 POJO 继承它

@Data
public class BasePo implements Serializable {
    private static final long serialVersionUID = -1136173266983480386L;

    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

}

在代码自动生成类的策略配置中,添加下面的两行设置即可

// 所有实体类继承自 BasePo, 且id在父类中
StrategyConfig strategy = new StrategyConfig();
strategy.setSuperEntityClass(BasePo.class);
strategy.setSuperEntityColumns("id");

c. 生成部分代码

有些时候,我并不希望生成service,xml,可能就只需要实体类 + mapper接口,这个时候可以设置TemplateConfig

TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setController(null);
templateConfig.setEntityKt(null);
templateConfig.setService(null);
templateConfig.setServiceImpl(null);

II. 其他

0. 项目

系列博文

  • 【DB 系列】MybatisPlus 整合篇
  • 【DB 系列】Mybatis+注解整合篇
  • 【DB 系列】Mybatis+xml 整合篇

源码

  • 工程:https://github.com/liuyueyi/spring-boot-demo
  • 源码:https://github.com/liuyueyi/spring-boot-demo/blob/master/spring-boot/106-mybatis-plus-generator

1. 一灰灰 Blog

尽信书则不如,以上内容,纯属一家之言,因个人能力有限,难免有疏漏和错误之处,如发现 bug 或者有更好的建议,欢迎批评指正,不吝感激

下面一灰灰的个人博客,记录所有学习和工作中的博文,欢迎大家前去逛逛

  • 一灰灰 Blog 个人博客 https://blog.hhui.top
  • 一灰灰 Blog-Spring 专题博客 http://spring.hhui.top

一灰灰blog


http://www.niftyadmin.cn/n/4231368.html

相关文章

javascript单线程 示例

<!DOCTYPE html><html><head lang"en"><meta charset"UTF-8"><title>progress 和 meter 元素</title></head><body><script>function btn(){for(var i0;i<100;i){ //先执行完循环再执行下面的函…

Java实现AES ECP PKCS5Padding加解密工具类

Java 实现一个AES/ECB/PKCS5Padding 加解密算法工具类 加密算法&#xff1a; AES模式&#xff1a; ECB补码方式&#xff1a; PKCS5Padding 1. 工具类 import lombok.Getter; import lombok.Setter; import lombok.extern.slf4j.Slf4j; import org.springframework.util.Base6…

【SpringBoot 基础系列】实现一个自定义配置加载器(应用篇)

【SpringBoot 基础系列】实现一个自定义配置加载器&#xff08;应用篇&#xff09; Spring 中提供了Value注解&#xff0c;用来绑定配置&#xff0c;可以实现从配置文件中&#xff0c;读取对应的配置并赋值给成员变量&#xff1b;某些时候&#xff0c;我们的配置可能并不是在配…

《我和你》-51CTO学院

提起码字儿写文章&#xff0c;只是《十七岁那年的雨季》时的《理想》。从当年的《追风少年》到现在的大叔&#xff0c;不知是什么力量&#xff0c;也许是《天意》&#xff0c;《在那遥远的地方》好像有一双《隐形的翅膀》使我《大约在冬季》的时候《选择》和学院《牵手》合作。…

【SpringCloud 系列】Eureka 注册中心初体验

【SpringCloud 系列】Eureka 注册中心初体验 在 SpringCloud 微服务体系中&#xff0c;有几个比较重要的组件&#xff0c;如注册中心&#xff0c;配置中心&#xff0c;网关&#xff0c;安全、负载均衡、监控等等&#xff0c;接下来我们将来看一下这些常用的组件有什么用&#x…

【SpringBoot 基础系列】SpEL 语法扫盲与查询手册

【SpringBoot 基础系列】SpEL 语法扫盲与查询手册 Spring 表达式语言简称为 SpEL&#xff0c;一种类似 Ognl 的对象图导航语言&#xff08;对于 ognl 不熟悉的同学可以参考一下: Ognl 系列博文&#xff09; SeEL 为 Spring 提供了丰富的想象空间&#xff0c;除了一些基本的表达…

php生成唯一编号(36进制的不重复编号)

2019独角兽企业重金招聘Python工程师标准>>> http://www.jb51.net/article/51705.htm class Code { //密码字典 private $dic array( 0>0, 1>1, 2>2, 3>3, 4>4, 5>5, 6>6, 7>7, 8>8, 9>9, 10>A, 11>B, 12>C, 13>…

【SpringBoot WEB系列】静态资源配置与读取

【WEB系列】静态资源配置与读取 SpringWeb项目除了我们常见的返回json串之外&#xff0c;还可以直接返回静态资源&#xff08;当然在现如今前后端分离比较普遍的情况下&#xff0c;不太常见了&#xff09;&#xff0c;一些简单的web项目中&#xff0c;前后端可能就一个人包圆了…