MyBatis-Plus程式碼生成器,根據表名生成各個模組的程式碼
阿新 • • 發佈:2018-11-06
根據表名生成Entity、Mapper、Mapper XML、Service、Controller 等各個模組的程式碼,程式碼如下(根據官網demo修改的),可以正常執行
package com; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException; import com.baomidou.mybatisplus.core.toolkit.StringPool; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.generator.AutoGenerator; import com.baomidou.mybatisplus.generator.InjectionConfig; import com.baomidou.mybatisplus.generator.config.*; import com.baomidou.mybatisplus.generator.config.po.TableInfo; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine; import java.util.ArrayList; import java.util.List; import java.util.Scanner; /** * Created by chenxq on 2018/9/26. */ public class TestCode { /** * <p> * 讀取控制檯內容 * </p> */ 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; } } throw new MybatisPlusException("請輸入正確的" + tip + "!"); } public static void main(String[] args) { // 程式碼生成器 AutoGenerator mpg = new AutoGenerator(); // 全域性配置 GlobalConfig gc = new GlobalConfig(); String projectPath = System.getProperty("user.dir"); gc.setOutputDir(projectPath + "/rd_basics_calculation/src/main/java"); gc.setAuthor("chenxq"); gc.setOpen(false); mpg.setGlobalConfig(gc); // 資料來源配置 DataSourceConfig dsc = new DataSourceConfig(); // dsc.setUrl("jdbc:mysql://localhost:3306/jnrd?serverTimezone=GMT%2B8");//8.0 dsc.setUrl("jdbc:sqlserver://172.136.10.103:1433;DatabaseName=haixinData");//8.0 // dsc.setSchemaName("public"); // dsc.setDriverName("com.mysql.cj.jdbc.Driver");//8.0 dsc.setDriverName("com.microsoft.sqlserver.jdbc.SQLServerDriver");// dsc.setDbType(DbType.SQL_SERVER);//設定資料來源,mysql不設定也能使用,SQLserver需要設定 // dsc.setUsername("sa"); // dsc.setPassword("123456"); dsc.setUsername("sa");//資料庫使用者名稱 dsc.setPassword("");//資料庫密碼 mpg.setDataSource(dsc); // 包配置 PackageConfig pc = new PackageConfig(); pc.setModuleName(scanner("模組名")); pc.setParent("org.jn"); // pc.setService("");//設定生成的service名 mpg.setPackageInfo(pc); // 自定義配置 InjectionConfig cfg = new InjectionConfig() { @Override public void initMap() { // to do nothing } }; List<FileOutConfig> focList = new ArrayList<>(); focList.add(new FileOutConfig("/templates/mapper.xml.ftl") { @Override public String outputFile(TableInfo tableInfo) { // 自定義輸入檔名稱 return projectPath + "/src/main/resources/mapper/" + pc.getModuleName() + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML; } }); cfg.setFileOutConfigList(focList); mpg.setCfg(cfg); mpg.setTemplate(new TemplateConfig().setXml(null)); // 策略配置 StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); // strategy.setSuperEntityClass("com.baomidou.ant.common.BaseEntity");//繼承的實體類 strategy.setEntityLombokModel(true); strategy.setRestControllerStyle(true); strategy.setSuperControllerClass("org.jn.rd.basics.controller.BaseController");//繼承的Controller strategy.setInclude(scanner("表名")); // strategy.setSuperEntityColumns("id"); strategy.setControllerMappingHyphenStyle(true); strategy.setTablePrefix(pc.getModuleName() + "_"); mpg.setStrategy(strategy); mpg.setTemplateEngine(new FreemarkerTemplateEngine()); mpg.execute(); } }
pom.xml依賴:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.1</version>
</dependency>
執行程式碼截圖:
生成專案結構: