引入依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.33</version>
</dependency>
快速生成
public class FastAutoGeneratorTest {
public static void main(String[] args) {
String url = "jdbc:mysql://127.0.0.1:3306/study?serverTimezone=GMT%2B8&characterEncoding=utf-8&useSSL=false";
String username = "root";
String password = "";
FastAutoGenerator.create(url, username, password)
.globalConfig(builder -> {
builder.author("qaqaq") // 设置作者
.enableSwagger() // 开启 swagger 模式
.outputDir("D://Java//Code//study//mybatisplus-create"); // 指定输出目录
})
.dataSourceConfig(builder ->
builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
int typeCode = metaInfo.getJdbcType().TYPE_CODE;
if (typeCode == Types.SMALLINT) {
// 自定义类型转换
return DbColumnType.INTEGER;
}
return typeRegistry.getColumnType(metaInfo);
})
)
.packageConfig(builder ->
builder.parent("top.qaqaq") // 设置父包名
.moduleName("mybatisplus") // 设置父包模块名
.pathInfo(Collections.singletonMap(OutputFile.xml, "D://Java//Code//study//mybatisplus-create")) // 设置mapperXml生成路径
)
.strategyConfig(builder ->
builder.addInclude("t_user") // 设置需要生成的表名
.addTablePrefix("t_", "c_") // 设置过滤表前缀
)
.templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
.execute();
}
}