MyBatis Plus Code Generation
- 配置依赖
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation group: 'com.baomidou', name: 'mybatis-plus-boot-starter', version: '3.5.4'
implementation group: 'com.baomidou', name: 'mybatis-plus-generator', version: '3.5.6'
// change before version 2.3.9
// maybe have a error in init,so update the version
implementation group: 'freemarker', name: 'freemarker', version: '2.3.31'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.oracle.database.jdbc:ojdbc8'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
- 编写生成器类
演示例子,执行 main 方法控制台输入 模块名 表名 回车自动生成对应项目目录中
public class NewCodeGenerator {
static final String URL = "jdbc:oracle:thin:@192.168.3.161:1521:jc";
static final String USERNAME = "jc";
static final String PASSWORD = "123456";
public static void main(String[] args) {
generate();
}
private static void generate() {
FastAutoGenerator.create(URL, USERNAME, PASSWORD)
.globalConfig(builder ->
builder.author("xx")
.commentDate("yyyy-MM-dd")
.disableOpenDir()
.outputDir(System.getProperty("user.dir")+"\\src\\main\\java")
.dateType(DateType.TIME_PACK)
.disableServiceInterface()
)
.packageConfig(builder ->
builder.parent("com.whalefall541.mybatisplus.samples.generator")
.moduleName("system")
.entity("po")
.serviceImpl("service.impl")
.pathInfo(Collections.singletonMap(OutputFile.xml, System.getProperty("user.dir")+"\\src\\main\\resources\\mapper\\test")))
.strategyConfig(builder ->
builder.addInclude("table_a")
.controllerBuilder()
.enableFileOverride()
.enableRestStyle()
.disable()
.entityBuilder()
.enableFileOverride()
.enableLombok()
.disableSerialVersionUID()
.enableTableFieldAnnotation()
.enableActiveRecord()
.enableFileOverride()
// .addTableFills(new Column("timecolunmn", FieldFill.INSERT))
.formatFileName("%sPO")
.serviceBuilder()
.disable()
.enableFileOverride()
.formatServiceImplFileName("%sServiceImpl")
.mapperBuilder()
.enableFileOverride()
.mapperAnnotation(Mapper.class)
).templateEngine(new FreemarkerTemplateEngine()).execute();
}
}
Agreement
The code part of this work is licensed under Apache License 2.0 . You may freely modify and redistribute the code, and use it for commercial purposes, provided that you comply with the license. However, you are required to:
- Attribution: Retain the original author's signature and code source information in the original and derivative code.
- Preserve License: Retain the Apache 2.0 license file in the original and derivative code.
- Attribution: Give appropriate credit, provide a link to the license, and indicate if changes were made.
- NonCommercial: You may not use the material for commercial purposes. For commercial use, please contact the author.
- ShareAlike: If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.