我们在使用springboot 框架 和mybatis-plus 开发web项目的时候,像 控制器 这类的文件 有了这个基于mybatis-plus 的 代码生成器 我们就不必自己创建了 ,直接执行后 自动帮我们生成好控制器、服务处、实现层 等等 非常的方便 。
废话不多说,还是直接分享代码生成器的工具类~
package com.jsonl.base.db;
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.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.jsonl.base.entities.BaseEntity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
/**
* 老代码生成器适用于 3.5.1 以下版本
* 提醒:
* 在使用之前 需要把 文件里的 xxx 替换成 自己的com.xxx 的包前缀
* mysqlIp 改为 自己的数据库 ip 包括账户密码
* 把 java.vm 模板放到 resources 文件夹下
* **/
public class CodeGenerator {
/**
* <p>
* 读取控制台内容
* </p>
*/
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.isNotBlank(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确的" + tip + "!");
}
public static void Cg() {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
System.out.println(projectPath);
gc.setOutputDir(projectPath + "/src/main/java");
gc.setOpen(false);
gc.setSwagger2(true); //实体属性 Swagger2 注解
mpg.setGlobalConfig(gc);
String moduleName = scanner("模块名");
String packageName = scanner("包名");
String Author = scanner("创建人");
gc.setAuthor(Author);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
String db = scanner("数据库名");
dsc.setUrl("jdbc:mysql://mysqlIp:3306/" + db + "?useUnicode=true&useSSL=false&characterEncoding=utf8");
// dsc.setSchemaName("public");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("root");
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
// pc.setModuleName(moduleName);
pc.setParent("");
pc.setEntity("com.xxx." + packageName + ".entities");
pc.setService("com.xxx." + packageName + ".service.inteface");
pc.setServiceImpl("com.xxx." + packageName + ".service.impl");
pc.setController("com.xxx." + packageName + ".controller");
pc.setMapper("com.xxx." + packageName + ".mapper");
HashMap<String, String> pathMap = new HashMap<>();
pathMap.put(ConstVal.ENTITY, projectPath + "/" + "service/" + moduleName + "/src/main/java/com/xxx/" + packageName + "/entities");
pathMap.put(ConstVal.SERVICE, projectPath + "/" + "service/" + moduleName + "/src/main/java/com/xxx/" + packageName + "/service/inteface");
pathMap.put(ConstVal.MAPPER, projectPath + "/" + "service/" + moduleName + "/src/main/java/com/xxx/" + packageName + "/mapper");
pathMap.put(ConstVal.SERVICE_IMPL, projectPath + "/" + "service/" + moduleName + "/src/main/java/com/xxx/" + packageName + "/service/impl");
pathMap.put(ConstVal.CONTROLLER, projectPath + "/" + "service/" + moduleName + "/src/main/java/com/xxx/" + packageName + "/controller");
pc.setPathInfo(pathMap);
mpg.setPackageInfo(pc);
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
String mapperJavaPath = "/templates/mapper.java.vm";
String entityPath = "/templates/entity.java.vm";
String servicePath = "/templates/service.java.vm";
String serviceImplPath = "/templates/serviceImpl.java.vm";
String controllxxxath = "/templates/controller.java.vm";
// 自定义输出配置
List<FileOutConfig> focList = new ArrayList<>();
// 自定义配置会被优先输出
focList.add(new FileOutConfig(entityPath) {
@Override
public String outputFile(TableInfo tableInfo) {
return projectPath + "/service/" + moduleName + "/src/main/java/com/xxx/" + packageName + "/entities/" + tableInfo.getEntityName() + StringPool.DOT_JAVA;
}
});
focList.add(new FileOutConfig(mapperJavaPath) {
@Override
public String outputFile(TableInfo tableInfo) {
return projectPath + "/service/" + moduleName + "/src/main/java/com/xxx/" + packageName + "/mapper/" + tableInfo.getMapperName() + StringPool.DOT_JAVA;
}
});
focList.add(new FileOutConfig(servicePath) {
@Override
public String outputFile(TableInfo tableInfo) {
return projectPath + "/service/" + moduleName + "/src/main/java/com/xxx/" + packageName + "/service/inteface/" + tableInfo.getServiceName() + StringPool.DOT_JAVA;
}
});
focList.add(new FileOutConfig(serviceImplPath) {
@Override
public String outputFile(TableInfo tableInfo) {
return projectPath + "/service/" + moduleName + "/src/main/java/com/xxx/" + packageName + "/service/impl/" + tableInfo.getServiceImplName() + StringPool.DOT_JAVA;
}
});
focList.add(new FileOutConfig(controllxxxath) {
@Override
public String outputFile(TableInfo tableInfo) {
return projectPath + "/service/" + moduleName + "/src/main/java/com/xxx/" + packageName + "/controller/" + tableInfo.getControllerName() + StringPool.DOT_JAVA;
}
});
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.setSuperEntityClass(BaseEntity.class);
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
strategy.setChainModel(true);
// 公共父类
strategy.setSuperControllerClass("com.xxx." + packageName + ".BaseController");
// 写于父类中的公共字段
strategy.setSuperEntityColumns("id","created_at","updated_at","is_deleted");
String[] tables = scanner("表名,多个英文逗号分割").split(",");
strategy.setInclude(tables);
strategy.setControllerMappingHyphenStyle(true);
// String[] prefix = tables[0].split("_");
// if (prefix.length > 1)
// strategy.setTablePrefix(prefix[0]);
strategy.setTablePrefix(moduleName);
strategy.setEntityTableFieldAnnotationEnable(true);
mpg.setStrategy(strategy);
// mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
public static void main(String[] args) {
Cg();
}
// private static Map<String, String> path = new HashMap() {{
// put("skeleton", "/service/aaa");
// }};
}
实际代码生成工具类的实现方式 就是根据 预先写好的 代码模板,然后 生成出来我们想要的代码
像这种 就是代码的模板
因为模板文件过多 我直接整理好了。配合工具类 一起使用,有需要的小伙伴可用去下载使用~
https://www.wwwoop.com/home/Index/projectInfo?goodsId=32&typeParam=2