MybatisX 自定义 FreeMarker 模块

image-20230624174416631

.meta.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?xml version="1.0" encoding="utf-8" ?>
<templates>
<template>
<property name="configName" value="controller"/>
<property name="configFile" value="controller.ftl"/>
<property name="fileName" value="${domain.fileName}Controller"/>
<property name="suffix" value=".java"/>
<property name="packageName" value="${domain.basePackage}.controller"/>
<property name="encoding" value="${domain.encoding}"/>
<property name="basePath" value="${domain.basePath}"/>
</template>
<template>
<property name="configName" value="serviceInterface"/>
<property name="configFile" value="serviceInterface.ftl"/>
<property name="fileName" value="${domain.fileName}Service"/>
<property name="suffix" value=".java"/>
<property name="packageName" value="${domain.basePackage}.service"/>
<property name="encoding" value="${domain.encoding}"/>
<property name="basePath" value="${domain.basePath}"/>
</template>
<template>
<property name="configName" value="serviceImpl"/>
<property name="configFile" value="serviceImpl.ftl"/>
<property name="fileName" value="${domain.fileName}ServiceImpl"/>
<property name="suffix" value=".java"/>
<property name="packageName" value="${domain.basePackage}.service.impl"/>
<property name="encoding" value="${domain.encoding}"/>
<property name="basePath" value="${domain.basePath}"/>
</template>
<template>
<property name="configName" value="mapperInterface"/>
<property name="configFile" value="mapperInterface.ftl"/>
<property name="fileName" value="${domain.fileName}Mapper"/>
<property name="suffix" value=".java"/>
<property name="packageName" value="${domain.basePackage}.mapper"/>
<property name="encoding" value="${domain.encoding}"/>
<property name="basePath" value="${domain.basePath}"/>
</template>
<template>
<property name="configName" value="mapperXml"/>
<property name="configFile" value="mapperXml.ftl"/>
<property name="fileName" value="${domain.fileName}Mapper"/>
<property name="suffix" value=".xml"/>
<property name="packageName" value="mapper"/>
<property name="encoding" value="${domain.encoding}"/>
<property name="basePath" value="src/main/resources"/>
</template>
<template>
<!-- 注意: 实体类无论如何都会生成 -->
<!-- 1.5.x 开始 configName=domain的配置会被特殊处理, 其他配置跟随实体的配置-->
<!-- 主要用于生成 request,response 类型的对象 -->
<property name="configName" value="domain"/>
<property name="configFile" value="domain.ftl"/>
<property name="fileName" value="${domain.fileName}"/>
<property name="suffix" value=".java"/>
<property name="packageName" value="${domain.basePackage}.domain"/>
<property name="encoding" value="${domain.encoding}"/>
<property name="basePath" value="${domain.basePath}"/>
</template>
</templates>

domain.ftl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package ${domain.packageName};

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import lombok.experimental.SuperBuilder;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.Date;
<#list tableClass.importList as fieldType>${"\n"}import ${fieldType};</#list>

/**
* ${tableClass.shortClassName}
*
* @author YuanJW
* @date ${.now?string('yyyy-MM-dd')}
*/
@Data
@SuperBuilder
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName(value ="${tableClass.tableName}")
@ApiModel(value = "${tableClass.shortClassName}", description = "${tableClass.remark!}")
public class ${tableClass.shortClassName} implements Serializable {

<#list tableClass.pkFields as field>
<#if !field.nullable><#if field.jdbcType=="VARCHAR">@NotBlank(message = "[${field.remark!}]不能为空")<#else>@NotNull(message = "[${field.remark!}]不能为空")</#if></#if><#if field.jdbcType=="VARCHAR">${"\n "}@Size(max = ${field.columnLength?c}, message = "编码长度不能超过${field.columnLength?c}")</#if><#if field.jdbcType=="VARCHAR">@Length(max = ${field.columnLength?c}, message = "编码长度不能超过${field.columnLength?c}")</#if>
@TableId(value = "${field.columnName}"<#if field.autoIncrement>, type = IdType.AUTO</#if>)
@ApiModelProperty(value = "${field.remark!}")
private ${field.shortTypeName} ${field.fieldName};
</#list>

<#list tableClass.baseBlobFields as field>
<#if !field.nullable><#if field.jdbcType=="VARCHAR">@NotBlank(message = "[${field.remark!}]不能为空")<#else>@NotNull(message = "[${field.remark!}]不能为空")</#if></#if><#if field.jdbcType=="VARCHAR">${"\n "}@Size(max = ${field.columnLength?c}, message = "编码长度不能超过${field.columnLength?c}")</#if><#if field.jdbcType=="VARCHAR">${"\n "}@Length(max = ${field.columnLength?c}, message = "编码长度不能超过${field.columnLength?c}")</#if><#if field.jdbcType=="TIMESTAMP">${"\n "}@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")</#if>
@ApiModelProperty(value = "${field.remark!}")
private ${field.shortTypeName} ${field.fieldName};
</#list>

@TableField(exist = false)
private static final long serialVersionUID = 1L;
}

mapperInterface.ftl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package ${mapperInterface.packageName};

import ${tableClass.fullClassName};
<#if tableClass.pkFields??>
<#list tableClass.pkFields as field><#assign pkName>${field.shortTypeName}</#assign></#list>
</#if>
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
* ${mapperInterface.fileName}
*
* @author YuanJW
* @date ${.now?string('yyyy-MM-dd')}
*/
@Mapper
public interface ${mapperInterface.fileName} extends BaseMapper<${tableClass.shortClassName}> {

/**
* 新增${tableClass.remark!?substring(0, tableClass.remark?length-1)}
*
* @param ${tableClass.shortClassName?uncap_first}
* @return
*/
int save${tableClass.shortClassName}(${tableClass.shortClassName} ${tableClass.shortClassName?uncap_first});

/**
* 根据id查询${tableClass.remark!?substring(0, tableClass.remark?length-1)}
*
* @param id
* @return ${tableClass.shortClassName}
*/
${tableClass.shortClassName} get${tableClass.shortClassName}ById(Long id);

/**
* 根据搜索条件获取${tableClass.remark!?substring(0, tableClass.remark?length-1)}列表
*
* @param ${tableClass.shortClassName?uncap_first}
* @return
*/
List<${tableClass.shortClassName}> list${tableClass.shortClassName}s(${tableClass.shortClassName} ${tableClass.shortClassName?uncap_first});

/**
* 修改${tableClass.remark!?substring(0, tableClass.remark?length-1)}
*
* @param ${tableClass.shortClassName?uncap_first}
* @return
*/
int update${tableClass.shortClassName}(${tableClass.shortClassName} ${tableClass.shortClassName?uncap_first});

/**
* 批量删除${tableClass.remark!?substring(0, tableClass.remark?length-1)}
*
* @param ids
* @return
*/
int delete${tableClass.shortClassName}s(List<Long> ids);

/**
* 统计${tableClass.remark!?substring(0, tableClass.remark?length-1)}
*
* @param ${tableClass.shortClassName?uncap_first}
* @return
*/
int count${tableClass.shortClassName}(${tableClass.shortClassName} ${tableClass.shortClassName?uncap_first});
}

mapper.ftl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${mapperInterface.packageName}.${baseInfo.fileName}">

<resultMap id="BaseResultMap" type="${tableClass.fullClassName}">
<#list tableClass.pkFields as field>
<id property="${field.fieldName}" column="${field.columnName}" jdbcType="${field.jdbcType}"/>
</#list>
<#list tableClass.baseFields as field>
<result property="${field.fieldName}" column="${field.columnName}" jdbcType="${field.jdbcType}"/>
</#list>
</resultMap>

<sql id="Base_Column_List">
<#list tableClass.allFields as field>${field.columnName}<#sep>,${"\n "}</#list>
</sql>

<sql id="Example_Where_Clause">
<#list tableClass.allFields as field>
<if test="${field.fieldName} != null">and ${field.columnName} = ${'#'}{${field.fieldName},jdbcType=${field.jdbcType}}</if>
</#list>
</sql>

<insert id="save${tableClass.shortClassName}"<#if (tableClass.pkFields?size==1)> keyColumn="${tableClass.pkFields[0].columnName}" keyProperty="${tableClass.pkFields[0].fieldName}" parameterType="${tableClass.fullClassName}" useGeneratedKeys="true"</#if>>
<selectKey keyProperty="id" resultType="java.lang.Long" order="AFTER">
SELECT LAST_INSERT_ID()
</selectKey>
INSERT INTO ${tableClass.tableName}
<trim prefix="(" suffix=")" suffixOverrides=",">
<#list tableClass.allFields as field>
<if test="${field.fieldName} != null">${field.columnName},</if>
</#list>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<#list tableClass.allFields as field>
<if test="${field.fieldName} != null">${'#'}{${field.fieldName},jdbcType=${field.jdbcType}},</if>
</#list>
</trim>
</insert>

<select id="get${tableClass.shortClassName}ById" parameterType="java.lang.Long" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List" />
FROM ${tableClass.tableName}
WHERE <#list tableClass.pkFields as field>${field.columnName} = ${'#'}{${field.fieldName},jdbcType=${field.jdbcType}} <#if field_has_next>AND</#if></#list>
</select>

<select id="list${tableClass.shortClassName}s" parameterType="${tableClass.fullClassName}" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List" />
FROM ${tableClass.tableName}
<where>
<include refid="Example_Where_Clause"/>
</where>
</select>

<delete id="delete${tableClass.shortClassName}s" parameterType="java.util.List">
DELETE FROM ${tableClass.tableName}
<where>
<#list tableClass.pkFields as field> ${field.columnName} IN
<foreach collection="list" item="item" index="index" open="(" separator="," close=")">
${'#'}{item}
</foreach>
</#list>
</where>
</delete>

<update id="update${tableClass.shortClassName}" parameterType="${tableClass.fullClassName}">
UPDATE ${tableClass.tableName}
<set>
<#list tableClass.baseBlobFields as field>
<if test="${field.fieldName} != null">${field.columnName} = ${'#'}{${field.fieldName},jdbcType=${field.jdbcType}},</if>
</#list>
</set>
WHERE <#list tableClass.pkFields as field>${field.columnName} = ${'#'}{${field.fieldName},jdbcType=${field.jdbcType}} <#if field_has_next>AND</#if></#list>
</update>

<select id="count${tableClass.shortClassName}" parameterType="${tableClass.fullClassName}" resultType="java.lang.Integer">
SELECT COUNT(*) FROM ${tableClass.tableName}
<where>
<include refid="Example_Where_Clause"/>
</where>
</select>
</mapper>

serviceInterface.ftl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package ${baseInfo.packageName};

import ${tableClass.fullClassName};
<#if baseService??&&baseService!="">
import ${baseService};
<#list baseService?split(".") as simpleName>
<#if !simpleName_has_next>
<#assign serviceSimpleName>${simpleName}</#assign>
</#if>
</#list>
</#if>
import com.baomidou.mybatisplus.extension.service.IService;

import java.util.List;

/**
* ${baseInfo.fileName}
*
* @author YuanJW
* @date ${.now?string('yyyy-MM-dd')}
*/
public interface ${baseInfo.fileName} extends IService<${tableClass.shortClassName}> {

/**
* 新增${tableClass.remark!?substring(0, tableClass.remark?length-1)}
*
* @param ${tableClass.shortClassName?uncap_first}
* @return
*/
int insert${tableClass.shortClassName}(${tableClass.shortClassName} ${tableClass.shortClassName?uncap_first});

/**
* 获取${tableClass.remark!?substring(0, tableClass.remark?length-1)}
*
* @param ${tableClass.shortClassName?uncap_first}
* @return
*/
List<${tableClass.shortClassName}> list${tableClass.shortClassName}s(${tableClass.shortClassName} ${tableClass.shortClassName?uncap_first});

/**
* 根据id获取${tableClass.remark!?substring(0, tableClass.remark?length-1)}
*
* @param id
* @return
*/
${tableClass.shortClassName} get${tableClass.shortClassName}ById(Long id);

/**
* 修改${tableClass.remark!?substring(0, tableClass.remark?length-1)}
*
* @param ${tableClass.shortClassName?uncap_first}
* @return
*/
int update${tableClass.shortClassName}(${tableClass.shortClassName} ${tableClass.shortClassName?uncap_first});

/**
* 批量删除${tableClass.remark!?substring(0, tableClass.remark?length-1)}
*
* @param ids
* @return
*/
int delete${tableClass.shortClassName}ByIds(List<Long> ids);

/**
* 统计${tableClass.remark!?substring(0, tableClass.remark?length-1)}
*
* @param ${tableClass.shortClassName?uncap_first}
* @return
*/
int count${tableClass.shortClassName}(${tableClass.shortClassName} ${tableClass.shortClassName?uncap_first});
}

ServiceImpl.ftl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package ${baseInfo.packageName};

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ${tableClass.fullClassName};
import ${serviceInterface.packageName}.${serviceInterface.fileName};
import ${mapperInterface.packageName}.${mapperInterface.fileName};
<#if baseService??&&baseService!="">
import ${baseService};
<#list baseService?split(".") as simpleName>
<#if !simpleName_has_next>
<#assign serviceSimpleName>${simpleName}</#assign>
</#if>
</#list>
</#if>
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* ${baseInfo.fileName}
*
* @author YuanJW
* @date ${.now?string('yyyy-MM-dd')}
*/
@Slf4j
@Service
public class ${baseInfo.fileName} extends ServiceImpl<${mapperInterface.fileName}, ${tableClass.shortClassName}> implements ${serviceInterface.fileName} {

@Resource
private ${mapperInterface.fileName} ${mapperInterface.fileName?uncap_first};

@Override
public int insert${tableClass.shortClassName}(${tableClass.shortClassName} ${tableClass.shortClassName?uncap_first}) {
return ${mapperInterface.fileName?uncap_first}.insert(${tableClass.shortClassName?uncap_first});
}

@Override
public ${tableClass.shortClassName} get${tableClass.shortClassName}ById(Long id) {
return ${mapperInterface.fileName?uncap_first}.selectById(id);
}

@Override
public List<${tableClass.shortClassName}> list${tableClass.shortClassName}s(${tableClass.shortClassName} ${tableClass.shortClassName?uncap_first}) {
return ${mapperInterface.fileName?uncap_first}.list${tableClass.shortClassName}s(${tableClass.shortClassName?uncap_first});
}

@Override
public int update${tableClass.shortClassName}(${tableClass.shortClassName} ${tableClass.shortClassName?uncap_first}) {
return ${mapperInterface.fileName?uncap_first}.update${tableClass.shortClassName}(${tableClass.shortClassName?uncap_first});
}

@Override
public int delete${tableClass.shortClassName}ByIds(List<Long> ids) {
return ${mapperInterface.fileName?uncap_first}.deleteBatchIds(ids);
}

@Override
public int count${tableClass.shortClassName}(${tableClass.shortClassName} ${tableClass.shortClassName?uncap_first}) {
return ${mapperInterface.fileName?uncap_first}.count${tableClass.shortClassName}(${tableClass.shortClassName?uncap_first});
}
}

controller.ftl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package ${controller.packageName};

import com.example.demo.common.CommonPage;
import com.example.demo.common.CommonResult;
import ${tableClass.fullClassName};
import ${serviceInterface.packageName}.${serviceInterface.fileName};
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
* ${controller.fileName}
*
* @author YuanJW
* @date ${.now?string('yyyy-MM-dd')}
*/
@RestController
@RequestMapping("/${tableClass.shortClassName?uncap_first}")
@Api(tags = "${controller.fileName}", value = "${tableClass.remark!}Controller")
public class ${controller.fileName} {

@Resource
private ${serviceInterface.fileName} ${serviceInterface.fileName?uncap_first};

@ApiOperation("新增${tableClass.remark!?substring(0, tableClass.remark?length-1)}")
@PostMapping(value = "/insert", produces = "application/json;charset=UTF-8")
public CommonResult save(@RequestBody @Validated ${tableClass.shortClassName} ${tableClass.shortClassName?uncap_first}) {
int count = ${serviceInterface.fileName?uncap_first}.insert${tableClass.shortClassName}(${tableClass.shortClassName?uncap_first});
if (count > 0) {
return CommonResult.success(count);
}
return CommonResult.failed();
}

@ApiOperation("修改${tableClass.remark!?substring(0, tableClass.remark?length-1)}")
@PostMapping(value = "/update", produces = "application/json;charset=UTF-8")
public CommonResult update(@RequestBody @Validated ${tableClass.shortClassName} ${tableClass.shortClassName?uncap_first}) {
int count = ${serviceInterface.fileName?uncap_first}.update${tableClass.shortClassName}(${tableClass.shortClassName?uncap_first});
if (count > 0) {
return CommonResult.success(count);
}
return CommonResult.failed();
}

@ApiOperation("分页获取${tableClass.remark!?substring(0, tableClass.remark?length-1)}")
@ApiImplicitParams({
@ApiImplicitParam(name = "pageNum", value = "页码",
defaultValue = "1", paramType = "query", dataType = "integer"),
@ApiImplicitParam(name = "pageSize", value = "每页显示数量",
defaultValue = "5", allowableValues = "0,5,10,15,20", paramType = "query", dataType = "integer"),
@ApiImplicitParam(name = "orderBy", value = "排序字段", paramType = "query", dataType = "string")}
)
@GetMapping(value = "/list", produces = "application/json;charset=UTF-8")
public CommonResult<CommonPage<${tableClass.shortClassName}>> list(${tableClass.shortClassName} ${tableClass.shortClassName?uncap_first},
@RequestParam(required = false, defaultValue = "1") Integer pageNum,
@RequestParam(required = false, defaultValue = "5") Integer pageSize,
@RequestParam(required = false) String orderBy) {
PageHelper.startPage(pageNum, pageSize, orderBy + " desc");
List<${tableClass.shortClassName}> list = ${serviceInterface.fileName?uncap_first}.list${tableClass.shortClassName}s(${tableClass.shortClassName?uncap_first});
return CommonResult.success(CommonPage.restPage(list));
}

@ApiOperation("根据id查询${tableClass.remark!?substring(0, tableClass.remark?length-1)}")
@GetMapping(value = "/{id}", produces = "application/json;charset=UTF-8")
public CommonResult<${tableClass.shortClassName}> detail(@PathVariable Long id) {
${tableClass.shortClassName} ${tableClass.shortClassName} = ${serviceInterface.fileName?uncap_first}.get${tableClass.shortClassName}ById(id);
return CommonResult.success(${tableClass.shortClassName});
}

@ApiOperation("批量删除${tableClass.remark!?substring(0, tableClass.remark?length-1)}")
@PostMapping(value = "/remove", produces = "application/json;charset=UTF-8")
public CommonResult remove(@RequestParam("ids") List<Long> ids) {
int count = ${serviceInterface.fileName?uncap_first}.delete${tableClass.shortClassName}ByIds(ids);
if (count > 0) {
return CommonResult.success(count);
}
return CommonResult.failed();
}
}