- 所需依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
<scope>provided</scope>
</dependency>
GeneratorCodeConfig配置类
package com.ruoyi.common.generator;
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.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.ruoyi.common.utils.OSUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* TODO mybatis-plus 逆向工程使用说明
* step1 配置需要生成的项目名称底下
* step2 配置数据库连接
* step3 配置生成java类的包位置
* step4 配置生成mapper.xml类的文件位置
* @author
*/
public class GeneratorCodeConfig {
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) {
AutoGenerator mpg = new AutoGenerator();
//1、全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
String os = System.getProperty("os.name").toLowerCase();
if (OSUtils.isWindows(os)){
gc.setOutputDir(projectPath +"/ruoyi-generator" + "/src/main/java");
}else {
gc.setOutputDir(projectPath +"/ruoyi/ruoyi-generator" + "/src/main/java");
}
gc.setAuthor("ruoyi");
gc.setDateType(DateType.ONLY_DATE);
gc.setOpen(false);
//第二次生成会把第一次生成的覆盖掉
gc.setFileOverride(true);
//生成的service接口名字首字母是否为I,这样设置就没有
gc.setServiceName("%sService");
//生成resultMap
gc.setBaseResultMap(true);
mpg.setGlobalConfig(gc);
//2、数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/ruoyi?useUnicode=true&serverTimezone=GMT&useSSL=false&characterEncoding=utf8");
dsc.setDriverName("com.mysql.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("root");
mpg.setDataSource(dsc);
//3、策略配置
StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig.setCapitalMode(true)//开启全局大写命名
//表名映射到实体的命名策略(下划线到驼峰)
.setNaming(NamingStrategy.underline_to_camel)
//表字段映射属性名策略(未指定按naming)
.setColumnNaming(NamingStrategy.underline_to_camel)
//.setSuperEntityClass("你自己的父类实体,没有就不用设置!")
//.setSuperEntityColumns("id");//写于父类中的公共字段
//.setSuperControllerClass("自定义继承的Controller类全称,带包名,没有就不用设置!")
//生成 @RestController 控制器
.setRestControllerStyle(true)
.setEntityLombokModel(true)
//逆向工程使用的表
.setInclude(scanner("请输入表名"))
.setSuperEntityColumns("id");
//4、包名策略配置
PackageConfig packageConfig = new PackageConfig();
packageConfig
//设置包名的parent
.setParent("com.ruoyi.generator")
.setMapper("mapper")
.setService("service")
//.setController("controller")
.setEntity("entity");
// 5、自定义配置(配置输出xml文件到resources下)
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
List<FileOutConfig> focList = new ArrayList<>();
String templatePath = "/templates/mapper.xml.vm";
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
if (OSUtils.isWindows(os)) {
return projectPath + "/ruoyi-generator/src/main/resources/mapper/"
+ tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}else {
return projectPath + "/ruoyi/ruoyi-generator/src/main/resources/mapper/generator"
+ tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
}
});
cfg.setFileOutConfigList(focList);
AutoGenerator autoGenerator = new AutoGenerator();
autoGenerator
.setGlobalConfig(gc)
.setDataSource(dsc)
.setPackageInfo(packageConfig)
.setStrategy(strategyConfig)
.setCfg(cfg)
.execute();
}
}
OSUtils类
package com.ruoyi.common.utils;
public class OSUtils {
public static boolean isMacOS(String os){
return os.indexOf("mac")>=0&&os.indexOf("os")>0&&os.indexOf("x")<0;
}
public static boolean isMacOSX(String os){
return os.indexOf("mac")>=0&&os.indexOf("os")>0&&os.indexOf("x")>0;
}
public static boolean isWindows(String os){
return os.indexOf("windows")>=0;
}
}
- mybatis-plus添加分页插件
package org.example.config;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* mybatis-plus 插件
*/
@Configuration
@MapperScan("org.example.model.mapper")
public class MybatisPlusConfig {
/**
* mybatis-plus 分页插件
* @return
*/
@Bean
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}
}
- 创建时间和修改时间全局处理
package org.example.config;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
/**
* 新增操作
*/
@Override
public void insertFill(MetaObject metaObject) {
this.setFieldValByName("createTime",new Date(), metaObject); //创建时间为系统时间
this.setFieldValByName("updateTime",new Date(), metaObject); //更新时间为系统时间
}
@Override
public void updateFill(MetaObject metaObject) {
this.setFieldValByName("updateTime",new Date(), metaObject);
}
}