<properties>
<mybatis-plus-boot-starter.version>3.1.2</mybatis-plus-boot-starter.version>
<springfox-swagger.version>2.9.2</springfox-swagger.version>
</properties>
<!--mybatisplus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus-boot-starter.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-typehandlers-jsr310</artifactId>
<version>1.0.1</version>
</dependency>
<!--mybatisplus generator-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>${mybatis-plus-boot-starter.version}</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.1</version>
</dependency>
<!--swagger2用于model说明-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox-swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox-swagger.version}</version>
</dependency>
<!--用于自定义模板中引入-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
<optional>true</optional>
</dependency>
package com.study.mybatisplus.util;
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.VelocityTemplateEngine;
import com.sun.deploy.util.StringUtils;
import java.util.*;
import java.util.stream.Collectors;
public class CodeGenerator {
private static class DbConfig {
private String dbName;
private String ip;
private String port;
private String username;
private String password;
public DbConfig(String dbName, String ip, String port, String username, String password) {
this.dbName = dbName;
this.ip = ip;
this.port = port;
this.username = username;
this.password = password;
}
public String getDbName() {
return dbName;
}
public void setDbName(String dbName) {
this.dbName = dbName;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
/**
*
* 读取控制台内容
*
*/
public static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
System.out.println(tip + ":");
return scanner.nextLine();
}
public static void main(String[] args) {
List<DbConfig> dbConfigs = new ArrayList<>();
dbConfigs.add(new DbConfig("springboot" , "127.0.0.1" , "3306" , "root" , "123456"));
List<String> dbnames = dbConfigs.stream().map(m -> m.dbName).collect(Collectors.toList());
String dbname = scanner("请输入库名,可选:" + StringUtils.join(dbnames, ","));
DbConfig dbConfig = dbConfigs.stream().filter(m -> m.getDbName().equals(dbname)).findFirst().orElse(null);
if (dbConfig == null) {
System.out.println("库名不存在");
return;
}
String[] tables = scanner("请输入表名,多个英文逗号分割").split(",");
boolean changePath = scanner("输入y生成到c盘根目录,默认回车生成到项目").equalsIgnoreCase("y");
String generatePath = changePath ? "c:/mybatis-plus/" : System.getProperty("user.dir") + "/src/main/";
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
gc.setOutputDir(generatePath + "java/");
gc.setAuthor("mybatis-plus生成");
gc.setOpen(false);
gc.setSwagger2(true); //实体属性 Swagger2 注解
gc.setBaseResultMap(true);
gc.setBaseColumnList(true);
gc.setEntityName("%sEntity");
gc.setMapperName("%sMapper");
gc.setFileOverride(changePath); //生成到项目时不覆盖源文件
gc.setOpen(changePath); //弹出生成的文件夹窗口
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://" + dbConfig.getIp() + ":" + dbConfig.getPort() + "/" + dbname + "?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=GMT");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername(dbConfig.getUsername());
dsc.setPassword(dbConfig.getPassword());
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setParent(changePath ? "auto" : "com.study.mybatisplus");
pc.setMapper("mapper");
pc.setEntity("domain");
mpg.setPackageInfo(pc);
// 模板配置
TemplateConfig tc = new TemplateConfig();
tc.setController(null);
tc.setService(null);
tc.setServiceImpl(null);
tc.setXml(null);
mpg.setTemplate(tc);
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
Map<String, Object> map = new HashMap<>();
map.put("dbname" , dbname);
this.setMap(map);
}
};
// 如果模板引擎是 velocity
String templatePath = "/templates/mapper.xml.vm";
String entitytemplatePath = "/templates/entity.java.vm";
String mappertemplatePath = "/templates/mapper.java.vm";
// 自定义输出配置
List<FileOutConfig> focList = new ArrayList<>();
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return generatePath + "resources/mapper/"
+ tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
focList.add(new FileOutConfig(entitytemplatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名
return generatePath + "java/com/study/mybatisplus/domain/"
+ tableInfo.getEntityName() + StringPool.DOT_JAVA;
}
});
focList.add(new FileOutConfig(mappertemplatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名
return generatePath + "java/com/study/mybatisplus/mapper/"
+ tableInfo.getMapperName() + StringPool.DOT_JAVA;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setInclude(tables);
strategy.setEntityLombokModel(true); //开启lombook支持
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new VelocityTemplateEngine());
mpg.execute();
}
}
@Configuration
@MapperScan("com.study.mybatisplus.mapper")
public class MybatisPlusConfig {
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
在application.yml中指定Mapper类输入debug日志
logging:
level:
com.study.mybatisplus.mapper: debug