關(guān)于mybatis-plus-generator的簡單使用示例詳解
在springboot項(xiàng)目中集成mybatis-plus是很方便開發(fā)的,最近看了一下plus的文檔,簡單用一下它的代碼生成器,首先在一個(gè)簡單的springboot項(xiàng)目中加入如下依賴
<!-- 引入mybatis-plus依賴 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.1.2</version> </dependency> <!-- 引入mybatis-plus-generator依賴 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.3.2</version> </dependency> <!-- 引入freemarker依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <!-- 引入mysql依賴 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.45</version> <scope>runtime</scope> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency>
我這邊呢習(xí)慣把mapper文件放在java源碼路徑下,而不是放在默認(rèn)的resources目錄下,項(xiàng)目啟動(dòng)有導(dǎo)致mapper注入不了,所以還得在pom的<build></build>標(biāo)簽之中加入如下配置,并在啟動(dòng)類上加上mapper掃描路徑
<resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> <include>**/*.ftl</include> </includes> <filtering>false</filtering> </resource> </resources>
@SpringBootApplication
@MapperScan(basePackages = {"com.fengyun.mallmanage"})
public class MallManageSystemApplication {
public static void main(String[] args) {
SpringApplication.run(MallManageSystemApplication.class, args);
}
}基本依賴和配置結(jié)束了,我參考了plus的官方文檔,根據(jù)自己的需求稍微修改了一下它的生成器代碼,官網(wǎng)地址
/**
* mybatis-plus代碼生成器,生成實(shí)體,mapper,mapper.xml,service,serviceImpl,controller
* 演示例子,執(zhí)行 main 方法控制臺(tái)輸入表名回車自動(dòng)生成對(duì)應(yīng)項(xiàng)目目錄中(目錄要需要自行修改)
*/
public class CodeGenerator {
/**
* <p>
* 讀取控制臺(tái)內(nèi)容
* </p>
*/
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 = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor("YuanXing");
//是否打開輸出的目錄,默認(rèn)true
gc.setOpen(false);
//覆蓋已有的文件,默認(rèn)false(第一次生成時(shí)放開)
// gc.setFileOverride(true);
gc.setBaseResultMap(true);
gc.setBaseColumnList(true);
// 設(shè)置日期類型為Date(若不設(shè)置時(shí)間類型都會(huì)變成LocalDateTime部分連接池例如druid是無法識(shí)別的)
gc.setDateType(DateType.ONLY_DATE);
mpg.setGlobalConfig(gc);
// 數(shù)據(jù)源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/malldata-dev?useUnicode=true&characterEncoding=UTF-8&useSSL=false");
dsc.setDriverName("com.mysql.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("密碼");
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
// pc.setModuleName(scanner("模塊名"));
pc.setParent("com.fengyun.mallmanage");
//自定義實(shí)體包名(不同的模塊自己手動(dòng)修改)
pc.setEntity("mapper.goods.entity");
//自定義mapper包名(不同的模塊自己手動(dòng)修改)
pc.setMapper("mapper.goods");
//自定義mapper.xml包名(不同的模塊自己手動(dòng)修改)
pc.setXml("mapper.goods");
//自定義service包名(不同的模塊自己手動(dòng)修改)
pc.setService("service.goods");
//自定義serviceImpl包名(不同的模塊自己手動(dòng)修改)
pc.setServiceImpl("service.goods.impl");
//自定義controller包名(不同的模塊自己手動(dòng)修改)
pc.setController("controller.goods");
mpg.setPackageInfo(pc);
// 自定義配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 如果模板引擎是 freemarker
String xmlPath = "/templates/mapper.xml.ftl";
// 自定義輸出配置
List<FileOutConfig> focList = new ArrayList<>();
// 自定義配置會(huì)被優(yōu)先輸出
focList.add(new FileOutConfig(xmlPath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定義輸出文件名 , 如果你 Entity 設(shè)置了前后綴、此處注意 xml 的名稱會(huì)跟著發(fā)生變化??!
return projectPath + "/src/main/java/com/fengyun/mallmanage/mapper/goods"
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
//是否為lombok模型,默認(rèn)為false
strategy.setEntityLombokModel(true);
//前后端分離時(shí)可開啟
// strategy.setRestControllerStyle(true);
strategy.setInclude(scanner("表名,多個(gè)英文逗號(hào)分割").split(","));
//RequestMapping駝峰轉(zhuǎn)連字符
// strategy.setControllerMappingHyphenStyle(true);
//生成實(shí)體時(shí)生成生成數(shù)據(jù)庫字段注解
strategy.setEntityTableFieldAnnotationEnable(true);
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}然后運(yùn)行即可


需要注意的是由于我在代碼生成器代碼中包配置的時(shí)候注釋掉了輸入模塊名(即這一段pc.setModuleName(scanner("模塊名"));),所以會(huì)導(dǎo)致controller中的RequestMapping的路徑有兩個(gè)//表名的駝峰命名,假設(shè)輸入模塊名的話RequestMapping的路徑就會(huì)是/模塊名/表名的駝峰命名,但是這樣的話生成的類的包就不是我現(xiàn)在這樣的了,這個(gè)看自己的需求吧,具體可以看一下生成包的源碼


更多Java內(nèi)容,請(qǐng)點(diǎn)擊下方名片。

到此這篇關(guān)于關(guān)于mybatis-plus-generator的簡單使用的文章就介紹到這了,更多相關(guān)mybatis-plus-generator使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot集成RedisTemplate的實(shí)現(xiàn)示例
本文主要介紹了SpringBoot集成RedisTemplate的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-09-09
java?數(shù)組實(shí)現(xiàn)學(xué)生成績統(tǒng)計(jì)教程
這篇文章主要介紹了java?數(shù)組實(shí)現(xiàn)學(xué)生成績統(tǒng)計(jì)教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
Java用數(shù)組實(shí)現(xiàn)循環(huán)隊(duì)列的示例
下面小編就為大家?guī)硪黄狫ava用數(shù)組實(shí)現(xiàn)循環(huán)隊(duì)列的示例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09
Maven依賴管理中<o(jì)ptional>與<scope>標(biāo)簽的用法詳解
Maven 作為 Java 項(xiàng)目的核心構(gòu)建工具,其依賴管理機(jī)制是項(xiàng)目開發(fā)的基石,在復(fù)雜的項(xiàng)目中,合理管理依賴不僅能提升構(gòu)建效率,還能有效避免版本沖突和冗余依賴,本文給大家介紹了Maven依賴管理中<o(jì)ptional>與<scope>標(biāo)簽的用法,需要的朋友可以參考下2025-07-07
詳解springboot接口如何優(yōu)雅的接收時(shí)間類型參數(shù)
這篇文章主要為大家詳細(xì)介紹了springboot的接口如何優(yōu)雅的接收時(shí)間類型參數(shù),文中為大家整理了三種常見的方法,希望對(duì)大家有一定的幫助2023-09-09
Java微信授權(quán)登陸的實(shí)現(xiàn)示例
微信授權(quán)登錄,官方文檔寫的比較簡潔。所以對(duì)于會(huì)的人一目了然,而對(duì)于新手剛?cè)腴T的人來說是舉步維艱。本文詳細(xì)的介紹了Java微信授權(quán)登陸的實(shí)現(xiàn)示例,感興趣的朋友可以了解一下2021-06-06
Java實(shí)現(xiàn)快速排序算法(Quicktsort)
這篇文章主要介紹了Java實(shí)現(xiàn)快速排序算法(Quicktsort),有需要的朋友可以參考一下2013-12-12
使用dubbo+zookeeper+spring boot構(gòu)建服務(wù)的方法詳解
這篇文章主要給大家介紹了關(guān)于如何使用dubbo+zookeeper+spring boot構(gòu)建服務(wù)的相關(guān)資料,文中通過示例代碼及圖片介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05

