MyBatis-Plus自动处理数据库字段

public static void main(String[] args) {

//用来获取Mybatis-Plus.properties文件的配置信息
        ResourceBundle rb =  ResourceBundle.getBundle("Mybatis-Plus");
        AutoGenerator mpg = new AutoGenerator();


// 全局配置
        GlobalConfig gc = new GlobalConfig();


        gc.setOutputDir(rb.getString("OutputDir"));


        gc.setFileOverride(true);
        // 开启 activeRecord 模式
        gc.setActiveRecord(true);
        // XML 二级缓存
        gc.setEnableCache(false);
        // XML ResultMap
        gc.setBaseResultMap(true);
        // XML columList
        gc.setBaseColumnList(false);
        gc.setAuthor(rb.getString("author"));
        mpg.setGlobalConfig(gc);


// 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);
        dsc.setTypeConvert(new MySqlTypeConvert());
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername(rb.getString("userName"));
        dsc.setPassword(rb.getString("passWord"));
        dsc.setUrl(rb.getString("url"));
        mpg.setDataSource(dsc);


// 策略配置
        StrategyConfig strategy = new StrategyConfig();
        // 此处可以修改为您的表前缀
        strategy.setTablePrefix(new String[] { "tbl", "" });
        // 表名生成策略
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setInclude(new String[] {"tbl_ext"});
      // 字段名生成策略

        mpg.setStrategy(strategy);


     // 包配置
        PackageConfig pc = new PackageConfig();
        // 自定义包路径
        pc.setParent(rb.getString("parent"));
        // 这里是控制器包名,默认 web
        pc.setController("controller");
        pc.setEntity("entity");
        pc.setMapper("mapper");
        pc.setXml("mapper");
        pc.setService("service");
        pc.setServiceImpl("service.impl");
        mpg.setPackageInfo(pc);
    
        //模板配置
        TemplateConfig tc = new TemplateConfig();
        tc.setController("/templates/mybatis/controller.java");
        tc.setEntity("/templates/mybatis/entity.java");
        tc.setMapper("/templates/mybatis/mapper.java");
        tc.setService("/templates/mybatis/service.java");
        tc.setServiceImpl("/templates/mybatis/serviceImpl.java");
        mpg.setTemplate(tc);

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

 

execute()方法引用mybatis-plus-generate-2.2.0.jar 

public void execute() {
    logger.debug("==========================准备生成文件...==========================");
    if(null == this.config) {
        this.config = new ConfigBuilder(this.packageInfo, this.dataSource, this.strategy, this.template, this.globalConfig);
        if(null != this.injectionConfig) {
            this.injectionConfig.setConfig(this.config);
        }
    }

    if(null == this.templateEngine) {
        this.templateEngine = new VelocityTemplateEngine();
    }

    this.templateEngine.init(this.pretreatmentConfigBuilder(this.config)).mkdirs().batchOutput().open();
    logger.debug("==========================文件生成完成!!!==========================");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值