Mybatis-plus自动代码生成器自定义模板类

1.为啥需要它?

AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 
Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率。
2.效果图

在这里插入图片描述

3.模板类
package com.kuang;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
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.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;

public class CodeAutoGenerator {

    public static void main(String[] args) {
        // 需要构建一个 代码自动生成器对象
        AutoGenerator mpg = new AutoGenerator();

        // 1.全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir"); // 获取项目路径
        gc.setOutputDir(projectPath+"/src/main/java");
        gc.setOpen(false);// 是否打开文件夹,就是执行完成后打开对应的文件夹
        gc.setFileOverride(false); // 是否文件覆盖
        gc.setAuthor("微滑低");// 作者
        gc.setSwagger2(true);  // swagger配置
        gc.setDateType(DateType.ONLY_DATE);  // 日期类型

        // 自定义文件命名,注意 %s 会自动填充表实体属性!
        gc.setControllerName("%sController");
        // 默认service接口名IXXXService 自定义指定之后就不会用I开头了
        gc.setServiceName("%sService");
        gc.setServiceImplName("%sServiceImpl");
        gc.setMapperName("%sMapper");
        gc.setXmlName("%sMapper");
        mpg.setGlobalConfig(gc);

        // 2.数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("root");
        dsc.setUrl("jdbc:mysql://localhost:3306/mybatis_plus?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8");
        mpg.setDataSource(dsc);

        // 3.策略配置
        StrategyConfig strategy = new StrategyConfig();

        strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略(下划线转驼峰)
        strategy.setInclude("user"); // 需要生成的表名
        strategy.setColumnNaming(NamingStrategy.underline_to_camel); // 列名下划线转驼峰
        strategy.setEntityLombokModel(true); // lombok
        strategy.setLogicDeleteFieldName("deleted"); // 逻辑删除

        // 自动填充配置
        TableFill createTime = new TableFill("create_time", FieldFill.INSERT); // 创建时间自动填充
        TableFill updateTime = new TableFill("update_time", FieldFill.INSERT_UPDATE); // 更新时间更新自动填充
        ArrayList<TableFill> tableFills = new ArrayList<>();
        tableFills.add(createTime);
        tableFills.add(updateTime);
        strategy.setTableFillList(tableFills);

        // 乐观锁
        strategy.setVersionFieldName("version");

        // controller驼峰命名
        strategy.setRestControllerStyle(true);

        // localhost:8080/hello_id_2
        strategy.setControllerMappingHyphenStyle(true);

        mpg.setStrategy(strategy);

        // 4.包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("blog");
        pc.setParent("com.kuang");
        pc.setController("controller");
        pc.setService("service");
        pc.setMapper("mapper");
        pc.setEntity("pojo");
        mpg.setPackageInfo(pc);

        // 执行生成
        mpg.execute();

    }

}
4.这里演示的一生成张表的目录结构,如果需要生成多表,则在下面这个属性添加要生成的表,它支持多个表

在这里插入图片描述

5.终于节省了时间,释放了双手,再也不用整天一步一步的写这么多的目录文件结构了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值