mybatis-plus使用generator實(shí)現(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 默認(rèn)是vm引擎 -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
<!-- mybatis-plus end -->3.自動(dòng)生成代碼
public static void main(String[] args) {
// 構(gòu)建一個(gè)代碼生成器對(duì)象
AutoGenerator mpg = new AutoGenerator();
// 配置執(zhí)行策略
// 1.全局配置
GlobalConfig gc = new GlobalConfig();
// 當(dāng)前項(xiàng)目路徑
String proPath = System.getProperty("user.dir");
// 設(shè)置代碼生成路徑
gc.setOutputDir(proPath + "/src/main/java");
// 生成的類的注釋中作者信息
gc.setAuthor("curry");
// 生成后是否打開(kāi)文件夾
gc.setOpen(false);
// 是否覆蓋
gc.setFileOverride(true);
// 生成service類的后綴
gc.setServiceName("%sService");
// 主鍵生成策略 和數(shù)據(jù)庫(kù)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è)置要映射的表名 不配置默認(rèn)處理全部表
// strategy.setInclude("user");
// 表名中下劃線轉(zhuǎn)駝峰命名
strategy.setNaming(NamingStrategy.underline_to_camel);
// 表中字段如果有下劃線,轉(zhuǎn)駝峰命名
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
// strategy.setEntityLombokModel(true);//自動(dòng)生成Lombok
// strategy.setRestControllerStyle(true);//開(kāi)啟 RestFul 風(fēng)格
// strategy.setControllerMappingHyphenStyle(true);
// 對(duì)表中的字段 設(shè)置邏輯刪除 生成的dao層代碼會(huì)添加@TableLogic
strategy.setLogicDeleteFieldName("delete_flag");
// 5.自動(dòng)填充 (表中如果有創(chuàng)建時(shí)間、修改時(shí)間話,可以使用自動(dò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);
// 樂(lè)觀鎖配置
// strategy.setVersionFieldName("version");
mpg.setStrategy(strategy);
// 6.配置實(shí)體類模板
TemplateConfig templateConfig = new TemplateConfig();
// 如果setXxxxx(null) 不會(huì)生成Xxxx實(shí)體類相關(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 默認(rèn)*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<>();
// 自定義配置會(huì)被優(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實(shí)現(xiàn)逆向工程的文章就介紹到這了,更多相關(guān)mybatis-plus generator 逆向工程內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)企業(yè)員工管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)企業(yè)員工管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
Spring?AOP實(shí)現(xiàn)聲明式事務(wù)機(jī)制源碼解析
詳解spring mvc 請(qǐng)求轉(zhuǎn)發(fā)和重定向
java實(shí)現(xiàn)文本框和文本區(qū)的輸入輸出
解決java web應(yīng)用線上系統(tǒng)偶發(fā)宕機(jī)的情況
SpringBoot 添加JSP 支持并附帶在IDEA下創(chuàng)建JSP文件【測(cè)試無(wú)誤】
SpringCloud開(kāi)啟session共享并存儲(chǔ)到Redis的實(shí)現(xiàn)

