Mybatis Plus 代碼生成器的實(shí)現(xiàn)
代碼生成器
MyBatis Plus是MyBatis的擴(kuò)展框架,而代碼生成器是MP的核心功能之一,另外還有 “條件構(gòu)造器”和“通用CRUD”等功能。
步驟演示
mp的代碼生成器有兩種方式自動(dòng)生成代碼,一種是通過(guò)main方法來(lái)執(zhí)行程序,另一種是通過(guò)maven插件build產(chǎn)生。第二種方法需要在pom.xml中添加大量的配置信息,因此本人偏向于使用第一種方式。步驟如下:
一、添加mybatis plus依賴:
如果還沒(méi)有創(chuàng)建項(xiàng)目,當(dāng)然需要先創(chuàng)建一個(gè)工程項(xiàng)目,然后將jar包依賴添加到項(xiàng)目的classpath下,如果是含有pom.xml的maven項(xiàng)目,比如我最近在使用的springboot項(xiàng)目,那么可以直接添加pom依賴:
<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>2.3</version> </dependency>
二、創(chuàng)建生成器主類
官方給出的實(shí)例連接:代碼生成器, 此處貼出我整理過(guò)后的代碼:
package com.mht.springbootmybatis.generate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.FileOutConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
/**
* <p>
* 代碼生成器演示
* </p>
*/
public class MpGenerator {
/**
* <p>
* MySQL 生成演示
* </p>
*/
public static void main(String[] args) {
AutoGenerator mpg = new AutoGenerator();
// 選擇 freemarker 引擎,默認(rèn) Veloctiy
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
// 全局配置
GlobalConfig gc = new GlobalConfig();
gc.setAuthor("Mht");
gc.setOutputDir("D://workspace/spring-boot-mybatis/src/main/java");
gc.setFileOverride(false);// 是否覆蓋同名文件,默認(rèn)是false
gc.setActiveRecord(true);// 不需要ActiveRecord特性的請(qǐng)改為false
gc.setEnableCache(false);// XML 二級(jí)緩存
gc.setBaseResultMap(true);// XML ResultMap
gc.setBaseColumnList(false);// XML columList
/* 自定義文件命名,注意 %s 會(huì)自動(dòng)填充表實(shí)體屬性! */
// gc.setMapperName("%sDao");
// gc.setXmlName("%sDao");
// gc.setServiceName("MP%sService");
// gc.setServiceImplName("%sServiceDiy");
// gc.setControllerName("%sAction");
mpg.setGlobalConfig(gc);
// 數(shù)據(jù)源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setDbType(DbType.MYSQL);
dsc.setTypeConvert(new MySqlTypeConvert() {
// 自定義數(shù)據(jù)庫(kù)表字段類型轉(zhuǎn)換【可選】
@Override
public DbColumnType processTypeConvert(String fieldType) {
System.out.println("轉(zhuǎn)換類型:" + fieldType);
// 注意??!processTypeConvert 存在默認(rèn)類型轉(zhuǎn)換,如果不是你要的效果請(qǐng)自定義返回、非如下直接返回。
return super.processTypeConvert(fieldType);
}
});
dsc.setDriverName("com.mysql.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("root");
dsc.setUrl("jdbc:mysql://localhost:3306/ease-run?useUnicode=true&characterEncoding=utf8");
mpg.setDataSource(dsc);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
// strategy.setCapitalMode(true);// 全局大寫(xiě)命名 ORACLE 注意
strategy.setTablePrefix(new String[] { "user_" });// 此處可以修改為您的表前綴
strategy.setNaming(NamingStrategy.nochange);// 表名生成策略
strategy.setInclude(new String[] { "user" }); // 需要生成的表
// strategy.setExclude(new String[]{"test"}); // 排除生成的表
// 自定義實(shí)體父類
// strategy.setSuperEntityClass("com.baomidou.demo.TestEntity");
// 自定義實(shí)體,公共字段
// strategy.setSuperEntityColumns(new String[] { "test_id", "age" });
// 自定義 mapper 父類
// strategy.setSuperMapperClass("com.baomidou.demo.TestMapper");
// 自定義 service 父類
// strategy.setSuperServiceClass("com.baomidou.demo.TestService");
// 自定義 service 實(shí)現(xiàn)類父類
// strategy.setSuperServiceImplClass("com.baomidou.demo.TestServiceImpl");
// 自定義 controller 父類
// strategy.setSuperControllerClass("com.baomidou.demo.TestController");
// 【實(shí)體】是否生成字段常量(默認(rèn) false)
// public static final String ID = "test_id";
// strategy.setEntityColumnConstant(true);
// 【實(shí)體】是否為構(gòu)建者模型(默認(rèn) false)
// public User setName(String name) {this.name = name; return this;}
// strategy.setEntityBuilderModel(true);
mpg.setStrategy(strategy);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setParent("com.mht.springbootmybatis");
// pc.setModuleName("test");
mpg.setPackageInfo(pc);
// 注入自定義配置,可以在 VM 中使用 cfg.abc 【可無(wú)】
// InjectionConfig cfg = new InjectionConfig() {
// @Override
// public void initMap() {
// Map<String, Object> map = new HashMap<String, Object>();
// map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
// this.setMap(map);
// }
// };
//
// // 自定義 xxList.jsp 生成
// List<FileOutConfig> focList = new ArrayList<>();
// focList.add(new FileOutConfig("/template/list.jsp.vm") {
// @Override
// public String outputFile(TableInfo tableInfo) {
// // 自定義輸入文件名稱
// return "D://my_" + tableInfo.getEntityName() + ".jsp";
// }
// });
// cfg.setFileOutConfigList(focList);
// mpg.setCfg(cfg);
//
// // 調(diào)整 xml 生成目錄演示
// focList.add(new FileOutConfig("/templates/mapper.xml.vm") {
// @Override
// public String outputFile(TableInfo tableInfo) {
// return "/develop/code/xml/" + tableInfo.getEntityName() + ".xml";
// }
// });
// cfg.setFileOutConfigList(focList);
// mpg.setCfg(cfg);
//
// // 關(guān)閉默認(rèn) xml 生成,調(diào)整生成 至 根目錄
// TemplateConfig tc = new TemplateConfig();
// tc.setXml(null);
// mpg.setTemplate(tc);
// 自定義模板配置,可以 copy 源碼 mybatis-plus/src/main/resources/templates 下面內(nèi)容修改,
// 放置自己項(xiàng)目的 src/main/resources/templates 目錄下, 默認(rèn)名稱一下可以不配置,也可以自定義模板名稱
// TemplateConfig tc = new TemplateConfig();
// tc.setController("...");
// tc.setEntity("...");
// tc.setMapper("...");
// tc.setXml("...");
// tc.setService("...");
// tc.setServiceImpl("...");
// 如上任何一個(gè)模塊如果設(shè)置 空 OR Null 將不生成該模塊。
// mpg.setTemplate(tc);
// 執(zhí)行生成
mpg.execute();
// 打印注入設(shè)置【可無(wú)】
// System.err.println(mpg.getCfg().getMap().get("abc"));
}
}
文件基本可以拿過(guò)來(lái)直接用,但需要注意這兩句代碼:
gc.setOutputDir("D://workspace/spring-boot-mybatis/src/main/java");// 設(shè)置文件輸出路徑
pc.setParent("com.mht.springbootmybatis");// 父包名
下圖是我項(xiàng)目的路徑(執(zhí)行代碼生成器之后的項(xiàng)目結(jié)構(gòu)):

執(zhí)行main方法前請(qǐng)注意,在生成文件的時(shí)候需要有一個(gè)模板引擎的選擇,MyBatis Plus的默認(rèn)模板引擎是velocity。我們可以使用freemarker。
<dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> </dependency>
最后執(zhí)行main方法就可以了。
執(zhí)行控制臺(tái)如下:

注意,代碼生成前請(qǐng)確保數(shù)據(jù)庫(kù)中表已經(jīng)存在。
以上就是對(duì)mybatis plus代碼生成器的簡(jiǎn)單使用,并不是非常詳細(xì)的應(yīng)用教程風(fēng),只是簡(jiǎn)單的入門使用,以便日后快速查閱,后續(xù)發(fā)現(xiàn)新的值得記錄的地方會(huì)繼續(xù)更新這篇博客。歡迎各位踴躍留言!
參考文章:《MyBatis Plus代碼生成器》《MyBatis Plus學(xué)習(xí)》《mybatis-plus思維導(dǎo)圖,讓mybatis-plus不再難懂》
到此這篇關(guān)于Mybatis Plus 代碼生成器的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Mybatis Plus 代碼生成器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
怎樣提高mybatis-plus中saveBatch方法的效率
這篇文章主要介紹了怎樣提高mybatis-plus中saveBatch方法的效率問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
SpringBoot如何實(shí)現(xiàn)分離資源文件并打包
這篇文章主要介紹了SpringBoot如何實(shí)現(xiàn)分離資源文件并打包,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
SpringSecurity框架下實(shí)現(xiàn)CSRF跨站攻擊防御的方法
CSRF是一種網(wǎng)絡(luò)攻擊方式,也可以說(shuō)是一種安全漏洞,這種安全漏洞在web開(kāi)發(fā)中廣泛存在。這篇文章主要介紹了SpringSecurity框架下實(shí)現(xiàn)CSRF跨站攻擊防御,需要的朋友可以參考下2019-12-12
Java JVM字節(jié)碼指令集總結(jié)整理與介紹
本節(jié)將會(huì)著重介紹一下JVM中的指令集、Java是如何跨平臺(tái)的、JVM指令集參考手冊(cè)等內(nèi)容。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09
Mybatis控制臺(tái)打印SQL執(zhí)行信息的方法詳解
SQL性能監(jiān)控是一個(gè)程序必要的功能,通常我們可以使用數(shù)據(jù)庫(kù)自帶的客戶端工具進(jìn)行SQL性能分析,本章節(jié)只實(shí)現(xiàn)Mybatis執(zhí)行時(shí)對(duì)執(zhí)行SQL進(jìn)行攔截,控制臺(tái)打印執(zhí)行SQL包括參數(shù)、執(zhí)行方法以及執(zhí)行時(shí)間,需要的朋友可以參考下2024-11-11
Java基礎(chǔ)之FileInputStream和FileOutputStream流詳解
這篇文章主要介紹了Java基礎(chǔ)之FileInputStream和FileOutputStream流詳解,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04
Java CompletableFuture如何實(shí)現(xiàn)超時(shí)功能
這篇文章主要為大家介紹了實(shí)現(xiàn)超時(shí)功能的基本思路以及CompletableFuture(之后簡(jiǎn)稱CF)是如何通過(guò)代碼實(shí)現(xiàn)超時(shí)功能的,需要的小伙伴可以了解下2025-01-01

