mybatis-plus的代码生成(逆向工程)

256 阅读3分钟

主要的分了五步:

1、数据源配置

DataSourceConfig dsc = new DataSourceConfig();
// 配置数据库 url 地址
dsc.setUrl("jdbc:mysql://XXXXXX:3306/kanban?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai");
// 配置数据库驱动
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
// 数据库账号
dsc.setUsername("账号");
// 数据库密码
dsc.setPassword("密码");

2、全局配置

GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
// 代码生成的目录
gc.setOutputDir(projectPath + "/src/main/java");
// 作者信息
gc.setAuthor("liaojie");
// 配置是否打开目录,false 为不打开
gc.setOpen(false);
// 第二次生成会把第一次生成的覆盖掉
gc.setFileOverride(true);

// 自定义service 命名
gc.setServiceName("%sService");
gc.setServiceImplName("%sServiceImpl");

// 生成 baseResultMap 和 baseColumnList
gc.setBaseResultMap(true); // 生成resultMap
gc.setBaseColumnList(true);// 在xml中生成基础列

3、包配置

PackageConfig pc = new PackageConfig();
// 设置包名的父类名
pc.setParent("com.tianshu");
// 配置模块名
pc.setModuleName("system");
mpg.setPackageInfo(pc);

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

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

4、模板配置

TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);

5、策略配置

StrategyConfig strategy = new StrategyConfig();
// 配置数据表与实体类名之间映射的策略 (下划线到驼峰的命名方式)
strategy.setNaming(NamingStrategy.underline_to_camel);
// 配置数据表的字段与实体类的属性名之间映射的策略 (表名字段名使用下划线)
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
// 配置 lombok 模式
strategy.setEntityLombokModel(true);
// 配置 rest 风格的控制器(@RestController)
strategy.setRestControllerStyle(true);
// 【实体】是否生成字段常量(默认 false)
strategy.setEntityColumnConstant(true);
// 是否为链式模型(默认 false)
strategy.setChainModel(true);

// 公共父类
strategy.setSuperEntityClass(BaseEntity.class);
// 自定义的 BaseMapper
strategy.setSuperMapperClass("com.tianshu.base.BaseMapper");
// 自定义的service
strategy.setSuperServiceClass(BaseService.class);
// 自定义的service实现类
strategy.setSuperServiceImplClass(BaseServiceImpl.class);
// 自定义的Controller
strategy.setSuperControllerClass(BaseController.class);

// 写于父类中的公共字段
strategy.setSuperEntityColumns();

if (StringUtils.hasText(pc.getModuleName())) {
    strategy.setTablePrefix(pc.getModuleName() + "_");
}
strategy.setControllerMappingHyphenStyle(true);


主要的代码
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.tianshu.base.BaseController;
import com.tianshu.base.BaseEntity;
import com.tianshu.base.BaseService;
import com.tianshu.base.BaseServiceImpl;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;


public class CodeGenerator {

    public static void main(String[] args) {

        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        // 配置数据库 url 地址
        dsc.setUrl("jdbc:mysql://数据库地址:3306/kanban?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai");
        // 配置数据库驱动
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        // 数据库账号
        dsc.setUsername("账号");
        // 数据库密码
        dsc.setPassword("密码");
        mpg.setDataSource(dsc);

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        // 代码生成的目录
        gc.setOutputDir(projectPath + "/src/main/java");
        // 作者信息
        gc.setAuthor("liaojie");
        // 配置是否打开目录,false 为不打开
        gc.setOpen(false);
        // 第二次生成会把第一次生成的覆盖掉
        gc.setFileOverride(true);

        // 自定义service 命名
        gc.setServiceName("%sService");
        gc.setServiceImplName("%sServiceImpl");

        // 生成 baseResultMap 和 baseColumnList
        gc.setBaseResultMap(true); // 生成resultMap
        gc.setBaseColumnList(true);// 在xml中生成基础列
        mpg.setGlobalConfig(gc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        // 设置包名的父类名
        pc.setParent("com.tianshu");
        // 配置模块名
        pc.setModuleName("system");
        mpg.setPackageInfo(pc);

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

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

        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setXml(null);
        mpg.setTemplate(templateConfig);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        // 配置数据表与实体类名之间映射的策略 (下划线到驼峰的命名方式)
        strategy.setNaming(NamingStrategy.underline_to_camel);
        // 配置数据表的字段与实体类的属性名之间映射的策略 (表名字段名使用下划线)
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        // 配置 lombok 模式
        strategy.setEntityLombokModel(true);
        // 配置 rest 风格的控制器(@RestController)
        strategy.setRestControllerStyle(true);
        // 【实体】是否生成字段常量(默认 false)
        strategy.setEntityColumnConstant(true);
        // 是否为链式模型(默认 false)
        strategy.setChainModel(true);

        // 公共父类
        strategy.setSuperEntityClass(BaseEntity.class);
        // 自定义的 BaseMapper
        strategy.setSuperMapperClass("com.tianshu.base.BaseMapper");
        // 自定义的service
        strategy.setSuperServiceClass(BaseService.class);
        // 自定义的service实现类
        strategy.setSuperServiceImplClass(BaseServiceImpl.class);
        // 自定义的Controller
        strategy.setSuperControllerClass(BaseController.class);

        // 写于父类中的公共字段
        strategy.setSuperEntityColumns();

        if (StringUtils.hasText(pc.getModuleName())) {
            strategy.setTablePrefix(pc.getModuleName() + "_");
        }
        strategy.setControllerMappingHyphenStyle(true);
        mpg.setStrategy(strategy);
        mpg.execute();
    }

}