Mybatis-Plus-AutoGenerator 最詳細(xì)使用方法
AutoGenerator 是 MyBatis-Plus 的代碼生成器,通過(guò) AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各個(gè)模塊的代碼,極大的提升了開(kāi)發(fā)效率。可以通過(guò)模版等一系列的方式來(lái)生成代碼,⚠️這個(gè)比Mybatis-Generator的更加強(qiáng)大,純java代碼。。官方地址:https://mp.baomidou.com/guide/generator.html
package com.cikers.ps;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
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 org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class MysqlGenerator {
public static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
StringBuilder help = new StringBuilder();
help.append("請(qǐng)輸入" + tip + ":");
System.out.println(help.toString());
if (scanner.hasNext()) {
String ipt = scanner.next();
if (StringUtils.isNotEmpty(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("請(qǐng)輸入正確的" + tip + "!");
}
public static void main(String[] args) {
// 代碼生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = "/Users/syk/Documents/*/*/";
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor("syk");
gc.setOpen(false);
gc.setBaseResultMap(true);
gc.setBaseColumnList(true);
//gc.setControllerName("SSSSScontroller");
// 是否覆蓋已有文件
gc.setFileOverride(false);
mpg.setGlobalConfig(gc);
// 數(shù)據(jù)源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://******/newstack_db?useUnicode=true&characterEncoding=UTF-8");
// dsc.setSchemaName("public");
dsc.setDriverName("com.mysql.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("password");
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
//pc.setModuleName(scanner("模塊名"));
pc.setParent(null);
// 這個(gè)地址是生成的配置文件的包路徑
pc.setEntity("com.cikers.ps.model.entity");
//pc.setController("com.cikers.ps.controller");
pc.setMapper("com.cikers.ps.mapper");
mpg.setPackageInfo(pc);
// 自定義配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 如果模板引擎是 freemarker
String templatePath = "/templates/mapper.xml.ftl";
// 如果模板引擎是 velocity
//String templatePath = "/templates/mapper.xml.vm";
// 自定義輸出配置
List<FileOutConfig> focList = new ArrayList<>();
// 自定義配置會(huì)被優(yōu)先輸出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定義輸出文件名
return projectPath + "/src/main/resources/mapper/entity"
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
// //配置自定義輸出模板
// 不需要其他的類型時(shí),直接設(shè)置為null就不會(huì)成對(duì)應(yīng)的模版了
//templateConfig.setEntity("...");
templateConfig.setService(null);
templateConfig.setController(null);
templateConfig.setServiceImpl(null);
// 自定義模板配置,可以 copy 源碼 mybatis-plus/src/main/resources/templates 下面內(nèi)容修改,
// 放置自己項(xiàng)目的 src/main/resources/templates 目錄下, 默認(rèn)名稱一下可以不配置,也
// 可以自定義模板名稱 只要放到目錄下,名字不變 就會(huì)采用這個(gè)模版 下面這句有沒(méi)有無(wú)所謂
// 模版去github上看地址:
/**https://github.com/baomidou/mybatis-plus/tree/3.0/mybatis-plus-generator/src/main/resources/templates*/
//templateConfig.setEntity("/templates/entity.java");
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setSuperEntityClass("com.cikers.ps.model.BaseEntity");
strategy.setSuperMapperClass("com.cikers.ps.util.IMapper");
strategy.setEntityLombokModel(false);
//strategy.setRestControllerStyle(false);
//strategy.setSuperControllerClass("com.cikers.ps.controller.MysqlController");
strategy.setInclude(scanner("表名"));
// 設(shè)置繼承的父類字段
strategy.setSuperEntityColumns("id","modifiedBy","modifiedOn","createdBy","createdOn");
//strategy.setControllerMappingHyphenStyle(true);
//strategy.setTablePrefix(pc.getModuleName() + "_");
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}
其中需要的maven依賴
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.0-RELEASE</version> </dependency> <!-- mp自動(dòng)代碼生成--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.0.7.1</version> </dependency> <!-- velocity 模板引擎, 默認(rèn) --> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-engine-core</artifactId> <version>2.0</version> </dependency> <!-- freemarker 模板引擎 --> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.23</version> </dependency> <!-- beetl 模板引擎 --> <dependency> <groupId>com.ibeetl</groupId> <artifactId>beetl</artifactId> <version>2.2.5</version> </dependency>

運(yùn)行輸入表面就可以了!?。?!
到此這篇關(guān)于Mybatis-Plus-AutoGenerator 最詳細(xì)使用方法的文章就介紹到這了,更多相關(guān)Mybatis Plus AutoGenerator內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Mybatis-plus?代碼生成器?AutoGenerator?的簡(jiǎn)介和使用詳解
- MyBatis-Plus中AutoGenerator的使用案例
- Mybatis-Plus開(kāi)發(fā)提速器mybatis-plus-generator-ui詳解
- mybatis-plus使用generator實(shí)現(xiàn)逆向工程
- 基于mybatis-plus-generator實(shí)現(xiàn)代碼自動(dòng)生成器
- mybatis mybatis-plus-generator+clickhouse自動(dòng)生成代碼案例詳解
- MyBatis-Plus逆向工程——Generator的使用
- MyBatis-Plus Generator配置詳解
- 使用mybatis-plus-generator進(jìn)行代碼自動(dòng)生成的方法
- Mybatis-Plus開(kāi)發(fā)提速器generator的使用
相關(guān)文章
spring boot 實(shí)現(xiàn)Minio分片上傳的步驟
分片上傳,就是將所要上傳的文件,按照一定的大小,將整個(gè)文件分隔成多個(gè)數(shù)據(jù)塊來(lái)進(jìn)行分別上傳,上傳完之后再由服務(wù)端對(duì)所有上傳的文件進(jìn)行匯總整合成原始的文件,本文給大家介紹spring boot 實(shí)現(xiàn)Minio分片上傳的步驟,感興趣的朋友跟隨小編一起看看吧2023-10-10
SpringBoot集成H2內(nèi)存數(shù)據(jù)庫(kù)的方法
H2是Thomas Mueller提供的一個(gè)開(kāi)源的、純java實(shí)現(xiàn)的關(guān)系數(shù)據(jù)庫(kù)。本文主要介紹了SpringBoot集成H2內(nèi)存數(shù)據(jù)庫(kù),具有一定的參考價(jià)值,感興趣的可以了解一下2021-09-09
Java編程Webservice指定超時(shí)時(shí)間代碼詳解
這篇文章主要介紹了Java編程Webservice指定超時(shí)時(shí)間代碼詳解,簡(jiǎn)單介紹了webservice,然后分享了通過(guò)使用JDK對(duì)Webservice的支持進(jìn)行Webservice調(diào)用實(shí)現(xiàn)指定超時(shí)時(shí)間完整示例,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-11-11
spring cloud如何修復(fù)zuul跨域配置異常的問(wèn)題
最近的開(kāi)發(fā)過(guò)程中,使用spring集成了spring-cloud-zuul,在配置zuul跨域的時(shí)候遇到了問(wèn)題,下面這篇文章主要給大家介紹了關(guān)于spring cloud如何修復(fù)zuul跨域配置異常的問(wèn)題,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-09-09
SpringBoot實(shí)現(xiàn)物品點(diǎn)贊功能
這篇文章主要介紹了SpringBoot物品點(diǎn)贊功能實(shí)現(xiàn),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
使用監(jiān)聽(tīng)器對(duì)Spring bean id進(jìn)行唯一校驗(yàn)過(guò)程解析
這篇文章主要介紹了使用監(jiān)聽(tīng)器對(duì)Spring bean id進(jìn)行唯一校驗(yàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
Springboot遷移到Micronaut實(shí)現(xiàn)過(guò)程詳解
這篇文章主要為大家?介紹了Springboot遷移到Micronaut實(shí)現(xiàn)過(guò)程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05

