使用mybatisplus一键生成通用代码。简单方便。
package com.clife.cvisionweb.generator;
import com.baomidou.mybatisplus.annotation.DbType;
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.util.ArrayList;
import java.util.List;
import java.util.Scanner;
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.isNotEmpty(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确的" + tip + "!");
}
public static void main(String[] args) throws InterruptedException {
AutoGenerator mpg = new AutoGenerator();
// 1.选择 freemarker 引擎,默认 Veloctiy
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
// 2.全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java");
gc.setFileOverride(true);
gc.setActiveRecord(true);
gc.setEnableCache(false);// XML 二级缓存
gc.setBaseResultMap(true);// XML ResultMap
gc.setBaseColumnList(true);// XML columList
gc.setOpen(false);
//使用swagger2
// gc.setSwagger2(true);
//设置主键生成策略 默认数据库自动生成
// gc.setIdType(IdType.ID_WORKER_STR);
gc.setAuthor("rookie");
// 自定义文件命名,注意 %s 会自动填充表实体属性!
gc.setMapperName("%sMapper");
gc.setXmlName("%sMapper");
gc.setServiceName("%sService");
gc.setServiceImplName("%sServiceImpl");
gc.setControllerName("%sController");
mpg.setGlobalConfig(gc);
// 3.数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setDbType(DbType.MYSQL);
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUrl("jdbc:mysql://10.6.14.1:3306/db_kgp_web?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8");
dsc.setUsername("root");
dsc.setPassword("123456");
mpg.setDataSource(dsc);
// 4.包配置
PackageConfig pc = new PackageConfig();
pc.setParent("com.clife.bigdata.api.web.kgp");
//包名默认即可
// pc.setController( "controller");
// pc.setEntity( "entity" );
// pc.setMapper("mapper");
// pc.setService("service");
// pc.setServiceImpl("impl");
// pc.setModuleName("test");
mpg.setPackageInfo(pc);
// 5.自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
List<FileOutConfig> focList = new ArrayList<>();
// Veloctiy模板
// focList.add(new FileOutConfig("/templates/mapper.xml.vm") {
// @Override
// public String outputFile(TableInfo tableInfo) {
// // 自定义输入文件名称
// return projectPath + "/src/main/resources/mapper/"
// + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
// }
// });
// freemark 输出模板 mapper.xml 模板
focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输入文件名称 注意如果pc.muduleName 没有设置会生成“null”字符串
return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()+"/"
+ tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
// 配置自定义输出模板
//指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
// templateConfig.setEntity("templates/entity2.java");
// templateConfig.setService();
// templateConfig.setController();
// ======================================================================================
// 自定义controller的代码模板
//String templatePath = "/template/controller.java";
// 自定义输出配置
// 自定义配置会被优先输出
// focList.add(new FileOutConfig(templatePath) {
// @Override
// public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 + pc.getModuleName()
// String expand = projectPath + "/cloud-api/src/main/java/com/api/" +pc.getModuleName() + "/" + "controller";
// String entityFile = String.format((expand + File.separator + "%s" + ".java"), tableInfo.getControllerName());
// return entityFile;
// }
// });
// 自定义mapper.xml的代码模板
// templatePath = "/template/mapper.xml";
// 自定义配置会被优先输出
// focList.add(new FileOutConfig(templatePath) {
// @Override
// public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 + pc.getModuleName()
// return projectPath + "/cloud-api/src/main/resources/mapper/"
// + "/" + tableInfo.getEntityName() + "Mapper" +StringPool.DOT_XML;
// }
// });
//=========================================================================================
// 6.设置模板
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
TemplateConfig templateConfig = new TemplateConfig();
//自定义了哪个就把哪个设置为null,关闭默认生成;此处只是定义了mapper.xml 的模板;如果自定义controller的模板,
// 就要设置 templateConfig.setController(null);
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
// 7.策略配置
StrategyConfig strategy = new StrategyConfig();
//此处可以修改为您的表前缀
strategy.setTablePrefix(new String[]{"tb_"});
// 表名生成策略
strategy.setNaming(NamingStrategy.underline_to_camel);
//数据表列生成策略
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
// 需要生成的表,两种方式本子一样
strategy.setInclude(new String[]{"tb_user"});
// strategy.setInclude(scanner("tb_employee,user").split(","));
// 排除生成的表
//strategy.setExclude(new String[]{"test"});
//is_xxx 去除is_ 前缀
strategy.setEntityBooleanColumnRemoveIsPrefix(true);
//字段常量
strategy.setEntityColumnConstant(true);
strategy.setEntityLombokModel(true);
//设置rest风格controller
strategy.setRestControllerStyle(true);
mpg.setStrategy(strategy);
// 执行生成
mpg.execute();
}
}