1 public class MyBatisPlusGenerator { 2 3 public static void main(String[] args) throws SQLException { 4 5 //1. 全局配置 6 GlobalConfig config = new GlobalConfig(); 7 config.setActiveRecord(true) // 是否支持AR模式 8 .setAuthor("zuo") // 作者 10 .setOutputDir("F:\stsworkspace\src\main\java") // 生成路径 11 .setFileOverride(true) // 文件覆盖 12 // .setIdType(IdType.AUTO) // 主键策略 13 .setServiceName("%sService") // 设置生成的service接口的名字的首字母是否为I 14 // IEmployeeService 15 .setBaseResultMap(true)//生成基本的resultMap 16 .setBaseColumnList(true);//生成基本的SQL片段 17 18 //2. 数据源配置 19 DataSourceConfig dsConfig = new DataSourceConfig(); 20 dsConfig.setDbType(DbType.MYSQL) // 设置数据库类型 21 .setDriverName("com.mysql.cj.jdbc.Driver") 22 .setUrl("jdbc:mysql://localhost:3306/demo?useUnicode=true&useSSL=false&serverTimezone=UTC") 23 .setUsername("root") 24 .setPassword("root"); 25 26 //3. 策略配置globalConfiguration中 27 StrategyConfig stConfig = new StrategyConfig(); 28 stConfig.setCapitalMode(true) //全局大写命名 29 .setDbColumnUnderline(true) // 指定表名 字段名是否使用下划线 30 .setNaming(NamingStrategy.underline_to_camel) // 数据库表映射到实体的命名策略 31 //.setTablePrefix("tbl_") 32 .setInclude(new String[]{"user"}); // 生成的表 33 34 // //4. 包名策略配置 35 // PackageConfig pkConfig = new PackageConfig(); 36 // pkConfig.setParent("com.imooc") 37 // .setMapper("dao")//dao 38 // .setService("service")//servcie 39 // .setController("controller")//controller 40 // .setEntity("entity") 41 // .setXml("mapper");//mapper.xml 42 //4. 包名策略配置 43 PackageConfig pkConfig = new PackageConfig(); 44 StrategyConfig sc=new StrategyConfig(); 45 46 pkConfig.setParent("com.imooc") 47 .setMapper("dao")//dao 48 .setService("service")//servcie 49 .setController("controller")//controller 50 .setEntity("entity") 51 .setXml("resource");//mapper.xml 52 53 //5. 整合配置 54 AutoGenerator ag = new AutoGenerator(); 55 ag.setGlobalConfig(config) 56 .setDataSource(dsConfig) 57 .setStrategy(stConfig) 58 .setPackageInfo(pkConfig); 59 60 //6. 执行 61 ag.execute(); 62 } 63 64 }
加入依赖包
1 <dependencies> 2 3 4 <dependency> 5 <groupId>com.baomidou</groupId> 6 <artifactId>mybatis-plus</artifactId> 7 <version>2.3</version> 8 </dependency> 13 14 <!-- 单元测试 --> 15 <dependency> 16 <groupId>junit</groupId> 17 <artifactId>junit</artifactId> 18 <version>4.11</version> 19 <scope>test</scope> 20 </dependency> 21 22 23 24 <!-- 日志 --> 25 <!-- 实现slf4j接口并整合 --> 26 <dependency> 27 <groupId>ch.qos.logback</groupId> 28 <artifactId>logback-classic</artifactId> 29 <version>1.1.1</version> 30 </dependency> 36 37 <!-- 数据库 --> 43 <!-- c3p0 --> 44 <dependency> 45 <groupId>com.mchange</groupId> 46 <artifactId>c3p0</artifactId> 47 <version>0.9.5.2</version> 48 </dependency> 51 <!-- Spring --> 52 <dependency> 53 <groupId>org.springframework</groupId> 54 <artifactId>spring-core</artifactId> 55 <version>4.3.10.RELEASE</version> 56 </dependency> 57 <dependency> 58 <groupId>org.springframework</groupId> 59 <artifactId>spring-beans</artifactId> 60 <version>4.3.10.RELEASE</version> 61 </dependency> 62 <dependency> 63 <groupId>org.springframework</groupId> 64 <artifactId>spring-webmvc</artifactId> 65 <version>4.3.10.RELEASE</version> 66 </dependency> 67 <dependency> 68 <groupId>org.springframework</groupId> 69 <artifactId>spring-jdbc</artifactId> 70 <version>4.3.10.RELEASE</version> 71 </dependency> 77 78 <!-- MP 代码生成工具需要的依赖1 velocity-engine-core 2 slf4j-api 3slf4j-log4j12 --> 79 <!-- Apache velocity --> 80 <dependency> 81 <groupId>org.apache.velocity</groupId> 82 <artifactId>velocity-engine-core</artifactId> 83 <version>2.0</version> 84 </dependency> 85 86 <!-- sfl4j --> 87 <dependency> 88 <groupId>org.slf4j</groupId> 89 <artifactId>slf4j-api</artifactId> 90 <version>1.7.7</version> 91 </dependency> 92 <dependency> 93 <groupId>org.slf4j</groupId> 94 <artifactId>slf4j-log4j12</artifactId> 95 <version>1.7.7</version> 96 </dependency> 97 98 <dependency> 99 <groupId>mysql</groupId> 100 <artifactId>mysql-connector-java</artifactId> 101 <version>8.0.9-rc</version> 102 </dependency> 103 <dependency> 104 <groupId>com.baomidou</groupId> 105 <artifactId>mybatis-plus</artifactId> 106 <version>2.1.4</version> 107 <scope>compile</scope> 108 </dependency> 109 </dependencies>