Mybatis-Plus 3.0代碼生成器


  1 package com.kyplatform.generator;
  2 
  3 import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
  4 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
  5 import com.baomidou.mybatisplus.generator.AutoGenerator;
  6 import com.baomidou.mybatisplus.generator.config.*;
  7 import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
  8 
  9 import java.util.Scanner;
 10 
 11 /**
 12  * mybatis代碼生成器
 13  */
 14 public class CodeGenerator {
 15     /**
 16      * <p>
 17      * 讀取控制台內容
 18      * </p>
 19      */
 20     public static String scanner(String tip) {
 21         Scanner scanner = new Scanner(System.in);
 22         StringBuilder help = new StringBuilder();
 23         help.append("請輸入" + tip + ":");
 24         System.out.println(help.toString());
 25         if (scanner.hasNext()) {
 26             String ipt = scanner.next();
 27             if (StringUtils.isNotEmpty(ipt)) {
 28                 return ipt;
 29             }
 30         }
 31         throw new MybatisPlusException("請輸入正確的" + tip + "!");
 32     }
 33 
 34     public static void main(String[] args) {
 35         // 代碼生成器
 36         AutoGenerator mpg = new AutoGenerator();
 37 
 38         // 全局配置
 39         GlobalConfig gc = new GlobalConfig();
 40         String projectPath = System.getProperty("user.dir");
 41         gc.setOutputDir(projectPath + "/src/main/java");//生成文件的輸出目錄
 42         gc.setAuthor("zhicaili");//開發人員
 43         gc.setOpen(true);//是否打開輸出目錄
 44         gc.setServiceName("%sService");//service 命名方式
 45         gc.setServiceImplName("%sServiceImpl");//service impl 命名方式
 46         // 自定義文件命名,注意 %s 會自動填充表實體屬性!
 47         gc.setMapperName("%sMapper");
 48         gc.setXmlName("%sMapper");
 49         gc.setFileOverride(true);
 50         gc.setActiveRecord(true);
 51         gc.setEnableCache(false);// XML 二級緩存
 52         gc.setBaseResultMap(true);// XML ResultMap
 53         gc.setBaseColumnList(false);// XML columList
 54         mpg.setGlobalConfig(gc);
 55 
 56         // 數據源配置
 57         DataSourceConfig dsc = new DataSourceConfig();
 58         dsc.setUrl("jdbc:mysql://127.0.0.1:3306/xxxxx?useUnicode=true&useSSL=false&characterEncoding=utf8");
 59         // dsc.setSchemaName("public"); 數據庫 schema name
 60         dsc.setDriverName("com.mysql.jdbc.Driver");
 61         dsc.setUsername("root");
 62         dsc.setPassword("******");
 63         mpg.setDataSource(dsc);
 64 
 65         // 包配置
 66         PackageConfig pc = new PackageConfig();
 67         //pc.setModuleName(scanner("模塊名"));//父包模塊名
 68         pc.setParent("com.kyplatform.admin");//父包名。// 自定義包路徑  如果為空,將下面子包名必須寫全部, 否則就只需寫子包名
 69         pc.setEntity("pojo");
 70         pc.setService("service");
 71         pc.setServiceImpl("service.impl");
 72         pc.setController("controller");//設置控制器包名
 73         mpg.setPackageInfo(pc);
 74 
 75         // 自定義配置
 76 /*        InjectionConfig cfg = new InjectionConfig() {
 77             @Override
 78             public void initMap() {
 79                 // to do nothing
 80             }
 81         };
 82         List<FileOutConfig> focList = new ArrayList<>();
 83         focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
 84             @Override
 85             public String outputFile(TableInfo tableInfo) {
 86                 // 自定義輸入文件名稱
 87                 return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
 88                         + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
 89             }
 90         });*/
 91  /*       cfg.setFileOutConfigList(focList);
 92         mpg.setCfg(cfg);*/
 93         mpg.setTemplate(new TemplateConfig().setXml(null));
 94 
 95         // 策略配置
 96         StrategyConfig strategy = new StrategyConfig();
 97         strategy.setNaming(NamingStrategy.underline_to_camel);//數據庫表映射到實體的命名策略
 98         strategy.setColumnNaming(NamingStrategy.underline_to_camel);//數據庫表字段映射到實體的命名策略, 未指定按照 naming 執行
 99       //  strategy.setSuperEntityClass("com.baomidou.ant.common.BaseEntity");//自定義繼承的Entity類全稱,帶包名
100         strategy.setEntityLombokModel(true);//【實體】是否為lombok模型(默認 false)
101         strategy.setRestControllerStyle(true);//生成 @RestController 控制器
102         //strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController");//自定義繼承的Controller類全稱,帶包名
103         strategy.setInclude("tb_user","tb_organization","tb_person","tb_signin","tb_sys_config","tb_sys_log");//需要包含的表名,允許正則表達式
104         //strategy.setSuperEntityColumns("id");//自定義基礎的Entity類,公共字段
105         strategy.setControllerMappingHyphenStyle(true);//駝峰轉連字符
106         strategy.setTablePrefix("tb_");//表前綴
107         mpg.setStrategy(strategy);
108         //mpg.setTemplateEngine(new FreemarkerTemplateEngine());
109         mpg.execute();
110     }
111 
112 
113 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM