要在项目下,新建一个module,把下面代码复制进去,配置好数据库连接字符串和账号密码,然后运行main,输入模块名,输入包路径,输入表名,就自动生产了。
- pom.xml配置
<!—_swagger文档管理—>
_
applicaiton.xml配置
spring:
datasource:
name: mysql-connect
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/fxq-api?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
mybatis:
mapper-locations: classpath:mapping/*Mapper.xml
type-aliases-package: org.convert.pdfconvert.model.entity
3. 自动生成代码
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.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* @program: codeduck-auth
* @description: mybatis-plus代码生成器
* @author: wty
* @create: 2020-09-27 15:27
**/
public class CodeGenerateUtil {
/**
* 读取控制台内容
*/
public static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
StringBuilder help = new StringBuilder();
help.append("请输入" + tip + ":");
System.out.println(help.toString());
if (scanner.hasNext()) {
String ipt = scanner.next();
if (StringUtils.isNotEmpty(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确的" + tip + "!");
}
public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/fxq-api?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=UTC");
// dsc.setSchemaName("public");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("root");
mpg.setDataSource(dsc);
String projectPath = System.getProperty("user.dir");
String module_name = scanner("请输入要生成代码的模块名字:");
File module_url = new File(projectPath + "/" + module_name);
if(!module_url.exists()){
System.out.println("输入的模块不存在!");
return;
}
String page_name = scanner("请输入模块的包全路径,例如:org.wty.mybatis.codercreate,根据自己的包路径正确输入:");
String[] page_name_array = page_name.split("\\.");
System.out.println(page_name_array[0]+page_name_array[1]+page_name_array[2]);
String str = "";
int n = page_name_array.length;
for(int i = 0; i < n; i++){
str += page_name_array[i];
if(i != n -1) str = str + "/";
}
File page_url = new File(projectPath + "/" + module_name + "/src/main/java/" + str);
if(!page_url.exists()){
System.out.println("输入的包名路径不正确,请检查模块名对应的包路径!");
return;
}
// 全局配置
GlobalConfig gc = new GlobalConfig();
gc.setOutputDir(projectPath + "/" + module_name +"/src/main/java");
// gc.setOutputDir("D:\\test");
gc.setAuthor("wty");
gc.setOpen(false);
gc.setSwagger2(true); // 实体属性 Swagger2 注解
gc.setServiceName("%sService");
mpg.setGlobalConfig(gc);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setModuleName(null);
pc.setParent(page_name);
mpg.setPackageInfo(pc);
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 如果模板引擎是 freemarker
String templatePath = "/templates/mapper.xml.ftl";
// 如果模板引擎是 velocity
// String templatePath = "/templates/mapper.xml.vm";
// 自定义输出配置
List<FileOutConfig> focList = new ArrayList<>();
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return projectPath + "/" + module_name + "/src/main/resources/mapper/"
+ "/" + 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.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
strategy.setControllerMappingHyphenStyle(true);
strategy.setTablePrefix("tb_"); // 设置表前缀
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}