MyBatis-Plus逆向生成工程
1.首先引入pom依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.2</version>
</dependency>
2.然后执行Java代码
package com.hzrc.user.main;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class MyBatisPlus {
public static void main(String[] args) {
FastAutoGenerator.create("jdbc:postgresql://localhost:5432/postgres?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8", "postgres", "123456")
.globalConfig(builder -> {
builder.author("xxx") // 设置作者
.enableSwagger() // 开启 swagger 模式
//.fileOverride() // 覆盖已生成文件
.outputDir("D:\\all\\java\\workspace\\first\\git\\hzrc-user\\src\\main\\java"); // 指定输出目录
})
.packageConfig(builder -> {
builder.parent("com.hzrc.user") // 设置父包名
.pathInfo(Collections.singletonMap(OutputFile.xml, "D:\\all\\java\\workspace\\first\\git\\hzrc-user\\src\\main\\resources\\mapper")); // 设置mapperXml生成路径
})
.strategyConfig(builder -> {
builder.addInclude(getTables("all")); // 设置需要生成的表名
})
.execute();
}
// 处理 all 情况
protected static List<String> getTables(String tables) {
return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
}
}
知是行之始,行是知之成