package com.company.project.generator; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.annotation.FieldFill; import com.baomidou.mybatisplus.annotation.IdType; 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.TableFill; import com.baomidou.mybatisplus.generator.config.po.TableInfo; import com.baomidou.mybatisplus.generator.config.rules.DateType; 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.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class MyBatisPlusGeneratorAnt { public static void main(String[] args) { String projectPath = System.getProperty("user.dir"); // 自定義需要填充的字段 List<TableFill> tableFillList = new ArrayList<TableFill>(); //如 每張表都有一個創建時間、修改時間 //而且這基本上就是通用的了,新增時,創建時間和修改時間同時修改 //修改時,修改時間會修改, //雖然像Mysql數據庫有自動更新幾只,但像ORACLE的數據庫就沒有了, //使用公共字段填充功能,就可以實現,自動按場景更新了。 //如下是配置 TableFill sysCreateTime = new TableFill("create_time", FieldFill.INSERT); TableFill sysUpdateTime = new TableFill("update_time", FieldFill.UPDATE); TableFill sysCreateBy = new TableFill("create_by", FieldFill.INSERT); TableFill sysUpdateBy = new TableFill("update_by", FieldFill.UPDATE); tableFillList.add(sysCreateTime); tableFillList.add(sysUpdateTime); tableFillList.add(sysCreateBy); tableFillList.add(sysUpdateBy); // 1. 全局配置 GlobalConfig config = new GlobalConfig(); // 是否支持AR模式 config.setActiveRecord(true) // 作者 .setAuthor("test@company.com") // 生成路徑 .setOutputDir(projectPath + "/src/main/java/") // 文件覆蓋 .setFileOverride(true) // 主鍵策略 .setIdType(IdType.AUTO) // 設置生成的service接口的名字的首字母是否為I,例如IEmployeeService .setServiceName("%sService") //生成基本的resultMap .setBaseResultMap(true) //生成基本的SQL片段 .setBaseColumnList(true) //生成后打開文件夾 .setOpen(false).setDateType(DateType.ONLY_DATE); // 2. 數據源配置 DataSourceConfig dsConfig = new DataSourceConfig(); // 設置數據庫類型 dsConfig.setDbType(DbType.MYSQL) .setDriverName("com.mysql.cj.jdbc.Driver") .setUrl("jdbc:mysql://localhost:3306/test") .setUsername("root") .setPassword("root") .setTypeConvert(new MySqlTypeConvert() { // 自定義數據庫表字段類型轉換【可選】 @Override public IColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) { System.out.println("轉換類型:" + fieldType); if (fieldType.toLowerCase().contains("tinyint")) { return DbColumnType.INTEGER; } return super.processTypeConvert(globalConfig, fieldType); } }); // 3. 策略配置globalConfiguration中 StrategyConfig stConfig = new StrategyConfig(); // 全局大寫命名 stConfig.setCapitalMode(true) // 指定表名 字段名是否使用下划線 //.setDbColumnUnderline(true) // 數據庫表映射到實體的命名策略 .setNaming(NamingStrategy.underline_to_camel) //.setTablePrefix("tbl_") // 生成的表 .setInclude(new String[] { "table_teacher","table_student" }) .setEntityBooleanColumnRemoveIsPrefix(false) // 自定義實體,公共字段 .setTableFillList(tableFillList); // 4. 包名策略配置 PackageConfig pkConfig = new PackageConfig(); pkConfig.setParent("com.company.project.api") //dao .setMapper("repository.mysql.mapper") //servcie .setService("service") //controller .setController("web") .setEntity("repository.mysql.domain") //mapper.xml .setXml("repository.mysql.mybatis"); // 注入自定義配置,可以在 VM 中使用 cfg.abc 【可無】 InjectionConfig cfg = new InjectionConfig() { @Override public void initMap() { Map<String, Object> map = new HashMap<>(); map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp"); this.setMap(map); } }; // 自定義輸出文件目錄 List<FileOutConfig> focList = new ArrayList<>(); // 調整xml生成目錄演示 focList.add(new FileOutConfig("/templates/mapper.xml.vm") { @Override public String outputFile(TableInfo tableInfo) { return projectPath + "/src/main/resources/META-INF/mybatis/" + tableInfo.getEntityName() + "Mapper.xml"; } }); cfg.setFileOutConfigList(focList); // 關閉默認生成,如果設置空 OR Null 將不生成該模塊。 TemplateConfig tc = new TemplateConfig(); tc.setController(null); tc.setXml(null); // 5. 整合配置 AutoGenerator ag = new AutoGenerator(); ag.setGlobalConfig(config) .setDataSource(dsConfig) .setStrategy(stConfig) .setPackageInfo(pkConfig) .setCfg(cfg) .setTemplate(tc); // 6. 執行 ag.execute(); } }