前言
- 代碼生成器顧名思義就是為我們生成一些代碼铐懊,省去了我們一些時(shí)間媒抠。
- MyBatis-Plus 的代碼生成器可以生成 Entity蝇完、Mapper爸邢、Mapper XML樊卓、Service、Controller 模塊代碼杠河。
須知
- MyBatis-Plus 從
3.0.3
之后移除了代碼生成器與模板引擎的默認(rèn)依賴简识,需要手動(dòng)添加相關(guān)依賴,才能實(shí)現(xiàn)代碼生成器功能感猛。
玩熟 MyBatis-Plus 代碼生成器
1.新建 MyBatis-Plus 代碼生成器項(xiàng)目
- 使用 Spring 腳手架創(chuàng)建 SpringBoot 項(xiàng)目七扰,如果不太熟悉 IDEA 快速生成 SpringBoot 項(xiàng)目,可以先看下面一篇博客陪白,幾分鐘就搞定颈走。
SpringBoot 快速入門
2.添加代碼生成器依賴
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.2.0</version>
</dependency>
3.添加模板引擎依賴
- MyBatis-Plus 支持 Velocity(默認(rèn))、Freemarker咱士、Beetl立由,可以選擇自己熟悉的模板引擎照激,如果都不滿足您的要求砾跃,可以采用自定義模板引擎轿衔。
- 下面三個(gè)依賴任選其一
<!-- https://mvnrepository.com/artifact/org.apache.velocity/velocity-engine-core --> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-engine-core</artifactId> <version>2.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.freemarker/freemarker --> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.30</version> </dependency> <!-- https://mvnrepository.com/artifact/com.ibeetl/beetl --> <dependency> <groupId>com.ibeetl</groupId> <artifactId>beetl</artifactId> <version>3.1.3.RELEASE</version> </dependency>
- 如果我們選擇了非默認(rèn)引擎腮鞍,需要在 AutoGenerator 中 設(shè)置模板引擎。
AutoGenerator generator = new AutoGenerator(); // set freemarker engine generator.setTemplateEngine(new FreemarkerTemplateEngine()); // set beetl engine generator.setTemplateEngine(new BeetlTemplateEngine()); // set custom engine (reference class is your custom engine class) generator.setTemplateEngine(new CustomTemplateEngine()); // other config ...
4.完整pom.xml
- 代碼生成器需要依賴數(shù)據(jù)庫表道盏,所以也需要 MySQL 驅(qū)動(dòng)包
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>cn.zwq</groupId> <artifactId>mybatis-plus-auto-generator</artifactId> <version>0.0.1-SNAPSHOT</version> <name>mybatis-plus-auto-generator</name> <description>mybatis-plus-auto-generator</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--MyBatis-Plus代碼生成器需要的依賴而柑,開始--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.2.0</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.2.0</version> </dependency> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!--MyBatis-Plus代碼生成器需要的依賴,結(jié)束--> </dependencies> </project>
5.全局配置
-
user.dir
獲取到你當(dāng)前工程的 src 目錄路徑
6.數(shù)據(jù)庫信息配置
7.包配置
8.策略配置
-
配置根據(jù)哪張表生成代碼
9.完整 MyBatis-Plus 代碼生成器代碼
- MyBatis-Plus 代碼生成器所有配置都可以使用 Java Config 完成荷逞,不需要單獨(dú)在 XML 配置媒咳。
public class SggCodeGenerator { public static void main(String[] args) { // 1、創(chuàng)建代碼生成器 AutoGenerator mpg = new AutoGenerator(); // 2种远、全局配置 GlobalConfig gc = new GlobalConfig(); String projectPath = System.getProperty("user.dir"); gc.setOutputDir(projectPath + "/src/main/java"); gc.setAuthor("zwq"); gc.setOpen(false); //生成后是否打開資源管理器 gc.setFileOverride(false); //重新生成時(shí)文件是否覆蓋 gc.setServiceName("%sService"); //去掉Service接口的首字母I gc.setIdType(IdType.ID_WORKER_STR); //主鍵策略 gc.setDateType(DateType.ONLY_DATE);//定義生成的實(shí)體類中日期類型 gc.setSwagger2(false);//開啟Swagger2模式 mpg.setGlobalConfig(gc); // 3涩澡、數(shù)據(jù)源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:mysql://localhost:3306/mybatis-plus?serverTimezone=GMT%2B8"); dsc.setDriverName("com.mysql.cj.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("root"); dsc.setDbType(DbType.MYSQL); mpg.setDataSource(dsc); // 4、包配置 PackageConfig pc = new PackageConfig(); pc.setModuleName(null); //模塊名 pc.setParent("cn.zwq.mybatis-plus"); pc.setController("controller"); pc.setEntity("entity"); pc.setService("service"); pc.setMapper("mapper"); mpg.setPackageInfo(pc); // 5坠敷、策略配置 StrategyConfig strategy = new StrategyConfig(); strategy.setInclude("user");//對(duì)那一張表生成代碼 strategy.setNaming(NamingStrategy.underline_to_camel);//數(shù)據(jù)庫表映射到實(shí)體的命名策略 strategy.setTablePrefix(pc.getModuleName() + "_"); //生成實(shí)體時(shí)去掉表前綴 strategy.setColumnNaming(NamingStrategy.underline_to_camel);//數(shù)據(jù)庫表字段映射到實(shí)體的命名策略 strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter鏈?zhǔn)讲僮? strategy.setRestControllerStyle(true); //restful api風(fēng)格控制器 strategy.setControllerMappingHyphenStyle(true); //url中駝峰轉(zhuǎn)連字符 mpg.setStrategy(strategy); // 6妙同、執(zhí)行 mpg.execute(); } }
運(yùn)行代碼生成器代碼
-
主要運(yùn)行其中的 main 方法即可,然后就自動(dòng)根據(jù)哪張表生成代碼了膝迎。
總結(jié)
- 代碼生成器的全部代碼都在這篇博客當(dāng)中了粥帚,沒有那塊代碼是缺漏,相信大家看完這篇博客之后就能玩熟代碼生成器了弄抬。
- 如果覺得不錯(cuò)茎辐,可以點(diǎn)個(gè)贊或者關(guān)注博主我也行宪郊,感謝掂恕!