MyBatis-Plus-Generator
:自动生成 Controller Service Mapper/DAO层等的基本代码,免去自己去写实体类映射数据库的繁琐操作
添加依赖 1 2 3 4 5 6 7 8 9 10 11 12 13 14 <dependency > <groupId > com.baomidou</groupId > <artifactId > mybatis-plus-generator</artifactId > <version > 3.4.1</version > </dependency > <dependency > <groupId > org.apache.velocity</groupId > <artifactId > velocity-engine-core</artifactId > <version > 2.0</version > </dependency >
运行代码 在测试类中运行代码即可
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 import com.baomidou.mybatisplus.annotation.DbType;import com.baomidou.mybatisplus.annotation.IdType;import com.baomidou.mybatisplus.generator.AutoGenerator;import com.baomidou.mybatisplus.generator.config.DataSourceConfig;import com.baomidou.mybatisplus.generator.config.GlobalConfig;import com.baomidou.mybatisplus.generator.config.PackageConfig;import com.baomidou.mybatisplus.generator.config.StrategyConfig;import com.baomidou.mybatisplus.generator.config.rules.DateType;import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;public class MyBatisPlusGenerator { public static void main (String[] args) { GlobalConfig config = new GlobalConfig (); config.setActiveRecord(true ) .setAuthor("EastWind" ) .setOutputDir("D:\\test" ) .setFileOverride(true ) .setIdType(IdType.AUTO) .setDateType(DateType.ONLY_DATE) .setServiceName("%sService" ) .setEntityName("%s" ) .setBaseResultMap(true ) .setActiveRecord(false ) .setBaseColumnList(true ); DataSourceConfig dsConfig = new DataSourceConfig (); dsConfig.setDbType(DbType.MYSQL) .setDriverName("com.mysql.cj.jdbc.Driver" ) .setUrl("jdbc:mysql://localhost:13306/mall?serverTimezone=UTC&useSSL=false" ) .setUsername("root" ) .setPassword("密码" ); StrategyConfig stConfig = new StrategyConfig (); stConfig.setCapitalMode(true ) .setNaming(NamingStrategy.underline_to_camel) .setRestControllerStyle(true ) .setInclude("自己在数据库中的表名" ); PackageConfig pkConfig = new PackageConfig (); pkConfig.setParent("fun.eastwind.mall" ) .setMapper("mapper" ) .setService("service" ) .setController("controller" ) .setEntity("pojo" ); AutoGenerator ag = new AutoGenerator (); ag.setGlobalConfig(config) .setDataSource(dsConfig) .setStrategy(stConfig) .setPackageInfo(pkConfig); ag.execute(); System.out.println("======= Done 相关代码生成完毕 ========" ); } }
运行后就会在D:/test目录下发现自己生成的代码了!