springboot+mybatisplus 代码生成器
转载
maven 依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>springBoot1</artifactId>
<version>1.0-SNAPSHOT</version>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<artifactId>mysql-connector-java</artifactId>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.78</version>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
public class CreateAoutGenerator {
public static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
StringBuilder help = new StringBuilder();
help.append("请输入" + tip + ":");
System.out.println(help.toString());
String ipt = scanner.next();
if (StringUtils.isNotEmpty(ipt)) {
throw new MybatisPlusException("请输入正确的" + tip + "!");
private String getProjectPath(){
return System.getProperty("user.dir");
public InjectionConfig CreateInjectionConfig(){
PackageConfig pc=CreatePackageConfig();
InjectionConfig cfg = new InjectionConfig() {
String templatePath = "/templates/mapper.xml.ftl";
// String templatePath = "/templates/mapper.xml.vm";
List<FileOutConfig> focList = new ArrayList<>();
focList.add(new FileOutConfig(templatePath) {
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return getProjectPath() + "/src/main/resources/mapper/" + pc.getModuleName()
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
public GlobalConfig CreateGlobConfig(){ //全局配置
GlobalConfig gc = new GlobalConfig();
gc.setOutputDir(getProjectPath()+"/src/main/java");
gc.setFileOverride(false);// 是否覆盖同名文件,默认是false
gc.setActiveRecord(true);// 不需要ActiveRecord特性的请改为false
gc.setEnableCache(false);// XML 二级缓存
gc.setBaseResultMap(true);// XML ResultMap
gc.setBaseColumnList(false);// XML columList
public DataSourceConfig CreateDataSourceConfig(){//数据源
DataSourceConfig dsc = new DataSourceConfig();
dsc.setDbType(DbType.MYSQL);
dsc.setTypeConvert(new MySqlTypeConvert() {
public DbColumnType processTypeConvert(String fieldType) {
System.out.println("转换类型:" + fieldType);
// 注意!!processTypeConvert 存在默认类型转换,如果不是你要的效果请自定义返回、非如下直接返回。
return super.processTypeConvert(fieldType);
dsc.setDriverName("com.mysql.jdbc.Driver");
dsc.setPassword("root3306");
dsc.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8");
public PackageConfig CreatePackageConfig(){//配置包名
PackageConfig pc = new PackageConfig();
pc.setParent("com.mht.springbootmybatisplus");
pc.setModuleName(scanner("模块名"));
pc.setController("controller");
pc.setService("service");
pc.setServiceImpl("serviceimpl");
public StrategyConfig CreateStrategyConfig(){//策略配置
PackageConfig pc=CreatePackageConfig();
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel)
本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。