Files
personal-blog/src/test/java/generator/SCloudCodeGenerator.java
2025-08-09 00:22:00 +08:00

60 lines
2.7 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package generator;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.sql.Types;
import java.util.Collections;
/**
* 代码生成器
*
* @author <a href="mailto:root@serliunx.com">SerLiunx</a>
* @since 2025/8/9
*/
public class SCloudCodeGenerator {
private static final String outputPath = "C:\\Users\\serliunx\\Desktop\\code-generate";
public static void main(String[] args) {
FastAutoGenerator.create(args[0], args[1], args[2])
.globalConfig(builder -> {
builder.author("<a href=\"mailto:root@serliunx.com\">SerLiunx</a>") // 设置作者
.outputDir(outputPath); // 指定输出目录
})
.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("com.serliunx.blog") // 设置父包名
.pathInfo(Collections.singletonMap(OutputFile.xml, outputPath)) // 设置mapperXml生成路径
)
.strategyConfig(builder ->
builder.addInclude("pb_article", "pb_tag", "pb_article_tag") // 设置需要生成的表名
.entityBuilder()
.enableLombok()
.disableSerialVersionUID()
.enableTableFieldAnnotation()
.controllerBuilder()
.template("/templates/controller.java")
.enableHyphenStyle()
.enableRestStyle()
.serviceBuilder()
.formatServiceFileName("%sService")
.mapperBuilder()
.disableMapperXml()
)
.templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板默认的是Velocity引擎模板
.execute();
}
}