上个小节我们看明白了复杂的SQL语句是怎么演进出来的,持久层的代码我们也写完了,本小节我们去把业务层也Web层的代码给写一下。
一、编写业务层代码
创建MeetingService.java
接口,然后定义抽象方法。
public interface MeetingService {
public PageUtils searchOfflineMeetingByPage(HashMap param);
}
创建MeetingServiceImpl.java
类,实现抽象方法。这里需要额外注意
@Service
@Slf4j
public class MeetingServiceImpl implements MeetingService {
@Autowired
private TbMeetingDao meetingDao;
@Override
public PageUtils searchOfflineMeetingByPage(HashMap param) {
ArrayList<HashMap> list = meetingDao.searchOfflineMeetingByPage(param);
long count = meetingDao.searchOfflineMeetingCount(param);
int start = (Integer) param.get("start");
int length = (Integer) param.get("length");
//把meeting字段转换成JSON数组对象
for (HashMap map : list) {
String meeting = (String) map.get("meeting");
//如果Meeting是有效的字符串,就转换成JSON数组对象
if (meeting != null && meeting.length() > 0) {
map.replace("meeting", JSONUtil.parseArray(meeting));
}
}
PageUtils pageUtils = new PageUtils(list, count, start, length);
return pageUtils;
}
}
二、编写Web层代码
创建SearchOfflineMeetingByPageForm.java
类,封装Ajax提交的数据。
@Data
@Schema(description = "查询线下会议分页表单")
public class SearchOfflineMeetingByPageForm {
@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 = "date内容不能为空")
@Schema(description = "日期")
private String date;
@NotNull(message = "mold不能空")
@Pattern(regexp = "^全部会议$|^我的会议$", message = "mold内容错误")
@Schema(description = "模式")
private String mold;
@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;
}
创建MeetingController.java
类,定义Web方法,然后用Swagger程序测试一下。
@RestController
@RequestMapping("/meeting")
@Tag(name = "MeetingController", description = "会议Web接口")
public class MeetingController {
@Autowired
private MeetingService meetingService;
@PostMapping("/searchOfflineMeetingByPage")
@Operation(summary = "查询线下会议分页数据")
@SaCheckLogin
public R searchOfflineMeetingByPage(@Valid @RequestBody SearchOfflineMeetingByPageForm form) {
int page = form.getPage();
int length = form.getLength();
int start = (page - 1) * length;
HashMap param = new HashMap() {{
put("date", form.getDate());
put("mold", form.getMold());
put("userId", StpUtil.getLoginId());
put("start", start);
put("length", length);
}};
PageUtils pageUtils = meetingService.searchOfflineMeetingByPage(param);
return R.ok().put("page", pageUtils);
}
}