生成entity -> mapper-> service ->controller所有的接口,实现,一键完成。
1. 轮子
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><!-- mybatis plus --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.1</version></dependency><!-- code generator! --><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.2</version></dependency><!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.9.2</version></dependency><!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui --><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.9.2</version></dependency>
2. 核心代码
开箱即用。配置好数据库地址
package cn.bitqian;import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
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.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;import java.util.ArrayList;
import java.util.List;/*** 使用mybatis 生成代码 all** <a>* https://baomidou.com/config/generator-config.html* </p>* @author echo lovely* @date 2020/11/17 09:07*/public class CodeGenerator {public static void main(String[] args) {// 代码生成器AutoGenerator autoGenerator = new AutoGenerator();// 全局配置GlobalConfig globalConfig = new GlobalConfig();globalConfig.setFileOverride(false);globalConfig.setOpen(false);// 出掉接口globalConfig.setServiceName("%sService");// 设置输出目录globalConfig.setOutputDir(System.getProperty("user.dir") + "/src/main/java");globalConfig.setAuthor("echo lovely");globalConfig.setDateType(DateType.ONLY_DATE);// 开启swaggerglobalConfig.setSwagger2(true);autoGenerator.setGlobalConfig(globalConfig);// dataSource 配置DataSourceConfig dataSourceConfig = new DataSourceConfig();dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/mybatis_study?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai");dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");dataSourceConfig.setUsername("root");dataSourceConfig.setPassword("123456");dataSourceConfig.setDbType(DbType.MYSQL);autoGenerator.setDataSource(dataSourceConfig);// 包配置PackageConfig pkgConfig = new PackageConfig();pkgConfig.setParent("cn.bitqian");pkgConfig.setMapper("mapper");pkgConfig.setEntity("entity");pkgConfig.setService("service");pkgConfig.setController("controller");autoGenerator.setPackageInfo(pkgConfig);// 设置模板引擎autoGenerator.setTemplateEngine(null);autoGenerator.setTemplate(null);// 自定义策略StrategyConfig strategy = new StrategyConfig();// camel命名规则strategy.setNaming(NamingStrategy.underline_to_camel);strategy.setColumnNaming(NamingStrategy.underline_to_camel);strategy.setControllerMappingHyphenStyle(true); // id_1_1strategy.setRestControllerStyle(true);// 开启lombok注解strategy.setEntityLombokModel(true);// 设置要生成的表 ... 这里有三个表strategy.setInclude("account", "user", "role");// 逻辑删除 的字段// strategy.setLogicDeleteFieldName("deleted");// 自动填充配置// 自动填充的字段List<TableFill> tableFillList = new ArrayList<>();TableFill fill1 = new TableFill("date", FieldFill.INSERT);tableFillList.add(fill1);// 设置填充的字段strategy.setTableFillList(tableFillList);// 乐观锁 字段strategy.setVersionFieldName("version");autoGenerator.setStrategy(strategy);// 开启代码生成...autoGenerator.execute();}}