按照以往开发一个新模块的套路,首先应该把分页数据展示出来,然后再实现该模块的添加、修改和删除功能。本小节咱们把查询罚款分页数据的后端代码给实现了,下节课就可以写前端代码了。
一、编写持久层
在TbAmectDao.xml
文件中,我们定义SQL语句。
<select id="searchAmectByPage" parameterType="HashMap" resultType="HashMap">
SELECT a.id,
a.uuid,
u.name,
d.dept_name AS deptName,
a.amount,
a.reason,
`at`.type,
a.`status`,
DATE_FORMAT(a.create_time,'%Y-%m-%d %H:%i') AS createTime,
IF(a.user_id=#{currentUserId},"true","false") AS mine
FROM tb_amect a
JOIN tb_amect_type `at` ON a.type_id = `at`.id
JOIN tb_user u ON u.id = a.user_id
JOIN tb_dept d ON u.dept_id = d.id
WHERE 1=1
<if test="userId != null">
AND u.id = #{userId}
</if>
<if test="name != null">
AND u.name LIKE '%${name}%'
</if>
<if test="deptId != null">
AND u.dept_id = #{deptId}
</if>
<if test="typeId != null">
AND at.id = #{typeId}
</if>
<if test="startDate != null and endDate != null">
AND a.create_time BETWEEN #{startDate} AND #{endDate}
</if>
<if test="status!=null">
AND a.`status` = #{status}
</if>
ORDER BY a.id DESC
LIMIT #{start}, #{length}
</select>
<select id="searchAmectCount" parameterType="HashMap" resultType="long">
SELECT COUNT(*)
FROM tb_amect a
JOIN tb_amect_type `at` ON a.type_id = `at`.id
JOIN tb_user u ON u.id = a.user_id
JOIN tb_dept d ON u.dept_id = d.id
WHERE 1=1
<if test="userId != null">
AND u.id = #{userId}
</if>
<if test="name != null">
AND u.name LIKE '%${name}%'
</if>
<if test="deptId != null">
AND u.dept_id = #{deptId}
</if>
<if test="typeId != null">
AND at.id = #{typeId}
</if>
<if test="startDate != null and endDate != null">
AND a.create_time BETWEEN #{startDate} AND #{endDate}
</if>
<if test="status!=null">
AND a.`status` = #{status}
</if>
</select>
在TbAmectDao.java
接口中,定义DAO方法。
public interface TbAmectDao {
public ArrayList<HashMap> searchAmectByPage(HashMap param);
public long searchAmectCount(HashMap param);
}
二、编写业务层代码
创建AmectService.java
接口,定义抽象方法。
public interface AmectService {
public PageUtils searchAmectByPage(HashMap param);
}
创建AmectServiceImpl.java
类,实现抽象方法。
@Service
@Slf4j
public class AmectServiceImpl implements AmectService {
@Autowired
private TbAmectDao amectDao;
@Override
public PageUtils searchAmectByPage(HashMap param) {
ArrayList<HashMap> list = amectDao.searchAmectByPage(param);
long count = amectDao.searchAmectCount(param);
int start = (Integer) param.get("start");
int length = (Integer) param.get("length");
PageUtils pageUtils = new PageUtils(list, count, start, length);
return pageUtils;
}
}
三、编写Web层代码
创建SearchAmectByPageForm.java
类,封装Ajax提交的数据。
@Data
@Schema(description = "查询罚款分页记录表单")
public class SearchAmectByPageForm {
@Pattern(regexp = "^[\\u4e00-\\u9fa5]{1,10}$", message = "name内容不正确")
@Schema(description = "姓名")
private String name;
@Min(value = 1, message = "deptId不能小于1")
@Schema(description = "部门编号")
private Integer deptId;
@Min(value = 1, message = "typeId不能小于1")
@Schema(description = "罚款类型编号")
private Integer typeId;
@Pattern(regexp = "^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$", message = "startDate内容不正确")
@Schema(description = "开始日期")
private String startDate;
@Pattern(regexp = "^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$", message = "endDate内容不正确")
@Schema(description = "截止日期")
private String endDate;
@Min(value = 1, message = "status不能小于1")
@Schema(description = "状态")
private Byte status;
@NotNull(message = "page不能为空")
@Min(value = 1, message = "page不能小于1")
@Schema(description = "页数")
private Integer page;
@NotNull(message = "length不能为空")
@Range(min = 10, max = 50, message = "length必须为10~50之间")
@Schema(description = "每页记录数")
private Integer length;
}
创建AmectController.java
类,定义Web方法。
@RestController
@RequestMapping("/amect")
@Tag(name = "AmectController", description = "罚款Web接口")
@Slf4j
public class AmectController {
@Autowired
private AmectService amectService;
@PostMapping("/searchAmectByPage")
@Operation(summary = "查询罚款分页记录")
@SaCheckLogin
public R searchAmectByPage(@Valid @RequestBody SearchAmectByPageForm form) {
if ((form.getStartDate() != null && form.getEndDate() == null) || (form.getStartDate() == null && form.getEndDate() != null)) {
return R.error("startDate和endDate只能同时为空,或者不为空");
}
int page = form.getPage();
int length = form.getLength();
int start = (page - 1) * length;
HashMap param = JSONUtil.parse(form).toBean(HashMap.class);
param.put("start", start);
param.put("currentUserId", StpUtil.getLoginIdAsInt());
if (!(StpUtil.hasPermission("AMECT:SELECT") || StpUtil.hasPermission("ROOT"))) {
param.put("userId", StpUtil.getLoginIdAsInt());
}
PageUtils pageUtils = amectService.searchAmectByPage(param);
return R.ok().put("page", pageUtils);
}
}
正常情况下,线上或者线下会议结束后,系统会自动生成未出席会议人员的罚款单。所以tb_amect
表中会有记录,所以我们用Swagger来测试Web方法,看看能否查询出来分页记录。