自动生成controller、service、entity、mapper、mapper.xml等文件,单表的增删查改无需写sql。
参考demo:
github地址
mybatis-plus 參考文献
苞米豆官方文档
依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<!--自动生成代码依赖包-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.1</version>
<scope>test</scope>
</dependency>
<!--简化code对象组件-->
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.8.RELEASE</version>
</dependency>
<!--swagger 文档-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
<!--导出swagger文档需要模板的依赖包-->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.28</version>
<scope>test</scope>
</dependency>
<!--对象字段校验注解,如:@NotBlank @NotNull @NotEmpty-->
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>2.0.2</version>
</dependency>
<!--jemete测试包-->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.10</version>
<scope>test</scope>
</dependency>
生成代码类
codeGenerator.java
package com.xxkj.myzone;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
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.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/**
* Project code genertor
*
* @author zjx
*/
public class CodeGenerator {
/**
* Project package
*/
private static String projectPackage;
private static String outputFilePath;
private static String outputMapXmlFilePath;
private static String tableNames;
/**
* 包配置
*/
private static String setMapper;
private static String setEntity;
private static String setService;
private static String setController;
/**
* Database url
*/
private static String url;
/**
* Database username
*/
private static String username;
/**
* Database password
*/
private static String password;
/**
* Database driver class
*/
private static String driverClass;
/**
* 文件名后缀
*/
private static String fileSuffix = ".java";
/**
* Init database information
*/
static {
Properties properties = new Properties();
InputStream i = CodeGenerator.class.getResourceAsStream("/generatorConfig.properties");
try {
properties.load(i);
url = properties.getProperty("generator.jdbc.url").trim();
username = properties.getProperty("generator.jdbc.username").trim();
password = properties.getProperty("generator.jdbc.password").trim();
driverClass = properties.getProperty("generator.jdbc.driverClass").trim();
projectPackage = properties.getProperty("projectPackage").trim();
outputFilePath = properties.getProperty("outputFilePath").trim();
outputMapXmlFilePath = properties.getProperty("outputMapXmlFilePath").trim();
tableNames = properties.getProperty("tableNames").trim();
setMapper = properties.getProperty("setMapper").trim();
setEntity = properties.getProperty("setEntity").trim();
setService = properties.getProperty("setService").trim();
setController = properties.getProperty("setController").trim();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* main method, execute code generator
*/
public static void main(String[] args) {
String projectPath = System.getProperty("user.dir");
String javaPath = projectPackage.replaceAll("\\.", "/");
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
gc.setAuthor("zhangjingxin");
gc.setOpen(false); // 是否打开输出目录
//gc.setOutputDir(projectPath + "/src/main/java"); // 输出文件目录
gc.setFileOverride(true); // 是否覆盖已有文件
gc.setSwagger2(true); // 实体属性 Swagger2 注解
gc.setMapperName("%sMapper");
gc.setXmlName("%sMapper");
gc.setServiceName("%sService");
gc.setServiceImplName("%sServiceImpl");
gc.setBaseResultMap(true); // mapper.xml中生成BaseResultMap
gc.setActiveRecord(true);
gc.setBaseColumnList(true);
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl(url.trim());
//dsc.setSchemaName("public");
dsc.setUsername(username.trim());
dsc.setPassword(password.trim());
dsc.setDriverName(driverClass.trim());
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setParent(projectPackage.trim());
//pc.setModuleName("model名"); 自定义包名
pc.setMapper(setMapper.trim());
pc.setEntity(setEntity.trim());
pc.setService(setService.trim());
pc.setController(setController.trim());
mpg.setPackageInfo(pc);
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 自定义输出配置
List<FileOutConfig> focList = new ArrayList<>();
/**mapper xml文件*/
// 如果模板引擎是 freemarker
String templatePath = "/templates/mapper.xml.ftl";
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return projectPath + outputMapXmlFilePath + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
/**控制层*/
templatePath = "/templates/controller.java.ftl";
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 + pc.getModuleName()
String expand = projectPath + outputFilePath + "controller";
String entityFile = String.format(expand + File.separator + "%s" + fileSuffix, tableInfo.getControllerName());
return entityFile;
}
});
/**业务接口层*/
templatePath = "/templates/service.java.ftl";
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 + pc.getModuleName()
String expand = projectPath + outputFilePath + "service";
String entityFile = String.format(expand + File.separator + "%s" + fileSuffix, tableInfo.getServiceName());
return entityFile;
}
});
/**业务实现层*/
templatePath = "/templates/serviceImpl.java.ftl";
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 + pc.getModuleName()
String expand = projectPath + outputFilePath + "service/impl";
String entityFile = String.format(expand + File.separator + "%s" + fileSuffix, tableInfo.getServiceImplName());
return entityFile;
}
});
/**数据mapper层*/
templatePath = "/templates/mapper.java.ftl";
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 + pc.getModuleName()
String expand = projectPath + outputFilePath + "mapper";
String entityFile = String.format(expand + File.separator + "%s" + fileSuffix, tableInfo.getMapperName());
return entityFile;
}
});
/**数据entity层*/
templatePath = "/templates/entity.java.ftl";
// 自定义配置会优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 + pc.getModuleName()
String expand = projectPath + outputFilePath + "model/entity";
String entityFile = String.format(expand + File.separator + "%s" + fileSuffix, tableInfo.getEntityName());
return entityFile;
}
});
/*cfg.setFileOutConfigList(new IFileCreate() {
@Override
public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
// 判断自定义文件夹是否需要创建
checkDir("调用默认方法创建目录");
return false;
}
});*/
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setController(null);
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
//strategy.setSuperEntityClass("");
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
//strategy.setExclude("");
// 表名
strategy.setInclude(tableNames.split(","));
strategy.setControllerMappingHyphenStyle(true);
/**表前缀,要自动去除添加*/
//strategy.setTablePrefix("t_");
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}
配置文件
generatorConfig.properties
generator.jdbc.driverClass=com.mysql.cj.jdbc.Driver
generator.jdbc.url=jdbc:mysql://123.56.26.20:3308/resume?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
generator.jdbc.username=root
generator.jdbc.password=root
projectPackage=com.xxkj.swaggerdemo
outputFilePath=/swagger-demo/src/main/java/com/xxkj/swaggerdemo/
outputMapXmlFilePath=/swagger-demo/src/main/resources/mapper/
# 表名 多表之间逗号分隔
tableNames=my_test
# 包配置
setMapper=mapper
setEntity=model.entity
setService=service
setController=controller
yml新增配置
# mybatis config
mybatis-plus:
type-aliases-package: com.xxkj.myzone.model.entity
mapper-locations: classpath*:mapper/*Mapper.xml
# 这个配置会将执行的sql打印出来
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
启动类新增注解(mapper接口对应路径)
@MapperScan("com.xxkj.swaggerdemo.mapper")
mybatis-plus 參考文献
苞米豆官方文档