Mybatis-plus自动生成实体类工具

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.IColumnType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

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

public class Generator {
    /**
     * <p>
     * MySQL 生成演示
     * </p>
     */
    //模块名称
    private static String projectName = "模块名称";

    private static String canonicalPath = "项目绝对路径名称"+projectName;

    public static void main(String[] args) {

        //获取项目路径
//        try {
//            canonicalPath = new File("").getCanonicalPath();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        String filePath = canonicalPath+"\\src\\main\\";
        System.out.println("canonicalPath="+filePath);
        AutoGenerator mpg = new AutoGenerator();
        // 选择 freemarker 引擎,默认 Veloctiy
        //mpg.setTemplateEngine(new FreemarkerTemplateEngine());

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setAuthor("yekongyu");
        gc.setOutputDir(filePath+"java");//代码生成路径
        gc.setFileOverride(true);// 是否覆盖同名文件,默认是false
        gc.setActiveRecord(true);// 不需要ActiveRecord特性的请改为false
        gc.setEnableCache(false);// XML 二级缓存
        gc.setBaseResultMap(true);// XML ResultMap
        gc.setBaseColumnList(false);// XML columList
        gc.setOpen(false);//生成后打开文件夹
        /* 自定义文件命名,注意 %s 会自动填充表实体属性! */
        gc.setMapperName("%sMapper");
        gc.setXmlName("%sMapper");
        gc.setServiceName("%sService");
        gc.setServiceImplName("%sServiceImpl");
        // gc.setControllerName("%sAction");
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);
        dsc.setTypeConvert(new MySqlTypeConvert() {
            // 自定义数据库表字段类型转换【可选】
            @Override
            public IColumnType processTypeConvert(GlobalConfig gc, String fieldType) {
                System.out.println("转换类型:" + fieldType);
                // 注意!!processTypeConvert 存在默认类型转换,如果不是你要的效果请自定义返回、非如下直接返回。
                //默认会把日期类型 转为LocalDateTime ,在查询的时候会报错,这里改为Date
                String t = fieldType.toLowerCase();
                if(t.contains("date") || t.contains("time") || t.contains("year")){
                    return DbColumnType.DATE;
                }else{
                    return super.processTypeConvert(gc,fieldType);
                }

            }
        });
        // 连接数据库
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("root");
        dsc.setUrl("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8");
        mpg.setDataSource(dsc);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        //生成 @RestController 控制器
//        strategy.setRestControllerStyle(false);
        //strategy.setCapitalMode(true);// 全局大写命名 ORACLE 注意
        //strategy.setTablePrefix("tb_");// 此处可以修改为您的表前缀
        strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);//【实体】是否为lombok模型(默认 false)
        //strategy.setInclude(new String[] { "user" }); // 需要生成的表
         strategy.setExclude(new String[]{"undo_log"}); // 排除生成的表
        // 自定义实体父类
        // strategy.setSuperEntityClass("com.baomidou.demo.TestEntity");
        // 自定义实体,公共字段
        // strategy.setSuperEntityColumns(new String[] { "test_id", "age" });
        // 自定义 mapper 父类
        // strategy.setSuperMapperClass("com.baomidou.demo.TestMapper");
        // 自定义 service 父类
        // strategy.setSuperServiceClass("com.baomidou.demo.TestService");
        // 自定义 service 实现类父类
        // strategy.setSuperServiceImplClass("com.baomidou.demo.TestServiceImpl");
        // 自定义 controller 父类
        // strategy.setSuperControllerClass("com.baomidou.demo.TestController");
        // 【实体】是否生成字段常量(默认 false)
        // public static final String ID = "test_id";
        // strategy.setEntityColumnConstant(true);
        // 【实体】是否为构建者模型(默认 false)
        // public User setName(String name) {this.name = name; return this;}
        // strategy.setEntityBuilderModel(true);
        mpg.setStrategy(strategy);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.heima."+projectName);// 自定义包路径
//        pc.setController("controller");// 这里是控制器包名,默认 web
        pc.setMapper("mapper");// 设置Mapper包名,默认mapper
        pc.setService("service");// 设置Service包名,默认service
        pc.setEntity("entity");// 设置Entity包名,默认entity,继承的父类  已序列化
        //pc.setXml("mapper.xml");// 设置Mapper XML包名,默认mapper.xml
        mpg.setPackageInfo(pc);


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

        // 注入自定义配置,可以在 VM 中使用 cfg.abc 【可无】
//        InjectionConfig cfg = new InjectionConfig() {
//            @Override
//            public void initMap() {
//                Map<String, Object> map = new HashMap<String, Object>();
//                map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
//                this.setMap(map);
//            }
//        };
//
//        // 自定义 xxList.jsp 生成
//        List<FileOutConfig> focList = new ArrayList<>();
//        focList.add(new FileOutConfig("/template/list.jsp.vm") {
//            @Override
//            public String outputFile(TableInfo tableInfo) {
//                // 自定义输入文件名称
//                return "D://my_" + tableInfo.getEntityName() + ".jsp";
//            }
//        });
//        cfg.setFileOutConfigList(focList);
//        mpg.setCfg(cfg);
//
//        // 调整 xml 生成目录演示
//        focList.add(new FileOutConfig("/templates/mapper.xml.vm") {
//            @Override
//            public String outputFile(TableInfo tableInfo) {
//                return "/develop/code/xml/" + tableInfo.getEntityName() + ".xml";
//            }
//        });
//        cfg.setFileOutConfigList(focList);
//        mpg.setCfg(cfg);
//
//        // 关闭默认 xml 生成,调整生成 至 根目录
        TemplateConfig tc = new TemplateConfig();
        tc.setXml(null);
        mpg.setTemplate(tc);


        // 调整 xml 生成目录演示
        List<FileOutConfig> focList = new ArrayList<>();

        focList.add(new FileOutConfig("/templates/mapper.xml.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return filePath+"\\resources\\mapper\\xml\\" + tableInfo.getEntityName() + "Mapper.xml";
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);

        // 自定义模板配置,可以 copy 源码 mybatis-plus/src/main/resources/templates 下面内容修改,
        // 放置自己项目的 src/main/resources/templates 目录下, 默认名称一下可以不配置,也可以自定义模板名称
//         TemplateConfig tc = new TemplateConfig();
//         tc.setController("");
        // tc.setEntity("...");
        // tc.setMapper("...");
        // tc.setXml("...");
        // tc.setService("...");
        // tc.setServiceImpl("...");
        // 如上任何一个模块如果设置 空 OR Null 将不生成该模块。
        // mpg.setTemplate(tc);

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

        // 打印注入设置【可无】
//        System.err.println(mpg.getCfg().getMap().get("abc"));
    }
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自己编写的实体类生成器 支持数据库oracle 和 SqlServer 支持实体类生成程序:C# 和 java *注意: 电脑上一定要安装 framework才能运行本软件 有什么问题请联系qq:910579826 备注添加:实体类生成器 1. 用户首先选择要操作的数据库,选中后程序会自动创建数据库连接字符串。 2.选择要生成实体类类型,程序会根据选中的类型自动生成相对应的实体类 3.在程序左侧会显示连接的数据库列表,选择要操作的数据库,会自动加载该数据库下的表信息。 4.选择要操作的表,在右侧会显示该表的表中的字段信息 5.程序自动处理数据库中的书库类型,将其转换为程序相对应的可以识别的数据类型。 6.在字段显示窗体,用户还可以手动选择数据类型。 7.用户可以选择要生成的字段信息,不想生成的字段只要取消选中状态即可 8.选择生成文件保存的路径 9.设置要生成的包名或名称空间名 10.点击生成按钮。这时,弹出一个编辑实体类窗体,在这里可以对即将要生成实体类信息进行检查并修改信息。 11.修改完毕后,点击生成。完毕后提示生成成功! *备注:程序中每一步都有相对应的必要检测,当用户选择不符合必须选择或填写的信息时,将提示相对应的提示。 修改,创建操作时,都有确认提示。 说明: 根据数据库表结构,自动生成实例类 支持的数据库:oracle和SQLServer 可生成实体类:C#实体类Java实体类 软件特点: 同时支持Oracle和SQLServer两种数据库 同时支持Java和C#程序的实体类生成 同时支持任意数据库搭配任意程序的实体类生成

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值