Mybatis ---自动生成代码

今天用到了自动代码生成功能,特记录下来,以备后用。下面代码为链接DB2数据库的。

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
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.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import gen.db2code.Db2Query;
import gen.db2code.Db2TypeConvert;

public class AmsGenerator {
    /**
     * <p>
     * DB2 生成演示
     * </p>
     */
    public static void main(String[] args) {
        AutoGenerator mpg = new AutoGenerator();
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(".\\src\\main\\java\\cn\\ghac\\epc\\api\\gen");
        gc.setFileOverride(true);
        gc.setActiveRecord(false);// 不需要ActiveRecord特性的请改为false
        gc.setEnableCache(false);// XML 二级缓存
        gc.setBaseResultMap(true);// XML ResultMap
        gc.setBaseColumnList(false);// XML columList
        gc.setKotlin(false); //是否生成 kotlin 代码
        gc.setAuthor("epc");

        // 自定义文件命名,注意 %s 会自动填充表实体属性!
        gc.setMapperName("%sDaoGen");
        gc.setXmlName("%sMapperGen");
        gc.setServiceName("%sServiceGen");
        gc.setServiceImplName("%sServiceImplGen");
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();

        //因为目前不支持db2数据库,这里需要自己重写一个DbQuery,然后注入进来
        dsc.setDbQuery(new Db2Query());

        dsc.setDbType(DbType.OTHER);

        dsc.setTypeConvert(new Db2TypeConvert());
        /*dsc.setTypeConvert(new Db2TypeConvert(){
            // 自定义数据库表字段类型转换【可选】
            @Override
            public DbColumnType processTypeConvert(String fieldType) {
                // 注意!!processTypeConvert 存在默认类型转换,如果不是你要的效果请自定义返回、非如下直接返回。
                String t = fieldType.toLowerCase();
                if (t.contains("long varchar")) {
                    return DbColumnType.STRING;
                }
                return super.processTypeConvert(fieldType);
            }
        });*/
        //com.mysql.jdbc.Driver
        dsc.setDriverName("com.ibm.db2.jcc.DB2Driver");
        dsc.setUsername("test");
        dsc.setPassword("test@2018");
        dsc.setUrl("jdbc:db2://11.111.66.66:66666/test:currentSchema=TEST;");
        mpg.setDataSource(dsc);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        // strategy.setCapitalMode(true);// 全局大写命名 ORACLE 注意
        strategy.setTablePrefix(new String[] {  });// 此处可以修改为您的表前缀
        strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
        strategy.setInclude(new String[] {"CO_FTMAST"}); // 需要生成的表
        //strategy.setExclude(new String[]{"W_CM_SSO_USER","W_CM_SSO_USER2"}); // 排除生成的表
        // 自定义实体父类
        strategy.setSuperEntityClass("cn.ghac.epc.entity.BaseEntity");
        // 自定义实体,公共字段
        //strategy.setSuperEntityColumns(new String[] { "CREATE_TIME", "CREATE_USER","UPDATE_TIME","UPDATE_USER","RECORD_VERSION" });        // 自定义 mapper 父类
        // strategy.setSuperMapperClass("com.baomidou.demo.TestMapper");
        // 自定义 service 父类
        // strategy.setSuperServiceClass("com.baomidou.demo.TestService");
        // 自定义 service 实现类父类
        //strategy.setSuperServiceImplClass("cn.ghac.ams.api.service.impl.BaseService");
        // 自定义 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("cn.ghac");
        pc.setModuleName("epc");
        pc.setService("service.gen");
        pc.setServiceImpl("service.impl.gen");
        pc.setMapper("dao.gen");
        pc.setController("tobedeleted");
        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);
//
//        // 调整 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);

        // 自定义模板配置,可以 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();

    }

}

运行以上代码就可直接生成基本的增删改查等功能代码。。

猜你喜欢

转载自blog.csdn.net/Zhang_521521/article/details/81358016