1. 程式人生 > >Mybatis ---自動生成程式碼

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("
[email protected]
"); 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(); } }

 

執行以上程式碼就可直接生成基本的增刪改查等功能程式碼。。