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");
// 設置代碼生成路徑
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);
// 設置日期類型
gc.setDateType(DateType.ONLY_DATE);
// 是否生成Swagger
gc.setSwagger2(false);
// 生成entity類的后綴
gc.setEntityName("%sEntity");
mpg.setGlobalConfig(gc);
// 2.設置數(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();
// 設置模塊存放位置
pc.setParent("com.");
// 設置該模塊包的路徑
pc.setModuleName("dao");
pc.setEntity("entity");
pc.setMapper("mapper");
pc.setService("service");
pc.setController("controller");
mpg.setPackageInfo(pc);
// 4.策略配置
StrategyConfig strategy=new StrategyConfig();
// 設置要映射的表名 不配置默認處理全部表
// 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);
// 對表中的字段 設置邏輯刪除 生成的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實體類相關代碼
// 因此如果只生成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();
// 設置模塊存放位置
pc.setParent("com.");
// 設置該模塊包的路徑
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文件 防止重復生成
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 設置了前后綴
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);到此這篇關于mybatis-plus使用generator實現(xiàn)逆向工程的文章就介紹到這了,更多相關mybatis-plus generator 逆向工程內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java實現(xiàn)企業(yè)員工管理系統(tǒng)
這篇文章主要為大家詳細介紹了Java實現(xiàn)企業(yè)員工管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02
詳解spring mvc 請求轉(zhuǎn)發(fā)和重定向
解決java web應用線上系統(tǒng)偶發(fā)宕機的情況
SpringBoot 添加JSP 支持并附帶在IDEA下創(chuàng)建JSP文件【測試無誤】
SpringCloud開啟session共享并存儲到Redis的實現(xiàn)

