MyBatisPlus —— 7、代码生成器

该篇博客介绍了如何通过MyBatisPlus的FastAutoGenerator工具来快速生成Java项目的数据库操作代码。首先,需要引入MyBatisPlus和Freemarker的依赖。然后,配置数据库连接、作者信息、输出目录、包名、模板引擎等参数,并指定要生成的表。执行代码生成后,可以看到生成的文件包括实体类,且实体类属性遵循驼峰命名规则。整个过程简洁高效,大大提高了开发效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

1、引入依赖

2、快速生成


1、引入依赖

        <!-- MyBatisPlus代码生成器核心依赖 -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.5.1</version>
        </dependency>

        <!-- freemarker引擎模板依赖 -->
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.31</version>
        </dependency>

2、快速生成

生成代码:

public class FastAutoGeneratorTest {

    public static void main(String[] args) {
        FastAutoGenerator.create("jdbc:mysql://127.0.0.1:3306/mybatis_plus?characterEncoding=utf-8&userSSL=false", "root", "zyj123")  // 连接数据库
                .globalConfig(builder -> {
                    builder.author("zyj") // 设置作者
                            // .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir("D://Java//尚硅谷MyBatisPlus//MyBatisPlusFastAutoGenerator"); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent("com.zyj") // 设置父包名
                            .moduleName("mybatisplus") // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, "D://Java//尚硅谷MyBatisPlus//MyBatisPlusFastAutoGenerator")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude("t_user") // 设置需要生成的表名
                            .addTablePrefix("t_", "c_"); // 设置过滤表前缀
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker 引擎模板,默认的是Velocity引擎模板
                .execute();
    }

}

输出结果:

10:15:17.975 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - ==========================准备生成文件...==========================
10:15:18.578 [main] DEBUG com.baomidou.mybatisplus.generator.config.querys.MySqlQuery - 执行SQL:show table status WHERE 1=1  AND NAME IN ('t_user')
10:15:18.632 [main] DEBUG com.baomidou.mybatisplus.generator.config.querys.MySqlQuery - 返回记录数:1,耗时(ms):51
10:15:18.675 [main] DEBUG com.baomidou.mybatisplus.generator.config.querys.MySqlQuery - 执行SQL:show full fields from `t_user`
10:15:18.696 [main] DEBUG com.baomidou.mybatisplus.generator.config.querys.MySqlQuery - 返回记录数:6,耗时(ms):20
10:15:19.077 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - ==========================文件生成完成!!!==========================

生成的文件结构如下:

 且生成的实体类的属性名是根据驼峰命名规则的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值