mybatis-plus使用generator實現(xiàn)逆向工程
1.背景
可以使用mybatis-plus-generator逆向生成dao層、service層、controller層等代碼
2.引入jar包
mybatis-plus-generator在3.5.0以及以后的版本使用新的方式逆向生成代碼。
這里介紹使用舊版本的方式生成代碼。
<!-- mybatis-plus begin -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
<!-- mybatis-plus 默認是vm引擎 -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
<!-- mybatis-plus end -->3.自動生成代碼
public static void main(String[] args) {
// 構(gòu)建一個代碼生成器對象
AutoGenerator mpg = new AutoGenerator();
// 配置執(zhí)行策略
// 1.全局配置
GlobalConfig gc = new GlobalConfig();
// 當前項目路徑
String proPath = System.getProperty("user.dir");
// 設(shè)置代碼生成路徑
gc.setOutputDir(proPath + "/src/main/java");
// 生成的類的注釋中作者信息
gc.setAuthor("curry");
// 生成后是否打開文件夾
gc.setOpen(false);
// 是否覆蓋
gc.setFileOverride(true);
// 生成service類的后綴
gc.setServiceName("%sService");
// 主鍵生成策略 和數(shù)據(jù)庫id生成策略一致
gc.setIdType(IdType.AUTO);
// 設(shè)置日期類型
gc.setDateType(DateType.ONLY_DATE);
// 是否生成Swagger
gc.setSwagger2(false);
// 生成entity類的后綴
gc.setEntityName("%sEntity");
mpg.setGlobalConfig(gc);
// 2.設(shè)置數(shù)據(jù)源
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://127.0.0.1:3306/test_db");
dsc.setDriverName("com.mysql.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("123456");
dsc.setDbType(DbType.MYSQL);
mpg.setDataSource(dsc);
// 3.配置生成包的路徑
PackageConfig pc = new PackageConfig();
// 設(shè)置模塊存放位置
pc.setParent("com.");
// 設(shè)置該模塊包的路徑
pc.setModuleName("dao");
pc.setEntity("entity");
pc.setMapper("mapper");
pc.setService("service");
pc.setController("controller");
mpg.setPackageInfo(pc);
// 4.策略配置
StrategyConfig strategy=new StrategyConfig();
// 設(shè)置要映射的表名 不配置默認處理全部表
// strategy.setInclude("user");
// 表名中下劃線轉(zhuǎn)駝峰命名
strategy.setNaming(NamingStrategy.underline_to_camel);
// 表中字段如果有下劃線,轉(zhuǎn)駝峰命名
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
// strategy.setEntityLombokModel(true);//自動生成Lombok
// strategy.setRestControllerStyle(true);//開啟 RestFul 風格
// strategy.setControllerMappingHyphenStyle(true);
// 對表中的字段 設(shè)置邏輯刪除 生成的dao層代碼會添加@TableLogic
strategy.setLogicDeleteFieldName("delete_flag");
// 5.自動填充 (表中如果有創(chuàng)建時間、修改時間話,可以使用自動填充)
TableFill createTime = new TableFill("created_date", FieldFill.INSERT);
TableFill updateTime = new TableFill("modified_date", FieldFill.INSERT_UPDATE);
ArrayList<TableFill> tableFills = new ArrayList<>();
tableFills.add(createTime);
tableFills.add(updateTime);
strategy.setTableFillList(tableFills);
// 樂觀鎖配置
// strategy.setVersionFieldName("version");
mpg.setStrategy(strategy);
// 6.配置實體類模板
TemplateConfig templateConfig = new TemplateConfig();
// 如果setXxxxx(null) 不會生成Xxxx實體類相關(guān)代碼
// 因此如果只生成dao層代碼
// 可以在這里控制
templateConfig.setController(null);
templateConfig.setMapper(null);
templateConfig.setService(null);
templateConfig.setServiceImpl(null);
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
// 7.執(zhí)行代碼生成操作
mpg.execute();
}4.修改*Mapper.xml文件的生成位置
4.1 默認*Mapper.xml文件生成位置
// 3.配置生成包的路徑
PackageConfig pc = new PackageConfig();
// 設(shè)置模塊存放位置
pc.setParent("com.");
// 設(shè)置該模塊包的路徑
pc.setModuleName("dao");
pc.setEntity("entity");
pc.setMapper("mapper");
pc.setService("service");
pc.setController("controller");
mpg.setPackageInfo(pc);以上面代碼為例,*Mapper.xml文件位置是mapper/xml/*Mapper.xml
4.2 修改*Mapper.xml文件生成位置
step1:在模板中控制不生成xml文件 防止重復(fù)生成
templateConfig.setXml(null);
step2:在第三步中mpg.execute();執(zhí)行前增加以下代碼
// 自定義配置
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<>();
// 自定義配置會被優(yōu)先輸出
// 這里自定義配置的是*Mapper.xml文件
// 所以templatePath = "/templates/mapper.xml.vm";
// 如果你想自定義配置其它 修改templatePath即可
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定義輸出文件名 如果你 Entity 設(shè)置了前后綴
String entityName = tableInfo.getEntityName();
int length = entityName.length();
entityName = entityName.substring(0, length - 6);
return proPath + "/src/main/resources/mapper/" +
entityName + "Mapper" + StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);到此這篇關(guān)于mybatis-plus使用generator實現(xiàn)逆向工程的文章就介紹到這了,更多相關(guān)mybatis-plus generator 逆向工程內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Cloud Ribbon實現(xiàn)客戶端負載均衡的方法
本篇文章主要介紹了Spring Cloud Ribbon實現(xiàn)客戶端負載均衡的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05
淺談Java讀寫注冊表的方式Preferences與jRegistry
這篇文章主要介紹了淺談Java讀寫注冊表的方式Preferences與jRegistry,分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-02-02
SpringBoot動態(tài)生成接口實現(xiàn)流程示例講解
最近遇到一個需求,需要在程序運行過程中,可以動態(tài)新增接口,自定義接口參數(shù)名稱,基本類型,以及請求方法,請求頭等等。通過幾天的研究,找到了我需要的解決方案2023-01-01
Netty組件NioEventLoopGroup創(chuàng)建線程執(zhí)行器源碼解析
這篇文章主要介紹了Netty組件NioEventLoopGroup創(chuàng)建線程執(zhí)行器源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-03-03
@valid 無法觸發(fā)BindingResult的解決
這篇文章主要介紹了@valid 無法觸發(fā)BindingResult的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
SSH框架網(wǎng)上商城項目第30戰(zhàn)之項目總結(jié)(附源碼下載地址)
這篇文章主要介紹了SSH框架網(wǎng)上商城項目第30戰(zhàn)之項目總結(jié),并附源碼下載地址,感興趣的小伙伴們可以參考一下2016-06-06
Java設(shè)計實現(xiàn)一個針對各種類型的緩存
這篇文章主要為大家詳細介紹了Java如何設(shè)計實現(xiàn)一個針對各種類型的緩存,文中的示例代碼講解詳細,具有一定的學習價值,感興趣的小伙伴可以了解一下2023-11-11

