MyBatis-Plus 的代码生成器
代码直接copy就能使用
package com.test;
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.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 {
// TODO 文件目录,本地要存在,否则报错
public static String projectPath = System.getProperty("user.dir")+"/test/src/main/java";
//总包名
public static String parentPackage = "com.test";
// 读取控制台内容
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 + "!");
}
//全局配置
private static GlobalConfig globalConfig(){
GlobalConfig gc = new GlobalConfig();
gc.setAuthor("jgYang") //生成的文件头@author
.setOutputDir(projectPath) //代码文件生成路径
.setFileOverride(true) //文件是否覆盖
.setIdType(IdType.ID_WORKER_STR) // 主键策略
.setServiceName("I%sService") //是否去掉Service接口命名开头的I
.setBaseColumnList(true) // 是否生成基本的sql列
.setSwagger2(true) //实体属性 Swagger2 注解
;
return gc;
}
//数据源配置
private static DataSourceConfig dataSourceConfig(){
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/account?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8&rewriteBatchedStatements=true");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("123456");
return dsc;
}
// 包配置
private static PackageConfig packageConfig() {
PackageConfig packageConfig = new PackageConfig();
packageConfig.setParent(parentPackage); // 总包名
packageConfig.setEntity("entity"); // 实体包名
packageConfig.setController("controller"); //控制器包名
packageConfig.setService("service"); //业务包名
packageConfig.setMapper("mapper"); //Mapper包名
return packageConfig;
}
//自定义配置
private static InjectionConfig injectionConfig() {
// 自定义配置
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 + "/" +parentPackage.replace(".","/") + "/" +packageConfig().getMapper() + "/xml/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
return cfg;
}
// 策略配置
private static StrategyConfig strategyConfig(){
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityBuilderModel(true);
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
strategy.setControllerMappingHyphenStyle(true);
return strategy;
}
public static void main(String[] args) {
System.out.println(System.getProperty("user.dir"));
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
mpg.setGlobalConfig(globalConfig());
// 数据源配置
mpg.setDataSource(dataSourceConfig());
// 包配置
mpg.setPackageInfo(packageConfig());
// 自定义配置
mpg.setCfg(injectionConfig());
// 配置模板
mpg.setTemplate(new TemplateConfig().setXml(null));
// 策略配置
mpg.setStrategy(strategyConfig());
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}
常见错误
Exception in thread "main" java.lang.NoClassDefFoundError: freemarker/template/Configuration
at com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine.init(FreemarkerTemplateEngine.java:41)
at com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine.init(FreemarkerTemplateEngine.java:34)
at com.baomidou.mybatisplus.generator.AutoGenerator.execute(AutoGenerator.java:103)
at com.dream.road.CodeGenerator.main(CodeGenerator.java:136)
Caused by: java.lang.ClassNotFoundException: freemarker.template.Configuration
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
<dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.28</version> <scope>compile</scope> </dependency>
上一篇: DataTable相关操作,筛选,取前N条数据,获取指定列数据
下一篇: DataTable案例