java 代码根据表创建实体类

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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 代码生成器演示
 * </p>
 */
public class CodeGenerator {

    /**
     * 修改生成配置
     */
    public static String dbUrl = "jdbc:mysql://aclsh-sit-pdb-sdi-ma.mysql.polardb.rds.aliyuncs.com:3306/actionnow-authorize?&useSSL=false&autoReconnect=true&autoReconnectForPools=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=Asia/Shanghai";

    public static String dbName = "sid_pdb_acn_sit";

    public static String dbPassword = "*n7eR)a^8KR3Ym";

    public static String[] removePreTableName = new String[]{""};

    //生成路径
    public static String parentpackage = "cn.com.do1.dsf.modules.dsf";

    //需要执行生成策略的表
    public static String[] tables = new String[]{
            "tb_actionnow_pipeline_node_messages"
    };

    /**
     * <p>
     * MySQL 生成演示
     * </p>
     */
    public static void main(String[] args) {
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        System.out.println(projectPath);
//        gc.setOutputDir(projectPath + "/acn-web-backend/model/src/main/java");
        gc.setFileOverride(true);
        gc.setActiveRecord(true);// 不需要ActiveRecord特性的请改为false
        gc.setEnableCache(false);// XML 二级缓存
        gc.setBaseResultMap(true);// XML ResultMap
        gc.setBaseColumnList(false);// XML columList
        gc.setAuthor("generator");

        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);
        // 自定义数据库表字段类型转换【可选】
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername(dbName);
        dsc.setPassword(dbPassword);
        dsc.setUrl(dbUrl);
        mpg.setDataSource(dsc);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setTablePrefix(removePreTableName);// 此处可以修改移除表前缀
        strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
        strategy.setTablePrefix("tb_");
        strategy.setInclude(tables); // 需要生成的表
        strategy.setEntityLombokModel(false);
        strategy.setRestControllerStyle(true);
        // 表填充字段,创建日期、更新日期
        List<TableFill> tableFillList = new ArrayList<>();
        tableFillList.add(new TableFill("CREATE_TIME", FieldFill.INSERT));
        tableFillList.add(new TableFill("UPDATE_TIME", FieldFill.INSERT_UPDATE));
        tableFillList.add(new TableFill("CREATE_USER", FieldFill.INSERT));
        tableFillList.add(new TableFill("UPDATE_USER", FieldFill.INSERT_UPDATE));
        strategy.setTableFillList(tableFillList);
        mpg.setStrategy(strategy);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent("");
//        pc.setModuleName("");
        pc.setEntity("cn.com.do1.dsf.modules.dsf.entity");
        pc.setController("cn.com.do1.dsf.controller");
        pc.setService("cn.com.do1.dsf.service.serviceInterface");
        pc.setServiceImpl("cn.com.do1.dsf.service");
        pc.setMapper("cn.com.do1.dsf.dao");
        mpg.setPackageInfo(pc);

        // 注入自定义配置,可以在 VM 中使用 cfg.abc 【可无】
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                Map<String, Object> map = new HashMap<>();
                map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
                this.setMap(map);
            }
        };

        List<FileOutConfig> focList = new ArrayList<>();
        // 调整 xml 生成目录演示
        focList.add(new FileOutConfig("/template/mapper.xml.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                tableInfo.setXmlName(tableInfo.getEntityName() + "Dao");
                return projectPath + "/acn-web-backend/web/src/main/resources/mapper/actionnow/" + tableInfo.getEntityName() + "Dao.xml";
            }
        });
        focList.add(new FileOutConfig("/template/domain.java.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return projectPath + "/acn-web-backend/model/src/main/java/cn/com/aia/acn/customization/entity/" + tableInfo.getEntityName() + ".java";
            }
        });
        focList.add(new FileOutConfig("/template/controller.java.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return projectPath + "/acn-web-backend/web/src/main/java/cn/com/aia/acn/customization/controller/" + tableInfo.getEntityName() + "Controller.java";
            }
        });
        focList.add(new FileOutConfig("/template/service.java.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                tableInfo.setServiceName(tableInfo.getEntityName() + "Service");
                return projectPath + "/acn-web-backend/web/src/main/java/cn/com/aia/acn/customization/service/" + tableInfo.getEntityName() + "Service.java";
            }
        });
        focList.add(new FileOutConfig("/template/serviceImpl.java.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                tableInfo.setMapperName(tableInfo.getEntityName() + "Dao");
                return projectPath + "/acn-web-backend/web/src/main/java/cn/com/aia/acn/customization/service/impl/" + tableInfo.getEntityName() + "ServiceImpl.java";
            }
        });
        focList.add(new FileOutConfig("/template/mapper.java.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                tableInfo.setMapperName(tableInfo.getEntityName() + "Dao");
                return projectPath + "/acn-web-backend/web/src/main/java/cn/com/aia/acn/customization/dao/" + tableInfo.getEntityName() + "Dao.java";
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);

        TemplateConfig tc = new TemplateConfig();
        tc.setEntity("/template/domain.java.vm");
        tc.setController("/template/controller.java.vm");
        tc.setService("/template/service.java.vm");
        tc.setServiceImpl("/template/serviceImpl.java.vm");
        tc.setMapper("/template/mapper.java.vm");
        tc.setXml("/template/mapper.xml.vm");
        mpg.setTemplate(tc);

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

    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值