mybatis plus generator配置
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                mybatis plus generator配置
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.                        
                                mybatis plus generator配置
代碼生成器AutoGenerator
AutoGenerator autoGenerator = new AutoGenerator();GlobalConfig
全局配置,定義文件的輸出目錄,設置文件的@author信息等公共配置
GlobalConfig gc = new GlobalConfig(); String projectPath = System.getProperty("user.dir");// 獲取用戶程序當前路徑(項目根的路徑) gc.setOutputDir(projectPath + "/src/main/java");// 生成文件的輸出目錄(默認值:D 盤根目錄) gc.setAuthor("yourName");// 開發(fā)人員(默認值:null) gc.setOpen(false);// 是否打開輸出目錄(默認值:null) gc.setFileOverride(true);// 是否覆蓋已有文件(默認值:false) autoGenerator.setGlobalConfig(gc);//把全局配置添加到代碼生成器主類DataSourceConfig
DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:mysql://xxx.xxx.x.x:3306/xxx?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8"); //dsc.setSchemaName("public");// 數(shù)據(jù)庫方案名 dsc.setDbType(DbType.MYSQL);// 數(shù)據(jù)庫類型 dsc.setDriverName("com.mysql.cj.jdbc.Driver");// 驅動名稱 dsc.setUsername("xxx"); // 用戶名 dsc.setPassword("xxx"); // 密碼autoGenerator.setDataSource(dsc);//把數(shù)據(jù)源配置添加到代碼生成器主類PackageConfig
包配置,就是每個類最上面的package,比如:package com.ck.mybatisplus.foundation.service;這里包配置可以分別配置service、entity、controller等等
PackageConfig pc = new PackageConfig(); pc.setModuleName("foundation");// 父包模塊名 pc.setParent("com.ck.mybatisplus");// 父包名。如果為空,將下面子包名必須寫全部, 否則就只需寫子包名 pc.setService("service");// Service包名 pc.setEntity("entity");// Entity包名 pc.setServiceImpl("service.impl");// ServiceImpl包名 pc.setMapper("mapper");// Mapper包名 pc.setController("controller");// Contoller包名 // pc.setXml("mapper.xml");// Mapper.xml包名 mpg.setPackageInfo(pc);// 把包配置添加到代碼生成器主類TemplateConfig
方法一
設置自定義模板路徑,文件配置信息使用默認的。(如:生成的文件名,生成的文件路徑等),一般都是用mybatis plus中自帶的模板
//不用帶上.ftl/.vm, 會根據(jù)使用的模板引擎自動識別 templateConfig.setEntity("templates/entity.java"); templateConfig.setService("templates/service.java"); templateConfig.setController();方法二
使用自定義輸出配置
// 自定義輸出配置 List<FileOutConfig> focList = new ArrayList<>(); // 自定義配置會被優(yōu)先輸出 focList.add(new FileOutConfig(templatePath) {@Overridepublic String outputFile(TableInfo tableInfo) {return projectPath + "/src/main/resources/mapper/mysql/" + pc.getModuleName()+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;} });StrategyConfig
一些策略的配置
// 策略配置 StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); // strategy.setSuperEntityClass("你自己的父類實體,沒有就不用設置!"); strategy.setEntityLombokModel(true); strategy.setRestControllerStyle(true); // 公共父類 // strategy.setSuperControllerClass("你自己的父類控制器,沒有就不用設置!"); // 寫于父類中的公共字段 // strategy.setSuperEntityColumns("id"); strategy.setInclude(INCLUDE_TABLES.split(",")); strategy.setControllerMappingHyphenStyle(true); // strategy.setTablePrefix(pc.getModuleName() + "_"); autoGenerator.setStrategy(strategy); autoGenerator.setTemplateEngine(new FreemarkerTemplateEngine());生成器工具類
自己寫的一個生成器工具類,可以參考一下,根據(jù)自己的需求進行修改
import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.core.toolkit.StringPool; 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.*;/*** @author wenyao*/ public class CodeGenerator {private final static AutoGenerator autoGenerator = new AutoGenerator();//表名映射(','號分割符)private static String INCLUDE_TABLES = "user";//private static String INCLUDE_TABLES = "user,home";//包名配置private static String CONTROLLER = "controller";private static String SERVICE = "service";private static String SERVICEIMPL = "service.Impl";private static String MAPPER = "mapper";private static String ENTITY = "entity";private static String projectPath = System.getProperty("user.dir");// 獲取用戶程序當前路徑(項目根的路徑)private static String ParentName = "com.wenyao";private static String ModuleName = "";//公共全局private static String AUTHOR = "wenyao";//數(shù)據(jù)源配置private static String DB_USERNAME = "root";private static String DB_PASSWORD = "xxx";private static String DB_DRIVER = "com.mysql.cj.jdbc.Driver";private static String DB_URL = "jdbc:mysql://localhost:3306/mybatis_plus_test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8";//xml模板文件路徑private static String TEMPLATEPATH = "/templates/mapper.xml.ftl";//配置private static GlobalConfig gc = new GlobalConfig();private static DataSourceConfig dsc = new DataSourceConfig();private static PackageConfig pc = new PackageConfig();private static InjectionConfig cfg = new InjectionConfig() {@Overridepublic void initMap() {// to do nothing}};private static TemplateConfig templateConfig = new TemplateConfig();private static StrategyConfig strategy = new StrategyConfig();/*** 初始化全局配置*/private static void initGlobalConfig(){// 全局配置gc.setOutputDir(projectPath + "/src/main/java");// 生成文件的輸出目錄(默認值:D 盤根目錄)gc.setAuthor(AUTHOR);// 開發(fā)人員(默認值:null)gc.setOpen(false);// 是否打開輸出目錄(默認值:null)gc.setFileOverride(true);// 是否覆蓋已有文件(默認值:false)gc.setSwagger2(true);// 實體屬性 Swagger2 注解gc.setBaseResultMap(true); //在xml中生成BaseResultMapgc.setBaseColumnList(true); //autoGenerator.setGlobalConfig(gc);//把全局配置添加到代碼生成器主類}/*** 初始化數(shù)據(jù)源配置*/private static void initDataSourceConfig(){// 數(shù)據(jù)源配置dsc.setUrl(DB_URL);//dsc.setSchemaName("public");// 數(shù)據(jù)庫方案名dsc.setDbType(DbType.MYSQL);// 數(shù)據(jù)庫類型dsc.setDriverName(DB_DRIVER);// 驅動名稱dsc.setUsername(DB_USERNAME); // 用戶名dsc.setPassword(DB_PASSWORD); // 密碼autoGenerator.setDataSource(dsc);//把數(shù)據(jù)源配置添加到代碼生成器主類}/*** 初始化包配置*/private static void initPackageConfig(){// 包配置pc.setModuleName(ModuleName);// 父包模塊名pc.setParent(ParentName);// 父包名。如果為空,將下面子包名必須寫全部, 否則就只需寫子包名pc.setService(SERVICE);// Service包名pc.setEntity(ENTITY);// Entity包名pc.setServiceImpl(SERVICEIMPL);// ServiceImpl包名pc.setMapper(MAPPER);// Mapper包名pc.setController(CONTROLLER);// Contoller包名// pc.setXml("mapper.xml");// Mapper.xml包名(自定義配置到resource下,所以這里不使用默認路徑)autoGenerator.setPackageInfo(pc);// 把包配置添加到代碼生成器主類}/*** 初始化自定義配置* 這里配置生成的mapper.xml文件的自定義輸出路徑(默認不是輸出到resources文件夾下,所以需要自定義)*/private static void initInjectionConfig(){// 自定義配置// 自定義輸出配置,自定義配置會被優(yōu)先輸出List<FileOutConfig> focList = new ArrayList<>();focList.add(new FileOutConfig(TEMPLATEPATH) {@Overridepublic String outputFile(TableInfo tableInfo) {// 自定義輸出文件名 , 如果你 Entity 設置了前后綴、此處注意 xml 的名稱會跟著發(fā)生變化!!return projectPath + "/src/main/resources/mapper/mysql/" + pc.getModuleName()+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;}});cfg.setFileOutConfigList(focList);autoGenerator.setCfg(cfg);}/*** 初始化模板配置*/private static void initTemplateConfig(){// 配置模板templateConfig.setXml(null);autoGenerator.setTemplate(templateConfig);}/*** 生成策略初始化*/private static void initStrategyConfig(){// 策略配置strategy.setNaming(NamingStrategy.underline_to_camel);strategy.setColumnNaming(NamingStrategy.underline_to_camel);// strategy.setSuperEntityClass("你自己的父類實體,沒有就不用設置!");strategy.setEntityLombokModel(true);strategy.setRestControllerStyle(true);// 公共父類// strategy.setSuperControllerClass("你自己的父類控制器,沒有就不用設置!");// 寫于父類中的公共字段// strategy.setSuperEntityColumns("id");strategy.setInclude(INCLUDE_TABLES.split(","));strategy.setControllerMappingHyphenStyle(true);// strategy.setTablePrefix(pc.getModuleName() + "_");autoGenerator.setStrategy(strategy);autoGenerator.setTemplateEngine(new FreemarkerTemplateEngine());}public static void init(){initGlobalConfig();initDataSourceConfig();initPackageConfig();initInjectionConfig();initTemplateConfig();initStrategyConfig();}public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {init();autoGenerator.execute();}} 與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的mybatis plus generator配置的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: mybatis plus 链式编程查询
- 下一篇: 炒鸡内金粉的功效与作用、禁忌和食用方法
