MyBatisPlus全局配置

全局配置

此文件是根据Oracle数据库(可为其他数据库)表反向生成entity、mapper、service以及web(Controller)包及其配置文件

package com;


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


import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.FileOutConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.converts.OracleTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;


public class TestLP {


public static void main(String[] args) throws Exception {
String name = TestLP.class.getName().toLowerCase();
        name = name.substring(name.lastIndexOf(".") + 1, name.lastIndexOf("lp"));
        
        AutoGenerator mpg = new AutoGenerator();
        
     // 全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir("D://");//自定义文件生成的根目录盘符
        gc.setFileOverride(true);
        gc.setActiveRecord(true);
        gc.setEnableCache(false);// XML 二级缓存
        gc.setBaseResultMap(false);// XML ResultMap
        gc.setBaseColumnList(true);// XML columList
        gc.setAuthor("lipeng");
        
     // 自定义文件夹名,注意 %s 会自动填充表实体属性!
//     gc.setMapperName("%sDao");
//     gc.setXmlName("%sDao");
//     gc.setServiceName("MP%sService");
//     gc.setServiceImplName("%sServiceDiy");
//     gc.setControllerName("%sAction");
        mpg.setGlobalConfig(gc);
        
     // 数据源配置
        Properties p = new Properties();
        p.load(TestLP.class.getResourceAsStream("/conf/resources.properties"));
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.ORACLE);//数据库类型
        dsc.setTypeConvert(new OracleTypeConvert());
        dsc.setDriverName(p.getProperty("jdbc_driver"));
        dsc.setUsername(p.getProperty("test.jdbc_username"));
        dsc.setPassword(p.getProperty("test.jdbc_password"));
        dsc.setUrl(p.getProperty("test.jdbc_url"));
        mpg.setDataSource(dsc);
        
     // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setCapitalMode(false);// 全局大写命名 ORACLE 注意
//        strategy.setTablePrefix(new String[] {"LP_","A_"});// 此处可以修改为您的表前缀(表名以什么开头)
        strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
        strategy.setFieldNaming(NamingStrategy.underline_to_camel);
        strategy.setDbColumnUnderline(true);
        // lp_test,a_settle_a,a_settle_b,a_settle_c
        strategy.setInclude(new String[] {
        "lp_test".toUpperCase(),
        "a_settle_a".toUpperCase(),
        "a_settle_b".toUpperCase(),
        "a_settle_c".toUpperCase()
        }); // 需要生成的表
        // strategy.setExclude(new String[]{"test"}); // 排除生成的表
        // 自定义实体父类
        // 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("core.system.module.controller.IBaseController");
        // 【实体】是否生成字段常量(默认 false)
        // public static final String ID = "test_id";
        // strategy.setEntityColumnConstant(true);
        // 【实体】是否为构建者模型(默认 false)
        // public User setName(String name) {this.name = name; return this;}
        // strategy.setEntityBuliderModel(true);
        mpg.setStrategy(strategy);


        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.sxit.pgc.module." + name);//自定义生成的文件位置,例:此文章生成的位置D:\com\sxit\pgc\module\test
        // pc.setModuleName("demo1");
        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);
            }
        };


        // 自定义 xxList.jsp 生成
        //List<FileOutConfig> focList = new ArrayList<FileOutConfig>();
        //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);


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


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


}

猜你喜欢

转载自blog.csdn.net/a990914093/article/details/80893159
今日推荐