mybatis-plus自动生成底层代码

package com.zhm.blog.util;

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.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;

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

/**
 * mybatis-plus代码生成器
 * 通过 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码
 */
public class CodeGenerator {

    @Autowired
    private Environment env;//环境变量

    public static void createClasses(String tableName){
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");//项目路径
        gc.setOutputDir(projectPath + "/src/main/java");//生成文件的输出目录 默认值:D 盘根目录
        gc.setFileOverride(false);//是否覆盖已有文件 默认false
        gc.setAuthor("zhm");//作者
        gc.setOpen(false); //是否打开输出目录 默认true
        gc.setEnableCache(false);//是否在xml中添加二级缓存配置 默认false
        gc.setIdType(null);//指定生成的主键的ID类型 默认null
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/cms-shangtang?serverTimezone=UTC");
        // dsc.setSchemaName("public");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("123456");
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName(""); //父包模块名
        pc.setParent("com.zhm.blog");//父包名。如果为空,将下面子包名必须写全部, 否则就只需写子包名
        pc.setEntity("entity");//Entity包名
        pc.setService("service");//Service包名
        pc.setServiceImpl("serviceImpl");//Service Impl包名
        pc.setMapper("mapper");//Mapper包名
        pc.setXml("xml");//Mapper XML包名
        pc.setController("controller");//Controller包名
        mpg.setPackageInfo(pc);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };
        List<FileOutConfig> focList = new ArrayList<>();
        focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
                return projectPath + "/src/main/resources/xml/" + pc.getModuleName()
                        + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);
        mpg.setTemplate(new TemplateConfig().setXml(null));

        // 策略配置(数据库表配置)
        StrategyConfig strategy = new StrategyConfig()
                .entityTableFieldAnnotationEnable(true);//是否生成实体时,生成字段注解
        strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略, 未指定按照 naming 执行
        strategy.setEntityLombokModel(true); //【实体】是否为lombok模型(默认 false)
        strategy.setRestControllerStyle(true);//生成 @RestController 控制器
//        strategy.setSuperEntityClass("com.baomidou.ant.common.BaseEntity");//自定义继承的Entity类全称,带包名
//        strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController");//自定义继承的Controller类全称,带包名
        strategy.setInclude(tableName);//需要包含的表名,允许正则表达式(与exclude二选一配置)
//        strategy.setSuperEntityColumns("id");//自定义基础的Entity类,公共字段
        strategy.setControllerMappingHyphenStyle(true);//驼峰转连字符
//        strategy.setTablePrefix(pc.getModuleName() + "_");//表前缀
        strategy.setLogicDeleteFieldName("del_flag");//逻辑删除属性名称
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }

    public static void main(String[] args) {
        createClasses("user");
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_39936341/article/details/84324979