1 <dependency> 2 <groupId>org.projectlombok</groupId> 3 <artifactId>lombok</artifactId> 4 <version>1.18.0</version> 5 <scope>provided</scope> 6 </dependency> 7 <!-- Add code generator dependencies --> 8 <dependency> 9 <groupId>org.apache.velocity</groupId> 10 <artifactId>velocity-engine-core</artifactId> 11 <version>2.0</version> 12 </dependency>
Development tools: STS
Code download link: Github management code
Edition:
Springboot: 1.5.14.RELEASE
Using Springboot above 2.0 will report some exceptions. Welcome to know the reason of the exception.
MybatisPlus: 2.3
Please refer to: Generating rule
Preface:
I've used the most convenient code generator, and it's really powerful!
Build code generator:
1. Add dependency:
(1) Code generator dependency
(2) Lombok depends on
Using lombok can reduce code development
1 <dependency> 2 <groupId>org.projectlombok</groupId> 3 <artifactId>lombok</artifactId> 4 <version>1.18.0</version> 5 <scope>provided</scope> 6 </dependency> 7 <!-- Add code generator dependencies --> 8 <dependency> 9 <groupId>org.apache.velocity</groupId> 10 <artifactId>velocity-engine-core</artifactId> 11 <version>2.0</version> 12 </dependency>
2. Write generator:
1 package com.xm; 2 3 import org.junit.Test; 4 5 import com.baomidou.mybatisplus.enums.IdType; 6 import com.baomidou.mybatisplus.generator.AutoGenerator; 7 import com.baomidou.mybatisplus.generator.config.DataSourceConfig; 8 import com.baomidou.mybatisplus.generator.config.GlobalConfig; 9 import com.baomidou.mybatisplus.generator.config.PackageConfig; 10 import com.baomidou.mybatisplus.generator.config.StrategyConfig; 11 import com.baomidou.mybatisplus.generator.config.rules.DbType; 12 import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; 13 14 public class MpGenerator { 15 16 @Test 17 public void create() { 18 19 String tableName = "user"; 20 21 /** 22 * To configure: 23 * 1.Global configuration 24 * 2.Data source configuration 25 * 3.Policy configuration 26 * 4.Package name policy configuration 27 * 5.Integrated configuration 28 */ 29 30 //1.Global configuration 31 GlobalConfig globalConfig = new GlobalConfig(); 32 globalConfig 33 //Set code generation path 34 .setOutputDir("F:\\xm\\springboot\\Springboot_MybatisPlus\\src\\main\\java") 35 //Author setting 36 .setAuthor("xm") 37 //Set the opening and closing of the secondary cache 38 .setEnableCache(false) 39 //Set up database id Self increment 40 .setIdType(IdType.AUTO) 41 //Set override update 42 .setFileOverride(true) 43 //Set up I 44 .setServiceName("%sService") 45 //Set up production result mapping map 46 .setBaseResultMap(true); 47 48 //2.Data source configuration 49 DataSourceConfig dataSourceConfig = new DataSourceConfig(); 50 dataSourceConfig 51 //Set database type 52 .setDbType(DbType.MYSQL) 53 .setUrl("jdbc:mysql://10.1.51.31:3306/xm?useSSL=true") 54 .setUsername("root") 55 .setPassword("cube1501") 56 .setDriverName("com.mysql.jdbc.Driver"); 57 58 //3.Policy configuration 59 StrategyConfig strategyConfig = new StrategyConfig(); 60 strategyConfig 61 //Turn on global capital naming 62 .setCapitalMode(true) 63 //Enable underline conversion 64 .setDbColumnUnderline(true) 65 //Turn on hump naming 66 .setNaming(NamingStrategy.underline_to_camel) 67 .setEntityLombokModel(true) 68 .setInclude(tableName); 69 70 //4.Package name policy configuration 71 PackageConfig packageConfig = new PackageConfig(); 72 packageConfig 73 .setParent("com.xm") 74 .setController("controller") 75 .setEntity("pojo") 76 .setMapper("mapper") 77 .setService("service") 78 .setServiceImpl("service.impl") 79 .setXml("mapper.xml"); 80 81 //5.Integrated configuration 82 AutoGenerator autoGenerator = new AutoGenerator(); 83 autoGenerator 84 .setGlobalConfig(globalConfig) 85 .setDataSource(dataSourceConfig) 86 .setStrategy(strategyConfig) 87 .setPackageInfo(packageConfig); 88 89 autoGenerator.execute(); 90 91 } 92 93 }
3. Test run:
2018-07-19