SpringBoot結(jié)合MyBatis Plus 自動生成代碼
本來這一章要介紹Redis+AOP優(yōu)化權(quán)限懂算,可是發(fā)現(xiàn)還是需要先介紹一些MyBatis Plus自動生成代碼
MyBatis Plus簡介
MyBatis-Plus (opens new window)(簡稱 MP)是一個 MyBatis (opens new window)的增強(qiáng)工具,在 MyBatis 的基礎(chǔ)上只做增強(qiáng)不做改變辙谜,為簡化開發(fā)邦马、提高效率而生抡柿。
MyBatis Plus特性
- 無侵入:只做增強(qiáng)不做改變冲甘,引入它不會對現(xiàn)有工程產(chǎn)生影響捷雕,如絲般順滑
- 損耗小:啟動即會自動注入基本 CURD湾蔓,性能基本無損耗瘫析,直接面向?qū)ο蟛僮?/li>
- 強(qiáng)大的 CRUD 操作:內(nèi)置通用 Mapper、通用 Service默责,僅僅通過少量配置即可實(shí)現(xiàn)單表大部分 CRUD 操作贬循,更有強(qiáng)大的條件構(gòu)造器,滿足各類使用需求
- 支持 Lambda 形式調(diào)用:通過 Lambda 表達(dá)式桃序,方便的編寫各類查詢條件杖虾,無需再擔(dān)心字段寫錯
- 支持主鍵自動生成:支持多達(dá) 4 種主鍵策略(內(nèi)含分布式唯一 ID 生成器 - Sequence),可自由配置葡缰,完美解決主鍵問題
- 支持 ActiveRecord 模式:支持 ActiveRecord 形式調(diào)用亏掀,實(shí)體類只需繼承 Model 類即可進(jìn)行強(qiáng)大的 CRUD 操作
- 支持自定義全局通用操作:支持全局通用方法注入( Write once, use anywhere )
- 內(nèi)置代碼生成器:采用代碼或者 Maven 插件可快速生成 Mapper 、 Model 泛释、 Service 滤愕、 Controller 層代碼,支持模板引擎怜校,更有超多自定義配置等您來使用
- 內(nèi)置分頁插件:基于 MyBatis 物理分頁间影,開發(fā)者無需關(guān)心具體操作,配置好插件之后茄茁,寫分頁等同于普通 List 查詢
- 分頁插件支持多種數(shù)據(jù)庫:支持 MySQL魂贬、MariaDB、Oracle裙顽、DB2付燥、H2、HSQL愈犹、SQLite键科、Postgre、SQLServer 等多種數(shù)據(jù)庫
- 內(nèi)置性能分析插件:可輸出 Sql 語句以及其執(zhí)行時間漩怎,建議開發(fā)測試時啟用該功能勋颖,能快速揪出慢查詢
- 內(nèi)置全局?jǐn)r截插件:提供全表 delete 、 update 操作智能分析阻斷勋锤,也可自定義攔截規(guī)則饭玲,預(yù)防誤操作
MyBatis Plus支持?jǐn)?shù)據(jù)庫
任何能使用 mybatis 進(jìn)行 crud, 并且支持標(biāo)準(zhǔn) sql 的數(shù)據(jù)庫
框架結(jié)構(gòu)
MyBatis Plus代碼生成器
AutoGenerator 是 MyBatis-Plus 的代碼生成器,通過 AutoGenerator 可以快速生成 Entity叁执、Mapper茄厘、Mapper XML矮冬、Service、Controller 等各個模塊的代碼蚕断,極大的提升了開發(fā)效率欢伏。
使用教程
添加 代碼生成器 依賴
<!--lombok依賴-->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.projectlombok</groupId>
? ? ? ? ? ? <artifactId>lombok</artifactId>
? ? ? ? ? ? <optional>true</optional>
? ? ? ? </dependency>
? ? ? ? <!--集成druid連接池-->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>com.alibaba</groupId>
? ? ? ? ? ? <artifactId>druid-spring-boot-starter</artifactId>
? ? ? ? ? ? <version>1.1.10</version>
? ? ? ? </dependency>
? ? ? ? <!--Mysql數(shù)據(jù)庫驅(qū)動-->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>mysql</groupId>
? ? ? ? ? ? <artifactId>mysql-connector-java</artifactId>
? ? ? ? ? ? <version>5.1.10</version>
? ? ? ? </dependency>
? ? ? ? <!--MyBatis Plus 依賴-->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>com.baomidou</groupId>
? ? ? ? ? ? <artifactId>mybatis-plus-boot-starter</artifactId>
? ? ? ? ? ? <version>3.3.2</version>
? ? ? ? </dependency>
? ? ? ? <!--MyBatis Plus 代碼生成器-->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>com.baomidou</groupId>
? ? ? ? ? ? <artifactId>mybatis-plus-generator</artifactId>
? ? ? ? ? ? <version>3.3.2</version>
? ? ? ? </dependency>
? ? ? ? <!--Hutool Java工具包-->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>cn.hutool</groupId>
? ? ? ? ? ? <artifactId>hutool-all</artifactId>
? ? ? ? ? ? <version>4.5.7</version>
? ? ? ? </dependency>
? ? ? ? <!--Velocity模板引擎-->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>org.apache.velocity</groupId>
? ? ? ? ? ? <artifactId>velocity-engine-core</artifactId>
? ? ? ? ? ? <version>2.2</version>
? ? ? ? </dependency>
? ? ? ? <!--Swagger-UI API文檔生產(chǎn)工具-->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>io.springfox</groupId>
? ? ? ? ? ? <artifactId>springfox-swagger2</artifactId>
? ? ? ? ? ? <version>2.7.0</version>
? ? ? ? </dependency>
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>io.springfox</groupId>
? ? ? ? ? ? <artifactId>springfox-swagger-ui</artifactId>
? ? ? ? ? ? <version>2.7.0</version>
? ? ? ? </dependency>
添加 模板引擎 依賴
MyBatis-Plus 支持 Velocity(默認(rèn))入挣、Freemarker亿乳、Beetl,用戶可以選擇自己熟悉的模板引擎径筏,如果都不滿足您的要求葛假,可以采用自定義模板引擎。本文使用默認(rèn)依賴
<dependency>
? ? <groupId>org.apache.velocity</groupId>
? ? <artifactId>velocity-engine-core</artifactId>
? ? <version>2.2</version>
</dependency>
編寫配置
MyBatis-Plus 的代碼生成器提供了大量的自定義參數(shù)供用戶選擇滋恬,能夠滿足絕大部分人的使用需求聊训。
配置GlobalConfig
全局策略 globalConfig 配置
outputDir
- 生成文件的輸出目錄
- 默認(rèn)值:D 盤根目錄
fileOverride
- 是否覆蓋已有文件
- 默認(rèn)值:false
open
- 是否打開輸出目錄
- 默認(rèn)值:true
enableCache
- 是否在xml中添加二級緩存配置
- 默認(rèn)值:false
author
- 開發(fā)人員
- 默認(rèn)值:null
kotlin
- 開啟 Kotlin 模式
- 默認(rèn)值:false
swagger2
- 開啟 swagger2 模式
- 默認(rèn)值:false
activeRecord
- 開啟 ActiveRecord 模式
- 默認(rèn)值:false
baseResultMap
- 開啟 BaseResultMap
- 默認(rèn)值:false
baseColumnList
- 開啟 baseColumnList
- 默認(rèn)值:false
dateType
- 時間類型對應(yīng)策略
- 默認(rèn)值:TIME_PACK
注意事項(xiàng):
如下配置 %s 為占位符
entityName
- 實(shí)體命名方式
- 默認(rèn)值:null 例如:%sEntity 生成 UserEntity
mapperName
- mapper 命名方式
- 默認(rèn)值:null 例如:%sDao 生成 UserDao
xmlName
- Mapper xml 命名方式
- 默認(rèn)值:null 例如:%sDao 生成 UserDao.xml
serviceName
- service 命名方式
- 默認(rèn)值:null 例如:%sBusiness 生成 UserBusiness
serviceImplName
- service impl 命名方式
- 默認(rèn)值:null 例如:%sBusinessImpl 生成 UserBusinessImpl
controllerName
- controller 命名方式
- 默認(rèn)值:null 例如:%sAction 生成 UserAction
idType
- 指定生成的主鍵的ID類型
- 默認(rèn)值:null
/**
? ? * 全局配置
? ? * @param projectPath
? ? * @return
? ? */
? ? public static GlobalConfig initGlobal(String projectPath){
? ? ? ? GlobalConfig gc = new GlobalConfig();
? ? ? ? gc.setOutputDir(projectPath + "/src/main/java");
? ? ? ? gc.setAuthor("zbb");
? ? ? ? gc.setOpen(false);
? ? ? ? gc.setSwagger2(true);
? ? ? ? gc.setBaseResultMap(true);
? ? ? ? gc.setFileOverride(true);
? ? ? ? gc.setDateType(DateType.ONLY_DATE);
? ? ? ? gc.setEntityName("%s");
? ? ? ? gc.setMapperName("%sMapper");
? ? ? ? gc.setXmlName("%sMapper");
? ? ? ? gc.setServiceName("%sService");
? ? ? ? gc.setServiceImplName("%sServiceImpl");
? ? ? ? gc.setControllerName("%sController");
? ? ? ? return gc;
? ? }
配置 DataSourceConfig
dbQuery
- 數(shù)據(jù)庫信息查詢類
- 默認(rèn)由 dbType 類型決定選擇對應(yīng)數(shù)據(jù)庫內(nèi)置實(shí)現(xiàn)實(shí)現(xiàn) IDbQuery 接口自定義數(shù)據(jù)庫查詢 SQL 語句 定制化返回自己需要的內(nèi)容
dbType
- 數(shù)據(jù)庫類型
- 該類內(nèi)置了常用的數(shù)據(jù)庫類型【必須】
schemaName
- 數(shù)據(jù)庫 schema name
- 例如 PostgreSQL 可指定為 publictypeConvert
- 類型轉(zhuǎn)換
- 默認(rèn)由 dbType 類型決定選擇對應(yīng)數(shù)據(jù)庫內(nèi)置實(shí)現(xiàn) 實(shí)現(xiàn) ITypeConvert 接口自定義數(shù)據(jù)庫 字段類型 轉(zhuǎn)換為自己需要的 java 類型,內(nèi)置轉(zhuǎn)換類型無法滿足可實(shí)現(xiàn) IColumnType 接口自定義
url
- 驅(qū)動連接的URLdriverName
- 驅(qū)動名稱username
- 數(shù)據(jù)庫連接用戶名password
- 數(shù)據(jù)庫連接密碼
/**
? ? *
? ? * 配置 DataSourceConfig
? ? * @return
? ? */
? ? public? static DataSourceConfig initDataSource(){
? ? ? ? Props props = new Props("application.properties");
? ? ? ? DataSourceConfig dataSourceConfig = new DataSourceConfig();
? ? ? ? dataSourceConfig.setUrl(props.getStr("dataSource.url"));
? ? ? ? dataSourceConfig.setDriverName(props.getStr("dataSource.driverName"));
? ? ? ? dataSourceConfig.setUsername(props.getStr("dataSource.username"));
? ? ? ? dataSourceConfig.setPassword(props.getStr("dataSource.password"));
? ? ? ? return dataSourceConfig;
? ? }
包配置包名配置
parent
- 父包名恢氯。如果為空带斑,將下面子包名必須寫全部, 否則就只需寫子包名
moduleName
- 父包模塊名
entity
- Entity包名
service
- Service包名
serviceImpl
- Service Impl包名
mapper
- Mapper包名
xml
- Mapper XML包名
controller
- Controller包名
pathInfo
- 路徑配置信息
/**
? ? * 包配置
? ? * @param packname
? ? * @return
? ? */
? ? public? static? PackageConfig initPackage(String packname){
? ? ? ? Props props = new Props("application.properties");
? ? ? ? PackageConfig packageConfig = new PackageConfig();
? ? ? ? packageConfig.setModuleName(packname);
? ? ? ? packageConfig.setParent(props.getStr("package.base"));
? ? ? ? packageConfig.setEntity("model");
? ? ? ? return packageConfig;
? ? }
模板配置 TemplateConfig
entity
- Java 實(shí)體類模板
entityKt
- Kotin 實(shí)體類模板
service
- Service 類模板
serviceImpl
- Service impl 實(shí)現(xiàn)類模板
mapper
- mapper 模板
xml
- mapper xml 模板
controller
- controller 控制器模板
/**
? ? * 模板配置
? ? * @return
? ? */
? ? public static TemplateConfig initTemplate() {
? ? ? ? TemplateConfig templateConfig = new TemplateConfig();
? ? ? ? //可以對controller勋拟、service勋磕、entity模板進(jìn)行配置
? ? ? ? templateConfig.setXml(null);
? ? ? ? return templateConfig;
? ? }
自定義屬性注入 InjectionConfig
map
- 自定義返回配置 Map 對象
- 該對象可以傳遞到模板引擎通過 cfg.xxx 引用
fileOutConfigList
- 自定義輸出文件
- 配置 FileOutConfig 指定模板文件、輸出文件達(dá)到自定義文件生成目的
fileCreate
- 自定義判斷是否創(chuàng)建文件
- 實(shí)現(xiàn) IFileCreate 接口
該配置用于判斷某個類是否需要覆蓋創(chuàng)建敢靡,當(dāng)然你可以自己實(shí)現(xiàn)差異算法 merge 文件
initMap
- 注入自定義 Map 對象(注意需要setMap放進(jìn)去)
/**
? ? * 自定義屬性注入
? ? */
? ? public static InjectionConfig initInjection(String projectPath, String moduleName) {
? ? ? ? // 自定義配置
? ? ? ? InjectionConfig injectionConfig = new InjectionConfig() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void initMap() {
? ? ? ? ? ? ? ? // 可用于自定義屬性
? ? ? ? ? ? }
? ? ? ? };
? ? ? ? // 模板引擎是Velocity
? ? ? ? String templatePath = "/templates/mapper.xml.vm";
? ? ? ? // 自定義輸出配置
? ? ? ? List<FileOutConfig> focList = new ArrayList<>();
? ? ? ? // 自定義配置會被優(yōu)先輸出
? ? ? ? focList.add(new FileOutConfig(templatePath) {
? ? ? ? ? ? @Override
? ? ? ? ? ? public String outputFile(TableInfo tableInfo) {
? ? ? ? ? ? ? ? // 自定義輸出文件名 挂滓, 如果你 Entity 設(shè)置了前后綴、此處注意 xml 的名稱會跟著發(fā)生變化Pル省赶站!
? ? ? ? ? ? ? ? return projectPath + "/src/main/resources/mapper/" + moduleName
? ? ? ? ? ? ? ? ? ? ? ? + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? injectionConfig.setFileOutConfigList(focList);
? ? ? ? return injectionConfig;
? ? }
策略配置StrategyConfig
/**
? ? * 策略配置
? ? */
? ? public static StrategyConfig initStrategy(String[] tableNames) {
? ? ? ? StrategyConfig strategyConfig = new StrategyConfig();
? ? ? ? strategyConfig.setNaming(NamingStrategy.underline_to_camel);
? ? ? ? strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
? ? ? ? strategyConfig.setEntityLombokModel(true);
? ? ? ? strategyConfig.setRestControllerStyle(true);
? ? ? ? //當(dāng)表名中帶*號時可以啟用通配符模式
? ? ? ? if (tableNames.length == 1 && tableNames[0].contains("*")) {
? ? ? ? ? ? String[] likeStr = tableNames[0].split("_");
? ? ? ? ? ? String likePrefix = likeStr[0] + "_";
? ? ? ? ? ? strategyConfig.setLikeTable(new LikeTable(likePrefix));
? ? ? ? } else {
? ? ? ? ? ? strategyConfig.setInclude(tableNames);
? ? ? ? }
? ? ? ? return strategyConfig;
? ? }
代碼生成器
public static void main(String[] args) {
? ? ? ? String projectPath = System.getProperty("user.dir");
? ? ? ? String moduleName = scanner("模塊名");
? ? ? ? String[] tableNames = scanner("表名,多個英文逗號分割").split(",");
? ? ? ? // 代碼生成器
? ? ? ? AutoGenerator autoGenerator = new AutoGenerator();
? ? ? ? autoGenerator.setGlobalConfig(initGlobal(projectPath));
? ? ? ? autoGenerator.setDataSource(initDataSource());
? ? ? ? autoGenerator.setPackageInfo(initPackage(moduleName));
? ? ? ? autoGenerator.setCfg(initInjection(projectPath, moduleName));
? ? ? ? autoGenerator.setTemplate(initTemplate());
? ? ? ? autoGenerator.setStrategy(initStrategy(tableNames));
? ? ? ? autoGenerator.setTemplateEngine(new VelocityTemplateEngine());
? ? ? ? autoGenerator.execute();
? ? }
讀取控制臺內(nèi)容
/**
? ? * <p>
? ? * 讀取控制臺內(nèi)容
? ? * </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 (StrUtil.isNotEmpty(ipt)) {
? ? ? ? ? ? ? ? return ipt;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? throw new MybatisPlusException("請輸入正確的" + tip + "纺念!");
? ? }
配置application.properties
dataSource.url=jdbc:mysql://db:3306/mymes?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
dataSource.driverName=com.mysql.jdbc.Driver
dataSource.username=reader
dataSource.password=123456
package.base=com.springboot.mymes_demo.modules
運(yùn)行代碼
注:
在輸入表明的時候輸入xx_*表示xx前綴的所有表 若輸入全名贝椿,則生成對于的表