mohのAI正在绞尽脑汁想思路ING···
mohのAI摘要
mohのAI-Lite
碎碎念

最近在开发过程中,由于基本都是采用连表查询,每次都要写代码,所以想写一个代码生成工具,根据表结构生成代码,方便开发。

基础代码生成后端模板使用说明

快速使用流程

  1. 填「一、表信息」 — 主表/关联表的建表语句
  2. 填「二、文件路径」 — 7 个文件的实际路径
  3. 填「三、业务配置」 — 业务名称、URL 前缀、权限前缀
  4. 填「四、命名规范」 — 提供 {xxx} 业务标识
  5. 填「五、查询字段」 — 需要查询的字段和动态条件
  6. AI 根据「七」的模板 — 自动替换占位符,生成 7 个文件的完整代码

注意:上面是使用流程 问ai时候 记得把这个删除了

一、表信息

主表

表名{主表名称}(别名 t1)

1
{主表建表语句}

关联表

表名{关联表名称}(别名 t2)

1
{关联表建表语句}

关联条件t1.{主表外键字段} = t2.ID AND t2.DELETE_FLAG = 'NOT_DELETE'


二、文件路径

  • DTO 路径:{DTO完整类路径}
  • PageParam 路径:{PageParam完整类路径}
  • Mapper XML:{Mapper XML文件系统路径}
  • Mapper 接口:{Mapper接口完整类路径}
  • Service 接口:{Service接口完整类路径}
  • Service 实现:{Service实现类完整类路径}
  • Controller:{Controller完整类路径}

三、业务配置

  • 业务名称(英文):{如 Award、TeacherWork 等}
  • 业务描述(中文):{如 获奖设置、教师作品 等}
  • 接口URL前缀:{如 /trc/projectattendrecord}
  • 权限标识前缀:{如 trc:projectattendrecord}

四、命名规范

{xxx} 作为业务标识,生成的文件和方法命名如下:

文件命名

类型 命名规则 示例
DTO {xxx}Dto AwardDto
PageParam {xxx}PageParam AwardPageParam
Mapper XML {xxx}Mapper.xml AwardMapper.xml
Mapper 接口 {xxx}Mapper.java AwardMapper.java
Service 接口 {xxx}Service.java AwardService.java
Service 实现 {xxx}ServiceImpl.java AwardServiceImpl.java
Controller {xxx}Controller.java AwardController.java

方法命名

类型 命名规则 示例
分页查询 page{xxx} pageAward
列表查询 list{xxx} listAward

resultMap 命名

类型 命名规则 示例
resultMap {xxx}ResultMap AwardResultMap

权限/URL 命名

类型 命名规则 示例
权限标识 {权限前缀}/page{xxx} trc:projectattendrecord/pageAward
接口URL {URL前缀}/page{xxx} /trc/projectattendrecord/pageAward

五、查询字段

需要查询的字段

  • 主表:ID, CREATE_TIME, {其他字段}
  • 从表:{需要的字段}

动态查询条件

  • 模糊搜索:{字段名}
  • 精确匹配:{字段名}
  • 时间范围:{字段名}

六、代码规范

  • 数据库列名:大写下划线(PROJECT_ID
  • Java 属性名:驼峰(projectId
  • 软删除:DELETE_FLAG = 'NOT_DELETE'
  • 参数访问:#{param.xxx}
  • 动态判断:test="param.xxx != null and param.xxx != ''"
  • 分页参数:CommonPageRequest.defaultPage()
  • 默认排序:ORDER BY t1.CREATE_TIME DESC
  • 返回类型:Page<Map<String, Object>> / List<Map<String, Object>>
  • DTO 模式:扁平模式(不继承,字段平铺)

七、代码模板

PageParam

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@Getter
@Setter
public class {xxx}PageParam {

/** 当前页 */
@Schema(description = "当前页码")
private Integer current;

/** 每页条数 */
@Schema(description = "每页条数")
private Integer size;

/** 排序字段 */
@Schema(description = "排序字段,字段驼峰名称,如:userName")
private String sortField;

/** 排序方式 */
@Schema(description = "排序方式,升序:ASCEND;降序:DESCEND")
private String sortOrder;

/** 关键词 */
@Schema(description = "关键词")
private String searchKey;
}

DTO(扁平模式)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package {DTO包路径};

import lombok.Data;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
* {业务描述}DTO(扁平模式)
*/
@Data
public class {Dto名称} {
// ========== 主表字段 ==========
/** 主键ID */
private String id;
/** 创建时间 */
private LocalDateTime createTime;
// ... 其他主表字段

// ========== 从表字段(平铺) ==========
// ... 从表字段
}

Mapper 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
<!-- resultMap 定义 -->
<resultMap id="{ResultMapId}" type="{DTO完整类路径}">
<!-- 主表字段 -->
<id column="ID" property="id"/>
<result column="CREATE_TIME" property="createTime"/>
<!-- 从表字段 -->
<result column="{从表字段大写}" property="{从表字段驼峰}"/>
</resultMap>

<!-- 分页查询 -->
<select id="page{业务名称}" resultMap="{ResultMapId}">
SELECT t1.ID, t1.CREATE_TIME, {其他字段}
FROM {主表名} t1
LEFT JOIN {关联表名} t2 ON t1.{外键} = t2.ID AND t2.DELETE_FLAG = 'NOT_DELETE'
WHERE t1.DELETE_FLAG = 'NOT_DELETE'
<if test="param.xxx != null and param.xxx != ''">
AND t1.XXX LIKE CONCAT('%', #{param.xxx}, '%')
</if>
ORDER BY t1.CREATE_TIME DESC
</select>

<!-- 列表查询 -->
<select id="list{业务名称}" resultMap="{ResultMapId}">
SELECT t1.ID, t1.CREATE_TIME, {其他字段}
FROM {主表名} t1
LEFT JOIN {关联表名} t2 ON t1.{外键} = t2.ID AND t2.DELETE_FLAG = 'NOT_DELETE'
WHERE t1.DELETE_FLAG = 'NOT_DELETE'
<if test="param.xxx != null and param.xxx != ''">
AND t1.XXX LIKE CONCAT('%', #{param.xxx}, '%')
</if>
ORDER BY t1.CREATE_TIME DESC
</select>

Mapper 接口

1
2
3
4
5
6
7
8
9
10
11
12
/**
* 分页查询{业务描述}
*/
Page<{Dto名称}> page{业务名称}(
@Param("page") Page<{EntityName}> page,
@Param("param") {PageParam名称} param);

/**
* 列表查询{业务描述}
*/
List<{Dto名称}> list{业务名称}(
@Param("param") {PageParam名称} param);

Service 接口

1
2
3
4
5
6
7
8
9
/**
* 分页查询{业务描述}
*/
Page<Map<String, Object>> page{业务名称}({PageParam名称} param);

/**
* 列表查询{业务描述}
*/
List<Map<String, Object>> list{业务名称}({PageParam名称} param);

ServiceImpl 实现

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
@Override
public Page<Map<String, Object>> page{业务名称}({PageParam名称} param) {
Page<{EntityName}> pageParam = CommonPageRequest.defaultPage();
Page<{Dto名称}> dtoPage = {mapper名}.page{业务名称}(pageParam, param);

List<Map<String, Object>> result = new ArrayList<>();
for ({Dto名称} dto : dtoPage.getRecords()) {
Map<String, Object> map = BeanUtil.beanToMap(dto);
result.add(map);
}

Page<Map<String, Object>> pageResult = new Page<>(
dtoPage.getCurrent(), dtoPage.getSize(), dtoPage.getTotal()
);
pageResult.setRecords(result);
return pageResult;
}

@Override
public List<Map<String, Object>> list{业务名称}({PageParam名称} param) {
List<{Dto名称}> dtoList = {mapper名}.list{业务名称}(param);
List<Map<String, Object>> result = new ArrayList<>();
for ({Dto名称} dto : dtoList) {
Map<String, Object> map = BeanUtil.beanToMap(dto);
result.add(map);
}
return result;
}

Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Operation(summary = "{业务描述}")
@SaCheckPermission("{权限前缀}/page{业务名称}")
@GetMapping("{URL前缀}/page{业务名称}")
public CommonResult<Page<Map<String, Object>>> page{业务名称}(
{PageParam名称} param) {
return CommonResult.data({service变量名}.page{业务名称}(param));
}

@Operation(summary = "{业务描述}")
@SaCheckPermission("{权限前缀}/list{业务名称}")
@GetMapping("{URL前缀}/list{业务名称}")
public CommonResult<List<Map<String, Object>>> list{业务名称}(
{PageParam名称} param) {
return CommonResult.data({service变量名}.list{业务名称}(param));
}

八、动态条件速查

  • 模糊搜索:

    1
    2
    3
    <if test="param.xxx != null and param.xxx != ''">
    AND t1.XXX LIKE CONCAT('%', #{param.xxx}, '%')
    </if>
  • 精确匹配:

    1
    2
    3
    <if test="param.xxx != null and param.xxx != ''">
    AND t1.XXX = #{param.xxx}
    </if>
  • 时间范围:

    1
    2
    3
    <if test="param.startTime != null and param.endTime != null">
    AND t1.CREATE_TIME BETWEEN #{param.startTime} AND #{param.endTime}
    </if>
  • IN 查询:

    1
    2
    3
    4
    5
    6
    <if test="param.xxxList != null and param.xxxList.size() > 0">
    AND t1.XXX IN
    <foreach collection="param.xxxList" item="item" open="(" separator="," close=")">
    #{item}
    </foreach>
    </if>