本文将详细介绍mybatisplus-generator的相关使用,mybatisplus-generator是 MyBatis Plus 框架中的一个代码生成工具,它能够自动生成与数据库表对应的 Java 实体类、Mapper 接口以及 XML 配置文件。它的出现极大地提高了开发效率,让开发者能够将更多的精力集中在业务逻辑上,而不是繁琐的 CRUD 操作上。
使用mybatisplus-generator可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率。
博客这里使用的是.vm模板引擎,所以需要手动引入velocity的依赖;
<dependency>
<groupId>org.apache.velocitygroupId>
<artifactId>velocity-engine-coreartifactId>
<version>2.0version>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-generatorartifactId>
<version>3.5.1version>
dependency>
以下是mybatisplus官方提供的可交互是的代码生成的例子:
public class MyBatisPlusGenerator {
// 处理 all 情况
protected static List<String> getTables(String tables) {
return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
}
public static void main(String[] args) {
FastAutoGenerator.create("jdbc:mysql://localhost:3306/mengjie", "root", "123456")
// 全局配置
.globalConfig((scanner, builder) -> {
builder.author(scanner.apply("请输入作者名称?")).fileOverride();
builder.enableSwagger();
builder.outputDir("E:\\study\\SpringCloud\\mybatisplus\\src\\main\\java");
})
// 包配置
.packageConfig((scanner, builder) -> {
builder.parent(scanner.apply("请输入包名?"));
})
// 策略配置
.strategyConfig((scanner, builder) -> {
builder.addInclude(MyBatisPlusGenerator.getTables(scanner.apply("请输入表名,多个英文逗号分隔?所有输入 all")))
.controllerBuilder().enableRestStyle().enableHyphenStyle().build();
builder.serviceBuilder()
.formatServiceFileName("%sService")
.formatServiceImplFileName("%sServiceImp")
.build();
//entity的策略配置
builder.entityBuilder()
.enableLombok()
.enableTableFieldAnnotation()
.versionColumnName("version")
.logicDeleteColumnName("is_delete")
.columnNaming(NamingStrategy.underline_to_camel)
.idType(IdType.ASSIGN_ID)
.formatFileName("%sEntity")
.build();
})
.execute();
}
}
new GlobalConfig.Builder()
.fileOverride()
.outputDir("")//输出目录
.author("wmj")
.enableKotlin()
.enableSwagger()
.dateType(DateType.TIME_PACK)
.commentDate("yyyy-MM-dd")
.build();
new PackageConfig.Builder()
.moduleName("sys")
.entity("po")
.service("service")
.serviceImpl("service.impl")
.mapper("mapper")
.xml("mapper.xml")
.controller("controller")
.other("other")
.pathInfo(Collections.singletonMap(OutputFile.mapperXml, "D://"))
.build();
new TemplateConfig.Builder()
.disable(TemplateType.ENTITY)
.entity("/templates/entity.java")
.service("/templates/service.java")
.serviceImpl("/templates/serviceImpl.java")
.mapper("/templates/mapper.java")
.mapperXml("/templates/mapper.xml")
.controller("/templates/controller.java")
.build();
new StrategyConfig.Builder()
.enableCapitalMode()
.enableSkipView()
.disableSqlFilter()
.likeTable(new LikeTable("USER"))
.addInclude("t_simple")
.addTablePrefix("t_", "c_")
.addFieldSuffix("_flag")
.build();
new StrategyConfig.Builder()
.entityBuilder()
.superClass(BaseEntity.class)
.disableSerialVersionUID()
.enableChainModel()
.enableLombok()
.enableRemoveIsPrefix()
.enableTableFieldAnnotation()
.enableActiveRecord()
.versionColumnName("version")
//.versionPropertyName("version")
.logicDeleteColumnName("deleted")
//.logicDeletePropertyName("deleteFlag")
.naming(NamingStrategy.no_change)
.columnNaming(NamingStrategy.underline_to_camel)
.addSuperEntityColumns("id", "created_by", "created_time", "updated_by", "updated_time")
.addIgnoreColumns("age")
.addTableFills(new Column("create_time", FieldFill.INSERT))
.addTableFills(new Property("updateTime", FieldFill.INSERT_UPDATE))
.idType(IdType.AUTO)
.formatFileName("%sEntity")
.build();
new StrategyConfig.Builder()
.controllerBuilder()
.superClass(BaseController.class)
.enableHyphenStyle()
.enableRestStyle()
.formatFileName("%sAction")
.build();
new StrategyConfig.Builder()
.serviceBuilder()
.superServiceClass(BaseService.class)
.superServiceImplClass(BaseServiceImpl.class)
.formatServiceFileName("%sService")
.formatServiceImplFileName("%sServiceImp")
.build();