package com.zuijin.vue2blog.generator;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.po.LikeTable;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import com.baomidou.mybatisplus.generator.function.ConverterFileName;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Collections;
public class Generator {
private static final String URL = "jdbc:mysql://localhost:3306/vue2blog?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai";
private static final String username = "root";
private static final String password = "admin";
private static final String projectPath = System.getProperty("user.dir");
public static void main(String[] args) {
System.out.println(projectPath);
FastAutoGenerator.create(URL, username, password)
.globalConfig(builder -> {
builder.author("醉瑾")
.fileOverride()
.outputDir(projectPath + "/src/main/java")
.enableSwagger()
.disableOpenDir()
.dateType(DateType.ONLY_DATE)
.commentDate("yyyy-MM-dd")
.build();
})
.packageConfig(builder -> {
builder.parent("com.zuijin.vue2blog")
.moduleName("")
.entity("entity")
.service("service")
.serviceImpl("service.impl")
.mapper("mapper")
.xml("mapper.xml")
.controller("controller")
.pathInfo(Collections.singletonMap(OutputFile.xml, projectPath + "/src/main/resources/mapper"))
.build();
})
.strategyConfig(builder -> {
builder.addTablePrefix("%")
.entityBuilder()
.enableTableFieldAnnotation()
.idType(IdType.AUTO)
.controllerBuilder()
.enableHyphenStyle()
.enableRestStyle()
.serviceBuilder()
.formatServiceFileName("%sService")
.formatServiceImplFileName("%sServiceImpl")
.mapperBuilder()
.superClass(BaseMapper.class)
.enableMapperAnnotation()
.enableBaseResultMap()
.enableBaseColumnList()
.formatMapperFileName("%sMapper")
.formatXmlFileName("%sMapperXml")
.build();
})
.templateEngine(new VelocityTemplateEngine())
.templateConfig(builder -> {
builder.controller("/templates/controller.java")
.build();
})
.execute();
}
}
个性化自定义模板在下包中 复制文件后自定义修改即可 例如:
package ${package.Controller};
import $!{package.Service}.$!{table.serviceName};
import org.springframework.web.bind.annotation.RequestMapping;
#if(${swagger})
import io.swagger.annotations.Api;
#end
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
#if(${superControllerClassPackage})
import ${superControllerClassPackage};
#end
#if(${swagger})
@Api(tags = "${table.comment}")
#end
#if(${restControllerStyle})
@RestController
#else
@Controller
#end
@RequestMapping("#if(${package.ModuleName})/${package.ModuleName}#end/#if(${controllerMappingHyphenStyle})${controllerMappingHyphen.replace("-","/")}#else${table.entityPath}#end")
#if(${kotlin})
class ${table.controllerName}#if(${superControllerClass}) : ${superControllerClass}()#end
#else
#if(${superControllerClass})
public class ${table.controllerName} extends ${superControllerClass} {
#else
public class ${table.controllerName} {
#end
@Resource
private ${table.serviceName} ${table.entityPath}Service;
}
#end
|