代码生成
pom依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.3</version>
</dependency>
低配
- 作者信息,输出目录
- 实体类:lombok风格
- mapper:接口和xml
- service:接口和实现类
- controller:rest风格
package com.qiangesoft.mybatisplus.util;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import java.nio.file.Paths;
import java.util.Collections;
public class SimpleCodeGenerator {
public static void main(String[] args) {
String path = Paths.get(System.getProperty("user.dir")).toString();
FastAutoGenerator
.create("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false",
"root", "root")
.globalConfig(builder -> builder
.author("qiangesoft")
.outputDir(path + "/src/main/java")
.disableOpenDir()
)
.packageConfig(builder -> builder
.parent("com.qiangesoft.demo")
.entity("entity")
.mapper("mapper")
.service("service")
.serviceImpl("service.impl")
.pathInfo(Collections.singletonMap(OutputFile.xml, path + "/src/main/resources/mapper"))
)
.strategyConfig(builder -> builder
.addInclude("sys_user", "sys_role")
.entityBuilder()
.enableLombok()
.controllerBuilder()
.enableRestStyle()
)
.templateEngine(new VelocityTemplateEngine())
.execute();
}
}
中配
- 作者信息,输出目录,接口文档,时间类型设置
- 实体类:lombok风格,命名规则,版本号和逻辑删除字段,字段填充
- mapper:接口添加@mapper注解,xml生成字段属性映射和结果字段
- service:接口和实现类
- controller:rest风格,连字符风格
package com.qiangesoft.mybatisplus.util;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import java.nio.file.Paths;
import java.util.Collections;
public class MiddleCodeGenerator {
public static void main(String[] args) {
String path = Paths.get(System.getProperty("user.dir")).toString();
FastAutoGenerator
.create("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false",
"root", "root")
.globalConfig(builder -> builder
.author("qiangesoft")
.enableSwagger()
.outputDir(path + "/src/main/java")
.commentDate("yyyy-MM-dd")
.dateType(DateType.TIME_PACK)
.disableOpenDir()
)
.packageConfig(builder -> builder
.parent("com.qiangesoft.demo")
.entity("entity")
.mapper("mapper")
.service("service")
.serviceImpl("service.impl")
.pathInfo(Collections.singletonMap(OutputFile.xml, path + "/src/main/resources/mapper"))
)
.strategyConfig(builder -> builder
.addInclude("sys_user", "sys_role")
.entityBuilder()
.enableLombok()
.naming(NamingStrategy.underline_to_camel)
.columnNaming(NamingStrategy.underline_to_camel)
.idType(IdType.AUTO)
.versionColumnName("version")
.logicDeleteColumnName("deleted")
.addTableFills(new Column("create_time", FieldFill.INSERT))
.enableFileOverride()
.mapperBuilder()
.enableMapperAnnotation()
.enableBaseResultMap()
.enableBaseColumnList()
.enableFileOverride()
.serviceBuilder()
.enableFileOverride()
.controllerBuilder()
.enableRestStyle()
.enableHyphenStyle()
.enableFileOverride()
)
.templateEngine(new VelocityTemplateEngine())
.execute();
}
}
高配
- 作者信息,输出目录,接口文档,时间类型设置,数据源配置
- 实体类:lombok风格,命名规则,版本号和逻辑删除字段,字段填充,基类配置,基类字段,忽略字段,文件命名规则
- mapper:接口添加@mapper注解及基类配置,xml生成字段属性映射和结果字段,文件命名规则
- service:接口和实现类,基类配置,文件命名规则
- controller:rest风格,连字符风格,基类配置,文件命名规则
package com.qiangesoft.mybatisplus.util;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.baomidou.mybatisplus.generator.fill.Property;
import org.apache.ibatis.type.JdbcType;
import java.nio.file.Paths;
import java.util.Collections;
public class CodeGenerator {
public static void main(String[] args) {
String path = Paths.get(System.getProperty("user.dir")).toString();
FastAutoGenerator
.create("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false",
"root", "root")
.dataSourceConfig(builder -> builder
.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
if (JdbcType.TINYINT == metaInfo.getJdbcType()) {
return DbColumnType.INTEGER;
}
return typeRegistry.getColumnType(metaInfo);
})
)
.globalConfig(builder -> builder
.author("qiangesoft")
.enableSwagger()
.outputDir(path + "/src/main/java")
.commentDate("yyyy-MM-dd")
.dateType(DateType.TIME_PACK)
.disableOpenDir()
)
.packageConfig(builder -> builder
.parent("com.qiangesoft.demo")
.entity("entity")
.mapper("mapper")
.service("service")
.serviceImpl("service.impl")
.pathInfo(Collections.singletonMap(OutputFile.xml, path + "/src/main/resources/mapper"))
)
.strategyConfig(builder -> builder
.addInclude("sys_user", "sys_role")
.entityBuilder()
.enableLombok()
.naming(NamingStrategy.underline_to_camel)
.columnNaming(NamingStrategy.underline_to_camel)
.disableSerialVersionUID()
.enableTableFieldAnnotation()
.idType(IdType.AUTO)
.superClass(BaseEntity.class)
.addSuperEntityColumns("id", "create_time")
.versionColumnName("version")
.logicDeleteColumnName("deleted")
.addIgnoreColumns("remark")
.addTableFills(new Column("create_time", FieldFill.INSERT))
.addTableFills(new Property("updateTime", FieldFill.INSERT_UPDATE))
.formatFileName("%s")
.enableFileOverride()
.mapperBuilder()
.enableMapperAnnotation()
.superClass(BaseMapper.class)
.disableMapperXml()
.enableBaseResultMap()
.enableBaseColumnList()
.formatMapperFileName("%sMapper")
.formatXmlFileName("%sMapper")
.enableFileOverride()
.serviceBuilder()
.superServiceClass(IService.class)
.superServiceImplClass(ServiceImpl.class)
.formatServiceFileName("I%sService")
.formatServiceImplFileName("%sServiceImpl")
.enableFileOverride()
.controllerBuilder()
.superClass(BaseController.class)
.enableRestStyle()
.enableHyphenStyle()
.formatFileName("%sController")
.enableFileOverride()
)
.injectionConfig(builder -> builder
.customMap(Collections.singletonMap("projectName", "演示代码生成"))
)
.templateEngine(new VelocityTemplateEngine())
.execute();
}
}