本文最后更新于:2023年11月9日 晚上
MyBatis-Plus代码生成器
AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率。
1.在SpringBoot项目中导入Maven依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <!--mybatis-plus--> <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> <!--Velocity--> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-engine-core</artifactId> <version>2.2</version> </dependency>
|
2.编写配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| @Test public void code() {
AutoGenerator autoGenerator = new AutoGenerator();
GlobalConfig gc = new GlobalConfig(); String property = System.getProperty("user.dir"); gc.setOutputDir(property + "/src/main/java"); gc.setAuthor("阿波"); gc.setOpen(false); gc.setFileOverride(false); gc.setIdType(IdType.ID_WORKER); gc.setServiceName("%sService"); gc.setDateType(DateType.ONLY_DATE); gc.setSwagger2(true); gc.setEnableCache(true);
autoGenerator.setGlobalConfig(gc);
DataSourceConfig dataSource = new DataSourceConfig(); dataSource.setUrl("jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=Asia/Shanghai"); dataSource.setDriverName("com.mysql.cj.jdbc.Driver"); dataSource.setUsername("root"); dataSource.setPassword("libo"); dataSource.setDbType(DbType.MYSQL);
autoGenerator.setDataSource(dataSource);
PackageConfig pc = new PackageConfig(); pc.setModuleName("codeTest"); pc.setParent("com.libo"); pc.setController("controller");
pc.setMapper("mapper"); pc.setService("service");
autoGenerator.setPackageInfo(pc);
StrategyConfig strategy = new StrategyConfig(); strategy.setInclude("user"); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityLombokModel(true); strategy.setLogicDeleteFieldName("deleted"); strategy.setVersionFieldName("version"); TableFill cteateTime = new TableFill("cteate_time", FieldFill.INSERT_UPDATE); TableFill updateTime = new TableFill("update_time", FieldFill.UPDATE); ArrayList<TableFill> tableFills = new ArrayList<>(); tableFills.add(cteateTime); tableFills.add(updateTime); strategy.setTableFillList(tableFills); strategy.setRestControllerStyle(true); strategy.setControllerMappingHyphenStyle(true);
autoGenerator.setStrategy(strategy);
autoGenerator.execute(); }
|
3.运行后的效果
如果不能忠于自己的心,胜负又有什么价值呢?