一、首先通过IDEA创建一个Springboot项目
二、引入相关依赖
1 <dependencies> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter-web</artifactId> 5 </dependency> 6 7 <dependency> 8 <groupId>org.springframework.boot</groupId> 9 <artifactId>spring-boot-starter-test</artifactId> 10 <scope>test</scope> 11 </dependency> 12 13 <!-- 引入mybatis-plus依赖 --> 14 <dependency> 15 <groupId>com.baomidou</groupId> 16 <artifactId>mybatis-plus-generator</artifactId> 17 <version>3.4.1</version> 18 </dependency> 19 20 <!-- 添加 模板引擎 依赖,MyBatis-Plus 支持 Velocity(默认) --> 21 <dependency> 22 <groupId>org.apache.velocity</groupId> 23 <artifactId>velocity-engine-core</artifactId> 24 <version>2.1</version> 25 </dependency> 26 27 <!-- 添加mybatis-plus依赖 --> 28 <dependency> 29 <groupId>com.baomidou</groupId> 30 <artifactId>mybatis-plus-boot-starter</artifactId> 31 <version>3.4.1</version> 32 </dependency> 33 34 <!-- 数据库驱动 --> 35 <dependency> 36 <groupId>mysql</groupId> 37 <artifactId>mysql-connector-java</artifactId> 38 <version>5.1.47</version> 39 </dependency> 40 41 <!-- lombok --> 42 <dependency> 43 <groupId>org.projectlombok</groupId> 44 <artifactId>lombok</artifactId> 45 </dependency> 46 47 <!-- 引入swagger --> 48 <dependency> 49 <groupId>io.springfox</groupId> 50 <artifactId>springfox-swagger2</artifactId> 51 <version>2.9.2</version> 52 </dependency> 53 54 <dependency> 55 <groupId>io.springfox</groupId> 56 <artifactId>springfox-swagger-ui</artifactId> 57 <version>2.9.2</version> 58 </dependency> 59 60 </dependencies>
三、在springboot全局配置文件中配置数据源
1 # 配置数据源 2 spring.datasource.driver-class-name=com.mysql.jdbc.Driver 3 spring.datasource.url=jdbc:mysql://localhost:3306/netshop?useUnicode=true&useSSL=false&characterEncoding=utf-8 4 spring.datasource.username=root 5 spring.datasource.password=123
四、编写MybatisPlus代码生成器的工具代码
1 package com.lzp; 2 3 import com.baomidou.mybatisplus.annotation.DbType; 4 import com.baomidou.mybatisplus.annotation.FieldFill; 5 import com.baomidou.mybatisplus.annotation.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.po.TableFill; 12 import com.baomidou.mybatisplus.generator.config.rules.DateType; 13 import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; 14 import org.junit.jupiter.api.Test; 15 import org.springframework.boot.test.context.SpringBootTest; 16 import java.util.ArrayList; 17 import java.util.List; 18 19 @SpringBootTest 20 class MybatisPlusNetshopApplicationTests { 21 22 @Test 23 void contextLoads() { 24 // 构造一个AutoGenerator 25 AutoGenerator autoGenerator = new AutoGenerator(); 26 27 // 配置 GlobalConfig 28 GlobalConfig gc = new GlobalConfig(); 29 gc.setOutputDir(System.getProperty("user.dir") + "/src/main/java"); 30 gc.setAuthor("LZP"); 31 gc.setOpen(false); 32 // 是否覆盖 33 gc.setFileOverride(false); 34 // 去Service的I前缀 35 gc.setServiceName("%sService"); 36 gc.setIdType(IdType.ID_WORKER); 37 gc.setDateType(DateType.ONLY_DATE); 38 gc.setSwagger2(true); 39 autoGenerator.setGlobalConfig(gc); 40 41 // 配置 DataSourceConfig 42 DataSourceConfig dataSourceConfig = new DataSourceConfig(); 43 dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/netshop?useUnicode=true&useSSL=false&characterEncoding=utf-8"); 44 dataSourceConfig.setDriverName("com.mysql.jdbc.Driver"); 45 dataSourceConfig.setUsername("root"); 46 dataSourceConfig.setPassword("123"); 47 dataSourceConfig.setDbType(DbType.MYSQL); 48 autoGenerator.setDataSource(dataSourceConfig); 49 50 // 配置包 51 PackageConfig pc = new PackageConfig(); 52 pc.setModuleName("netshop"); 53 pc.setParent("com.lzp"); 54 pc.setController("controller"); 55 pc.setService("service"); 56 pc.setMapper("mapper"); 57 pc.setEntity("pojo"); 58 autoGenerator.setPackageInfo(pc); 59 60 // 策略配置 61 StrategyConfig strategy = new StrategyConfig(); 62 strategy.setNaming(NamingStrategy.underline_to_camel); 63 strategy.setColumnNaming(NamingStrategy.underline_to_camel); 64 strategy.setEntityLombokModel(true); 65 // 逻辑删除 66 strategy.setLogicDeleteFieldName("deleted"); 67 // 配置自动填充 68 TableFill gmtCreate = new TableFill("create_time", FieldFill.INSERT); 69 TableFill gmtUpdate = new TableFill("update_time", FieldFill.INSERT_UPDATE); 70 List<TableFill> list = new ArrayList<>(); 71 list.add(gmtCreate); 72 list.add(gmtUpdate); 73 strategy.setTableFillList(list); 74 // 乐观锁 75 strategy.setVersionFieldName("version"); 76 strategy.setRestControllerStyle(true); 77 // localhost:8080/hello_id_2 78 strategy.setControllerMappingHyphenStyle(true); 79 autoGenerator.setStrategy(strategy); 80 81 // 执行 82 autoGenerator.execute(); 83 } 84 85 }
运行效果: