MybatisPlus代码自动生成工具类(代码生成器)

代码自动生成工具类

配置文件 mybatis-generator.properties

#jdbc:mysql://ip:port/database
DS_URL=jdbc:mysql://127.0.0.1:3306/test
DS_DRIVER_NAME=com.microsoft.sqlserver.jdbc.SQLServerDriver
#登陆账号
DS_USER_NAME=root
#密码
DS_PASSWORD=123456
#生成文件到本地项目路径 填错造成文件找不到
PROJECT_ROOT=E:/test/
#作者 可不填
AUTHOR=
#包配置
PARENT=com.test
#实体类配置
ENTITY=model.entity
OutputDir=src/main/java
OutputDirXml=src/main/resources/mapper
#默认无逻辑删除字段
tableLogicalDeletion=false
#逻辑删除字段
LogicDeleteField=is_delete
#继承逻辑删除公共实体类
SUPER_LOGIC_DELETE_ENTITY=module.LogicalDeletionEntity
#继承无逻辑删除公共实体类
SUPER_ENTITY=module.Entity
#缓存
enableCache=false
#表名 不填为所有
tableName=

工具类


import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

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

public class CodeGenerator {

    public static void main(String[] args) {
    	//获取配置文件
        final ResourceBundle moduleRb = ResourceBundle.getBundle("mybatis-generator");
		//代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = getString(moduleRb, "PROJECT_ROOT");
        gc.setOutputDir(projectPath + getString(moduleRb, "OutputDir"));
        gc.setAuthor(getString(moduleRb, "AUTHOR"));
        gc.setOpen(false);
        gc.setSwagger2(true); // 实体属性 Swagger2 注解
        gc.setBaseColumnList(true);
        gc.setEnableCache(Boolean.parseBoolean(getString(moduleRb, "enableCache")));
        gc.setIdType(IdType.AUTO);
        gc.setEntityName("%sDO");
        gc.setServiceName("%sService");
        gc.setXmlName("%sMapper");
        gc.setMapperName("%sMapper");
        gc.setBaseResultMap(true);
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(getString(moduleRb, "DS_URL"));
        dsc.setDriverName(getString(moduleRb, "DS_DRIVER_NAME"));
        dsc.setUsername(getString(moduleRb, "DS_USER_NAME"));
        dsc.setPassword(getString(moduleRb, "DS_PASSWORD"));
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(getString(moduleRb, "PARENT"));
        pc.setEntity(getString(moduleRb, "ENTITY"));
        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 + getString(moduleRb, "OutputDirXml") + "/" + tableInfo.getXmlName() + StringPool.DOT_XML;
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);

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

        // 策略配置
        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("你自己的父类控制器,没有就不用设置!");
        // 写于父类中的公共字段
        Boolean tableLogicalDeletion = Boolean.valueOf(getString(moduleRb, "tableLogicalDeletion"));
        if (tableLogicalDeletion) {
            strategy.setLogicDeleteFieldName(getString(moduleRb, "LogicDeleteField"));
            strategy.setSuperEntityClass(getString(moduleRb, "PARENT") + getString(moduleRb, "SUPER_LOGIC_DELETE_ENTITY"));
            //继承实体类的字段,包括逻辑删除字段
            strategy.setSuperEntityColumns("id", "add_time", "update_time", getString(moduleRb, "LogicDeleteField"));
        } else {
            strategy.setSuperEntityClass(getString(moduleRb, "PARENT") + getString(moduleRb, "SUPER_ENTITY"));
            //继承实体类的字段,不包括逻辑删除字段
            strategy.setSuperEntityColumns("id", "add_time", "update_time");
        }
        strategy.setEntityColumnConstant(true);
        strategy.setEntityBooleanColumnRemoveIsPrefix(true);

        String tableName = getString(moduleRb, "tableName");
        if (StringUtils.isNotBlank(tableName)) {
            strategy.setInclude(tableName);
        }

        strategy.setControllerMappingHyphenStyle(true);
        mpg.setStrategy(strategy);
        mpg.execute();
    }

    private static String getString(ResourceBundle resourceBundle, String key) {
        return resourceBundle.getString(key);
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是光吖。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值