日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

实用的java代码生成器,开箱即用(基于mybatisplus的AutoGenerator)

發布時間:2024/7/23 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 实用的java代码生成器,开箱即用(基于mybatisplus的AutoGenerator) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

spring-boot工程下,自動生成代碼,controller層,service層,mapper層。
根據數據庫配置好的表,逆向生成實體類和各層結構

1、引入maven

<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.0.6</version></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus</artifactId><version>3.0.6</version></dependency><!-- MySql --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><!-- velocity 模板引擎 --><dependency><groupId>org.apache.velocity</groupId><artifactId>velocity-engine-core</artifactId><version>2.0</version></dependency><!-- swagger --><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.8.0</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.8.0</version></dependency>

2、編寫CodeGenerator類,執行main方法

我希望配置一個com.parent.module的模塊,本地數據庫名字是aa,得事先建好,表名是product

public class CodeGenerator {public static void main(String[] args) {// 1、創建代碼生成器AutoGenerator mpg = new AutoGenerator();// 2、全局配置GlobalConfig gc = new GlobalConfig();String projectPath = System.getProperty("user.dir");gc.setOutputDir(projectPath + "/src/main/java");gc.setAuthor("zs"); // 開發人員gc.setOpen(false); // 生成后是否打開資源管理器gc.setFileOverride(false); // 重新生成時文件是否覆蓋gc.setServiceName("%sService"); // 去掉Service接口的首字母Igc.setIdType(IdType.ID_WORKER_STR); // 主鍵策略gc.setDateType(DateType.ONLY_DATE);// 定義生成的實體類中日期類型gc.setSwagger2(true); // 開啟Swagger2模式mpg.setGlobalConfig(gc);// 3、數據源配置DataSourceConfig dsc = new DataSourceConfig();dsc.setUrl("jdbc:mysql://localhost:3306/aa?serverTimezone=GMT%2B8"); // 驅動連接的urldsc.setDriverName("com.mysql.cj.jdbc.Driver"); // 驅動名稱dsc.setUsername("root"); // 用戶名dsc.setPassword("root"); // 密碼dsc.setDbType(DbType.MYSQL);mpg.setDataSource(dsc);// 4、包配置PackageConfig pc = new PackageConfig();pc.setModuleName("moduleName"); // 模塊名pc.setParent("com.parent");pc.setController("controller");pc.setEntity("entity");pc.setService("service");pc.setMapper("mapper");mpg.setPackageInfo(pc);// 5、策略配置StrategyConfig strategy = new StrategyConfig();strategy.setInclude("product"); // 表名稱strategy.setNaming(NamingStrategy.underline_to_camel);// 數據庫表映射到實體的命名策略strategy.setTablePrefix(pc.getModuleName() + "_"); // 生成實體時去掉表前綴strategy.setColumnNaming(NamingStrategy.underline_to_camel);// 數據庫表字段映射到實體的命名策略strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter鏈式操作strategy.setRestControllerStyle(true); // restful api風格控制器strategy.setControllerMappingHyphenStyle(true); // url中駝峰轉連字符mpg.setStrategy(strategy);// 6、執行mpg.execute();} }

3、最終執行效果

總結

以上是生活随笔為你收集整理的实用的java代码生成器,开箱即用(基于mybatisplus的AutoGenerator)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。