升级了下mybatis-plus版本,随之把自动生成代码插件也升级了,改动随之贴个代码。
pom信息:
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>${mybatis-plus-boot-starter.version}version>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-extensionartifactId>
<version>${mybatis-plus-boot-starter.version}version>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-generatorartifactId>
<version>${mybatis-plus-generator.version}version>
dependency>
<dependency>
<groupId>org.apache.velocitygroupId>
<artifactId>velocity-engine-coreartifactId>
<version>${velocity-engine-core.version}version>
dependency>
版本信息:
<mybatis-plus-boot-starter.version>3.4.3.4mybatis-plus-boot-starter.version>
<mybatis-plus-generator.version>3.5.1mybatis-plus-generator.version>
<velocity-engine-core.version>2.3velocity-engine-core.version>
1. 使用的是velocity做模板引擎,为了兼容之前的,自定义变量、自定义模板
2. 基本使用的是自定义模板,模板大家需求都不太一样,所以这里不贴太多,只发一两个做参考
3. 重写了templateEngine的outputCustomFile方法,主要是因为希望自己定义自定义模板的生成路径
4. 数据源使用MySQL 8.0
5. 关于模板里的参数,如果你需要自定义变量 temp,就塞进objectMap中,然后模板中使用${temp}
6. 源码已上传gitee : https://gitee.com/mo_mo_yang/genertor-code
package com.momomian.admin.generator.code;
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.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import com.baomidou.mybatisplus.generator.keywords.MySqlKeyWordsHandler;
import java.io.File;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class CurdGenerator {
private static String author = "momomian";
private static String url = "jdbc:mysql://localhost:3306/mmm-life?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
private static String username = "root";
private static String password = "123456";
//输出目录
private static String outputDir = System.getProperty("user.dir") + "/admin/src/main/java";
//父包
private static String parentPackage = "com.momomian.admin";
//模块名
private static String moduleName = "sys";
//表名
private static String tableName = "t_user";
private static String tablePrefix = "t_";
// 表有无创建时间日期字段
private static boolean existDate = false;
private static String createDateName = "createDate";
private static String updateDateName = "updateDate";
// 全局响应类名
private static String allResultApiName = "ApiResult";
/**
* 数据源配置
*/
private static final DataSourceConfig.Builder DATA_SOURCE_CONFIG = new DataSourceConfig
.Builder(url, username, password)
//数据库查询
.dbQuery(new MySqlQuery())
//数据库类型转换器
.typeConvert(new MySqlTypeConvert())
//数据库关键字处理器
.keyWordsHandler(new MySqlKeyWordsHandler());
/**
* 快速生成器
*/
private static FastAutoGenerator fastAutoGenerator = FastAutoGenerator.create(DATA_SOURCE_CONFIG);
public static void main(String[] args) {
genCode();
}
/**
* 全局配置
*/
public static void globalConfig() {
fastAutoGenerator.globalConfig(builder -> {
// 设置作者
builder.author(author)
// 开启 swagger 模式
//.enableSwagger()
// 覆盖已生成文件
.fileOverride()
//禁止生成代码后自动弹出输出目录
.disableOpenDir()
// 时间策略
.dateType(DateType.TIME_PACK)
//注释日期,默认值: yyyy-MM-dd
.commentDate("yyyy-MM-dd")
// 指定输出目录
.outputDir(outputDir);
});
}
/**
* 包配置
*/
public static void packageConfig() {
fastAutoGenerator.packageConfig(builder -> {
// 设置父包名
builder.parent(parentPackage)
// 设置父包模块名
.moduleName(moduleName)
.entity("po")
.service("service")
.serviceImpl("service.impl")
.mapper("mapper")
.xml("mapper.xml")
.controller("controller")
//设置自定义的文件包名,默认是other,这边取消掉
.other("")
// 设置mapperXml生成路径
.pathInfo(Collections.singletonMap(OutputFile.mapperXml, "admin/src/main/resources/mapper"));
});
}
/**
* 基本模板配置
*/
public static void templateConfig() {
fastAutoGenerator.templateConfig((scanner, builder) -> builder
.entity("/template/entity.java")
.service("/template/service.java")
.serviceImpl("/template/serviceImpl.java")
.mapper("/template/mapper.java")
.mapperXml("/template/mapper.xml")
.controller("/template/controller.java")
);
}
/**
* 自定义模板配置
*/
public static void injectionConfig() {
Map<String, String> fileMap = new HashMap<>();
Map<String, Object> fieldMap = new HashMap<>();
fastAutoGenerator.injectionConfig(builder -> builder
//输出文件之前消费者
.beforeOutputFile((tableInfo, objectMap) -> {
String entityName = tableInfo.getEntityName();
Map<String, Object> aPackageMap = (Map) objectMap.get("package");
//自定义字段,模板变量 直接粗暴加objectMap,因为加入fieldMap会不生效(源码里写的是先把customMap.putAll到objectMap,再执行的这里--哭了)
objectMap.put("table_name", entityName.substring(0, 1).toLowerCase() + entityName.substring(1));
objectMap.put("model", aPackageMap.get("Parent") + ".model");
objectMap.put("bean", entityName.replace("PO", ""));
objectMap.put("vo", entityName + "VO");
objectMap.put("convert", entityName + "Convert");
objectMap.put("dto", entityName + "DTO");
objectMap.put("query", entityName + "Query");
objectMap.put("common", "com.momomian.common");
diyConfig(objectMap);
//自定义生成文件配置
fileMap.put("\\model\\vo\\" + entityName + "VO.java", "/template/vo.java.vm");
fileMap.put("\\model\\convert\\" + entityName + "Convert.java", "/template/convert.java.vm");
fileMap.put("\\model\\dto\\" + entityName + "DTO.java", "/template/dto.java.vm");
fileMap.put("\\model\\query\\" + entityName + "Query.java", "/template/query.java.vm");
})
// 自定义属性,模板变量
.customMap(fieldMap)
.customFile(fileMap)
);
}
/**
* 自定义模板变量配置
* 主要用于生成一些特殊需求
*
* @param objectMap
*/
private static void diyConfig(Map<String, Object> objectMap) {
//设定entityLombokModel为true,使用lombok
objectMap.put("entityLombokModel", true);
//表有无创建时间日期字段
objectMap.put("existDate", existDate);
//时间字段set方法定义
objectMap.put("setCreateDate", "set" + createDateName.substring(0, 1).toUpperCase() + createDateName.substring(1));
objectMap.put("setUpdateDate", "set" + updateDateName.substring(0, 1).toUpperCase() + updateDateName.substring(1));
objectMap.put("ApiResult", allResultApiName);
objectMap.put("baseResultMap", true);
objectMap.put("baseColumnList", true);
}
/**
* 策略配置
*
* @return
*/
public static void strategyConfig() {
fastAutoGenerator.strategyConfig(builder -> {
// 设置需要生成的表名
builder.addInclude(tableName)
// 设置过滤表前缀
.addTablePrefix(tablePrefix);
});
}
/**
* 配置模板引擎
*/
public static void templateEngine() {
fastAutoGenerator.templateEngine(new VelocityTemplateEngine() {
/**
* 重写输出自定义文件方法,自定义文件输出路径
*/
@Override
protected void outputCustomFile(Map<String, String> customFile, TableInfo tableInfo, Map<String, Object> objectMap) {
String otherPath = getPathInfo(OutputFile.other);
customFile.forEach((key, value) -> {
String fileName = String.format((otherPath + File.separator + "%s"), key);
outputFile(new File(fileName), objectMap, value);
});
}
});
}
public static void genCode() {
globalConfig();
packageConfig();
templateConfig();
strategyConfig();
injectionConfig();
templateEngine();
fastAutoGenerator.execute();
}
}
package $!{model}.vo;
#if(${swagger2})
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
#end
import lombok.Data;
import lombok.NoArgsConstructor;
#foreach($pkg in ${table.importPackages})
import $!{pkg};
#end
/**
* @author $!{author}
* @date $!{date}
* @description $!{table.comment}视图对象
*/
@Data
@NoArgsConstructor
#if(${swagger2})
@ApiModel(value="$!{vo}视图对象")
#end
public class $!{vo} implements Serializable {
private static final long serialVersionUID = 1L;
## ---------- BEGIN 字段循环遍历 ----------
#foreach($field in ${table.fields})
#if(${field.keyFlag})
#set($keyPropertyName=${field.propertyName})
#end
#if("$!field.comment" != "")
#if(${swagger2})
@ApiModelProperty(value = "$!{field.comment}")
#else
/**
* $!{field.comment}
*/
#end
#end
private $!{field.propertyType} $!{field.propertyName};
#end
## ---------- END 字段循环遍历 ----------
}
mapper.xml.vm
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${package.Mapper}.${table.mapperName}">
#if(${enableCache})
<cache type="org.mybatis.caches.ehcache.LoggingEhcache"/>
#end
#if(${baseResultMap})
<resultMap id="BaseResultMap" type="${package.Entity}.${entity}">
#foreach($field in ${table.fields})
#if(${field.keyFlag})##生成主键排在第一位
<id column="${field.name}" property="${field.propertyName}"/>
#end
#end
#foreach($field in ${table.commonFields})##生成公共字段
<result column="${field.name}" property="${field.propertyName}"/>
#end
#foreach($field in ${table.fields})
#if(!${field.keyFlag})##生成普通字段
<result column="${field.name}" property="${field.propertyName}"/>
#end
#end
resultMap>
#end
#if(${baseColumnList})
<sql id="Base_Column_List">
#foreach($field in ${table.commonFields})
${field.name},
#end
${table.fieldNames}
sql>
#end
<sql id="t_name">${table.name}sql>
#set($i=${table.name.lastIndexOf('_')})
#set($alias=${table.name.substring($i+1)})
<sql id="t_alias">${table.name} as ${alias}sql>
<select id="getWithPage" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List"/>
FROM ${table.name}
<where>
#foreach($field in ${table.fields})
<if test="query.${field.propertyName} != null ">
and ${field.name} = #{query.${field.propertyName}}
if>
#end
where>
select>
<select id="getCountByQuery" resultType="java.lang.Integer">
select count(1)
from ${table.name}
<where>
#foreach($field in ${table.fields})
<if test="query.${field.propertyName} != null ">
and ${field.name} = #{query.${field.propertyName}}
if>
#end
where>
select>
<insert id="insertReturnId" parameterType="$!{package.Entity}.$!{entity}" useGeneratedKeys="true" keyProperty="id">
INSERT INTO ${table.name}(
<trim suffixOverrides=",">
#foreach($field in ${table.fields})
<if test="po.${field.propertyName} != null">
${field.name} ,
if>
#end
trim>
)VALUES(
<trim suffixOverrides=",">
#foreach($field in ${table.fields})
<if test="po.${field.propertyName} != null">
#{po.${field.propertyName}},
if>
#end
trim>
)
insert>
<select id="getOneByQuery" resultType="$!{package.Entity}.$!{entity}">
select <include refid="Base_Column_List"/>
from ${table.name}
<where>
#foreach($field in ${table.fields})
<if test="query.${field.propertyName} != null ">
and ${field.name} = #{query.${field.propertyName}}
if>
#end
where>
select>
<select id="getAllByQuery" resultType="$!{package.Entity}.$!{entity}">
select <include refid="Base_Column_List"/>
from ${table.name}
<where>
#foreach($field in ${table.fields})
<if test="query.${field.propertyName} != null ">
and ${field.name} = #{query.${field.propertyName}}
if>
#end
where>
select>
mapper>