<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
精简版
package com.aw.demo1;
/**
* @Author 阿威
* @Date 2021/5/24
*/
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.LikeTable;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Generation {
/**
* <p>
* 读取控制台内容
* </p>
*/
public static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入" + tip + ":");
if (scanner.hasNext()) {
String ipt = scanner.next();
if (StringUtils.isNotBlank(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确的" + tip + "!");
}
public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");//获取当前项目名称
gc.setOutputDir(projectPath + "/src/main/java")//生成代码的位置
.setAuthor("aw")//设置作者信息
.setOpen(false)//不打开资源管理器
.setBaseResultMap(true)//在生成的mapper.xml中自动生成BaseResultMap
.setBaseColumnList(true)//开启列集和
.setFileOverride(false)//是否覆盖原来已有的文件
.setServiceName("%sService")//去除Service的I前缀
.setIdType(IdType.ASSIGN_ID)//默认生成ID 雪花算法
.setSwagger2(true); //实体属性 Swagger2 注解
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/数据库名?useUnicode=true&useSSL=false&characterEncoding=utf-8&serverTimezone=UTC")
.setDbType(DbType.MYSQL)
.setDriverName("com.mysql.cj.jdbc.Driver")
.setUsername("用户名")
.setPassword("用户名");
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setParent("com.aw.server")
.setEntity("model")
.setMapper("mapper")
.setService("service")
.setServiceImpl("service.impl")
.setController("controller");
mpg.setPackageInfo(pc);
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 如果模板引擎是 freemarker
String templatePath = "/templates/mapper.xml.ftl";
// 自定义输出配置
List<FileOutConfig> focList = new ArrayList<>();
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);//表名下划线转驼峰命名
strategy.setColumnNaming(NamingStrategy.underline_to_camel);//字段名下划线转驼峰命名
strategy.setRestControllerStyle(true);//Controller开启驼峰命名
strategy.setRestControllerStyle(true);//生成@RestController
strategy.setEntityLombokModel(true);//开启lombok编程
strategy.setChainModel(true);//开启链式编程
strategy.setTablePrefix("前缀名");//去除表前缀
// strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));//使用多个表名生成
strategy.setLikeTable(new LikeTable("前缀名")); //模糊获取表名前缀生成
mpg.setStrategy(strategy);//添加策略
mpg.setTemplateEngine(new FreemarkerTemplateEngine());//添加自己定义的模板
mpg.execute();//执行
}
}
完整版
package com.aw.demo1;
/**
* @Author 阿威
* @Date 2021/5/24
*/
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.LikeTable;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Generation {
/**
* <p>
* 读取控制台内容
* </p>
*/
public static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入" + tip + ":");
if (scanner.hasNext()) {
String ipt = scanner.next();
if (StringUtils.isNotBlank(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确的" + tip + "!");
}
public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");//获取当前项目名称
System.out.println("获取当前项目名称:"+projectPath);
gc.setOutputDir(projectPath + "/src/main/java")//生成代码的位置
.setAuthor("aw")//设置作者信息
.setOpen(false)//不打开资源管理器
.setBaseResultMap(true)//在生成的mapper.xml中自动生成BaseResultMap
.setBaseColumnList(true)//开启列集和
.setFileOverride(false)//是否覆盖原来已有的文件
.setServiceName("%sService")//去除Service的I前缀
.setIdType(IdType.ASSIGN_ID)//默认生成ID 雪花算法
.setSwagger2(true); //实体属性 Swagger2 注解
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/yeb?useUnicode=true&useSSL=false&characterEncoding=utf-8&serverTimezone=UTC")
.setDbType(DbType.MYSQL)
.setDriverName("com.mysql.cj.jdbc.Driver")
.setUsername("root")
.setPassword("root");
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
// pc.setModuleName("设置模块名");
pc.setParent("com.aw.server")
.setEntity("model")
.setMapper("mapper")
.setService("service")
.setServiceImpl("service.impl")
.setController("controller");
mpg.setPackageInfo(pc);
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 如果模板引擎是 freemarker
String templatePath = "/templates/mapper.xml.ftl";
// 自定义输出配置
List<FileOutConfig> focList = new ArrayList<>();
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);//表名下划线转驼峰命名
strategy.setColumnNaming(NamingStrategy.underline_to_camel);//字段名下划线转驼峰命名
strategy.setRestControllerStyle(true);//Controller开启驼峰命名
// strategy.setControllerMappingHyphenStyle(true);//开启浏览器地址支持下划线
strategy.setRestControllerStyle(true);//生成@RestController
// strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");
strategy.setEntityLombokModel(true);//开启lombok编程
strategy.setChainModel(true);//开启链式编程
//设置逻辑删除
strategy.setLogicDeleteFieldName("state");//逻辑删除的字段名
// strategy.setLogicDeleteFieldName("enable");//逻辑删除的字段名
//自动填充策略
ArrayList<TableFill> arrayList = new ArrayList<>();
arrayList.add(new TableFill("createTime", FieldFill.INSERT));
arrayList.add(new TableFill("updateTime", FieldFill.INSERT_UPDATE));
strategy.setTableFillList(arrayList);
//开启乐观锁
strategy.setVersionFieldName("version");
// 公共父类
// strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!");
// 写于父类中的公共字段
// strategy.setSuperEntityColumns("id");
strategy.setTablePrefix("t_");//去除表前缀
// strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));//使用多个表名生成
strategy.setLikeTable(new LikeTable("t_")); //模糊获取表名前缀生成
mpg.setStrategy(strategy);//添加策略
mpg.setTemplateEngine(new FreemarkerTemplateEngine());//添加自己定义的模板
mpg.execute();//执行
}
}