mybatis-plus提供了代碼自動生成器;使用代碼生成器可以根據(jù)數(shù)據(jù)庫表結(jié)構生成我們需要的代碼結(jié)構,節(jié)省了我們建各種類的時間。
以下是集成mybatis-plus代碼生成器的具體配置:
pom.xml
com.baomidou mybatis-plus-generator ${mybatis-plus.version} compile
代碼生成器工具類
public class CodeGeneration { public static String scanner (String tip){ Scanner scanner = new Scanner(System.in); StringBuilder help = new StringBuilder(); help.append(“請輸入” + tip + “:”); System.out.println(help.toString()); if (scanner.hasNext()) { String ipt = scanner.next(); /*if (StringUtils.isNotEmpty(ipt)) { return ipt; }*/ if (ipt.length()>0) { return ipt; } } throw new MybatisPlusException(“請輸入正確的” + tip + “!”); } public static void main (String[]args){ // 代碼生成器 AutoGenerator mpg = new AutoGenerator(); // 全局配置 GlobalConfig gc = new GlobalConfig(); String projectPath = “D:/JAVA/crmvideo/src/main/java”; gc.setOutputDir(projectPath); gc.setAuthor(“wlx”); gc.setOpen(false); mpg.setGlobalConfig(gc); // 數(shù)據(jù)源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl(“jdbc:mysql://127.0.0.1:3306/crmvideo?characterEncoding=utf8&serverTimezone=UTC”); dsc.setDriverName(“com.mysql.jdbc.Driver”); dsc.setUsername(“root”); dsc.setPassword(“123456”); mpg.setDataSource(dsc); // 包配置 PackageConfig pc = new PackageConfig(); pc.setModuleName(scanner(“模塊名”)); pc.setParent(“com.wangkai”); pc.setController(“controller”); pc.setService(“service”); pc.setServiceImpl(“serviceImpl”); pc.setMapper(“mapper”); pc.setEntity(“entity”); pc.setXml(“xml”); mpg.setPackageInfo(pc); // 自定義配置 InjectionConfig cfg = new InjectionConfig() { @Override public void initMap() { // to do nothing } }; // 如果模板引擎是 freemarker String templatePath = “/templates/mapper.xml.ftl”; // 自定義輸出配置 List focList = new ArrayList(); // 自定義配置會被優(yōu)先輸出 focList.add(new FileOutConfig(templatePath) { @Override public String outputFile(TableInfo tableInfo) { return projectPath + “/com/wangkai/” + pc.getModuleName() + “/xml/” +tableInfo.getEntityName() + “Mapper” + StringPool.DOT_XML; } }); cfg.setFileOutConfigList(focList); mpg.setCfg(cfg); // 配置模板 TemplateConfig templateConfig = new TemplateConfig(); // 配置自定義輸出模板 //指定自定義模板路徑,注意不要帶上.ftl/.vm, 會根據(jù)使用的模板引擎自動識別 // templateConfig.setEntity(“templates/entity2.java”); // templateConfig.setService(); // templateConfig.setController(); templateConfig.setXml(null); mpg.setTemplate(templateConfig); // 策略配置 StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); strategy.setEntityLombokModel(true); strategy.setRestControllerStyle(true); strategy.setSuperEntityColumns(“id”); strategy.setInclude(scanner(“表名,多個英文逗號分割”).split(“,”)); strategy.setControllerMappingHyphenStyle(true); strategy.setTablePrefix(pc.getModuleName() + “_”); mpg.setStrategy(strategy); mpg.setTemplateEngine(new FreemarkerTemplateEngine()); mpg.execute(); }}
代碼生成器生成的代碼結(jié)構
代碼生成器生成的代碼結(jié)構