功能说明
实现分页查询用户打卡记录的接口,根据给定的查询条件返回用户打卡记录信息。
开发思路
- 定义查询表单:使用Java类表示前端传递的查询参数,并对其进行校验。
- 接收和处理请求:在控制器中接收前端的请求数据,进行参数校验后将数据传递给服务层。
- 服务层处理:服务层负责调用数据访问层,执行查询逻辑并返回结果。
- 数据访问层查询:编写SQL查询语句,根据传递的查询条件进行数据查询。
- 分页处理:实现分页逻辑,返回符合条件的数据及分页信息。
开发过程
1. 定义查询表单
@Data
public class searchCheckInByPageForm {
@NotNull(message = "page不能为空")
@Min(value = 1, message = "page最小值为1")
private Integer page;
@NotNull(message = "length不能为空")
@Range(min = 10, max = 50, message = "length每页长度不正确,最小10,最大50")
private Integer length;
private Integer userId;
private String nickName;
private String name;
private String email;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private DateTime startTime;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private DateTime endTime;
@NotNull(message = "排序不能为空")
private String order;
}
page:当前页码,不能为空且最小值为1。length:每页记录数,不能为空且范围为10到50。userId:用户ID,可选查询条件。nickName:用户昵称,可选查询条件。name:用户姓名,可选查询条件。email:用户邮箱,可选查询条件。startTime:查询起始时间,可选查询条件。endTime:查询结束时间,可选查询条件。order:排序方式,不能为空。
2. 接收和处理请求
@PostMapping("/searchCheckInByPage")
@SaCheckLogin
@SaCheckPermission(value = {"ROOT", "CHECK_IN:SELECT"}, mode = SaMode.OR)
public R searchCheckInByPage(@RequestBody @Valid searchCheckInByPageForm form) throws ParseException {
Map<String, Object> param = BeanUtil.beanToMap(form);
int page = form.getPage();
int length = form.getLength();
int start = (page - 1) * length;
param.put("start", start);
PageUtils pageUtils = userService.searchCheckInByPage(param);
return R.ok().put("result", pageUtils);
}
@PostMapping("/searchCheckInByPage"):定义HTTP POST请求的路由。@SaCheckLogin:表示该方法需要登录才能访问。@SaCheckPermission(value = {"ROOT", "CHECK_IN:SELECT"}, mode = SaMode.OR):表示该方法需要具有ROOT权限或CHECK_IN:SELECT权限才能访问。@RequestBody @Valid searchCheckInByPageForm form:接收并验证请求体中的数据。- 将
form转换为Map,并计算分页的起始位置start。 - 调用
userService.searchCheckInByPage方法,传递参数param。 - 返回查询结果。
3. 服务层处理
@Override
public PageUtils searchCheckInByPage(Map<String, Object> param) {
ArrayList<HashMap> list = null;
long count = wxUserDao.countUserCheckIn(param);
if (count > 0) {
list = wxUserDao.searchCheckInByPage(param);
} else {
list = new ArrayList<>();
}
int page = MapUtil.getInt(param, "page");
int length = MapUtil.getInt(param, "length");
PageUtils pageUtils = new PageUtils(list, count, page, length);
return pageUtils;
}
- 调用
wxUserDao.countUserCheckIn(param)获取符合条件的记录总数。 - 如果总数大于0,调用
wxUserDao.searchCheckInByPage(param)获取当前页的数据。 - 使用
PageUtils封装查询结果和分页信息,并返回。
4. 数据访问层查询
<select id="searchCheckInByPage" parameterType="Map" resultType="HashMap">
SELECT u.nick_name AS nickName,
ui.name,
ci.user_id,
ci.time,
ci.text,
ci.images,
ci.analysis
FROM check_in ci
JOIN user u ON ci.user_id = u.id
JOIN user_info ui ON u.id = ui.user_id
WHERE 1 = 1
<if test="userId != null and userId != ''">
AND u.id = #{userId}
</if>
<if test="nickName != null and nickName != ''">
AND u.nick_name LIKE CONCAT('%', #{nickName}, '%')
</if>
<if test="name != null and name != ''">
AND ui.name LIKE CONCAT('%', #{name}, '%')
</if>
<if test="email != null and email != ''">
AND ui.email = #{email}
</if>
<if test="startTime != null">
AND ci.time >= #{startTime}
</if>
<if test="endTime != null">
AND ci.time <= #{endTime}
</if>
<if test="order == 'desc'">
ORDER BY ci.time DESC
</if>
<if test="order == 'asc'">
ORDER BY ci.time ASC
</if>
LIMIT #{length} OFFSET #{start}
</select>
- 查询用户打卡记录,并根据传递的查询条件进行过滤。
- 使用
LIMIT和OFFSET实现分页功能。 - 根据
order参数确定排序方式。
5. 分页处理
分页逻辑通过计算 start 参数并在SQL查询中使用 LIMIT 和 OFFSET 来实现,返回符合条件的数据及分页信息。
总结
通过以上步骤,实现了分页查询用户打卡记录的功能。前端通过POST请求将查询条件发送到后端,后端接收并校验参数后,将查询条件传递给服务层。服务层根据查询条件调用数据访问层执行数据库查询,并返回查询结果及分页信息。

被折叠的 条评论
为什么被折叠?



