java 代码根据表创建实体类

此博客展示了使用Java和MyBatis-Plus进行代码生成的过程。配置了全局、数据源、策略、包等信息,包括数据库连接、表名策略、生成路径等,还设置了注入自定义配置和模板,最后执行代码生成操作。
部署运行你感兴趣的模型镜像
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();

    }

}

您可能感兴趣的与本文相关的镜像

Seed-Coder-8B-Base

Seed-Coder-8B-Base

文本生成
Seed-Coder

Seed-Coder是一个功能强大、透明、参数高效的 8B 级开源代码模型系列,包括基础变体、指导变体和推理变体,由字节团队开源

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值