關于mybatis-plus-generator的簡單使用示例詳解
在springboot項目中集成mybatis-plus是很方便開發(fā)的,最近看了一下plus的文檔,簡單用一下它的代碼生成器,首先在一個簡單的springboot項目中加入如下依賴
<!-- 引入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>
我這邊呢習慣把mapper文件放在java源碼路徑下,而不是放在默認的resources目錄下,項目啟動有導致mapper注入不了,所以還得在pom的<build></build>標簽之中加入如下配置,并在啟動類上加上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);
}
}基本依賴和配置結束了,我參考了plus的官方文檔,根據(jù)自己的需求稍微修改了一下它的生成器代碼,官網(wǎng)地址
/**
* mybatis-plus代碼生成器,生成實體,mapper,mapper.xml,service,serviceImpl,controller
* 演示例子,執(zhí)行 main 方法控制臺輸入表名回車自動生成對應項目目錄中(目錄要需要自行修改)
*/
public class CodeGenerator {
/**
* <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 (StringUtils.isNotEmpty(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("請輸入正確的" + 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");
//是否打開輸出的目錄,默認true
gc.setOpen(false);
//覆蓋已有的文件,默認false(第一次生成時放開)
// gc.setFileOverride(true);
gc.setBaseResultMap(true);
gc.setBaseColumnList(true);
// 設置日期類型為Date(若不設置時間類型都會變成LocalDateTime部分連接池例如druid是無法識別的)
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");
//自定義實體包名(不同的模塊自己手動修改)
pc.setEntity("mapper.goods.entity");
//自定義mapper包名(不同的模塊自己手動修改)
pc.setMapper("mapper.goods");
//自定義mapper.xml包名(不同的模塊自己手動修改)
pc.setXml("mapper.goods");
//自定義service包名(不同的模塊自己手動修改)
pc.setService("service.goods");
//自定義serviceImpl包名(不同的模塊自己手動修改)
pc.setServiceImpl("service.goods.impl");
//自定義controller包名(不同的模塊自己手動修改)
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<>();
// 自定義配置會被優(yōu)先輸出
focList.add(new FileOutConfig(xmlPath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定義輸出文件名 , 如果你 Entity 設置了前后綴、此處注意 xml 的名稱會跟著發(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模型,默認為false
strategy.setEntityLombokModel(true);
//前后端分離時可開啟
// strategy.setRestControllerStyle(true);
strategy.setInclude(scanner("表名,多個英文逗號分割").split(","));
//RequestMapping駝峰轉連字符
// strategy.setControllerMappingHyphenStyle(true);
//生成實體時生成生成數(shù)據(jù)庫字段注解
strategy.setEntityTableFieldAnnotationEnable(true);
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}然后運行即可


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


更多Java內(nèi)容,請點擊下方名片。

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

