引入依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.3.2</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.28</version>
</dependency>
自动生成代码
/**
* 配置详情
* https://baomidou.com/pages/981406/
*/
public class AutoGeneratorCode {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/gogogo?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8&useSSL=false";
String username = "root";
String password = "root";
String outputDir = "xxxxxx/shangan/src/main/java";
String mapperOutputDir = "xxxxxx/shangan/src/main/resources/mapper";
FastAutoGenerator.create(url, username, password)
// 全局配置
.globalConfig(builder -> {
builder
.author("xxxxxx") // 设置作者
.outputDir(outputDir); // 指定输出目录
})
.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("lzc.shangan") // 设置父包名
//.moduleName("system") // 设置父包模块名
.pathInfo(Collections.singletonMap(OutputFile.xml, mapperOutputDir)); // 设置mapperXml生成路径
})
.strategyConfig(builder -> {
builder
.addInclude("words_info") // 设置需要生成的表名
.addTablePrefix("t_", "c_") // 设置过滤表前缀
.entityBuilder().enableLombok();
})
.templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
.execute();
}
}