目录
业务层
一、定义Message业务接口
二、定义Message业务实现类
Web层
一、获取分页消息列表
二、根据ID查询消息
三、把未读消息更新成已读消息
四、删除消息
业务层
一、定义Message业务接口
创建 MessageService.java 类
public interface MessageService {public String insertMessage(MessageEntity entity);public List<HashMap> searchMessageByPage(int userId, long start, int length);public HashMap searchMessageById(String id);public String insertRef(MessageRefEntity entity);public long searchUnreadCount(int userId);public long searchLastCount(int userId);public long updateUnreadMessage(String id);public long deleteMessageRefById(String id);public long deleteUserMessageRef(int userId);
}
二、定义Message业务实现类
创建 MessageServiceImpl.java 类
@Service
public class MessageServiceImpl implements MessageService {@Autowiredprivate MessageDao messageDao;@Autowiredprivate MessageRefDao messageRefDao;@Overridepublic String insertMessage(MessageEntity entity) {String id=messageDao.insert(entity);return id;}@Overridepublic List<HashMap> searchMessageByPage(int userId, long start, int length) {List<HashMap> list=messageDao.searchMessageByPage(userId,start,length);return list;}@Overridepublic HashMap searchMessageById(String id) {HashMap map=messageDao.searchMessageById(id);return map;}@Overridepublic String insertRef(MessageRefEntity entity) {String id=messageRefDao.insert(entity);return id;}@Overridepublic long searchUnreadCount(int userId) {long count=messageRefDao.searchUnreadCount(userId);return count;}@Overridepublic long searchLastCount(int userId) {long count=messageRefDao.searchLastCount(userId);return count;}@Overridepublic long updateUnreadMessage(String id) {long rows=messageRefDao.updateUnreadMessage(id);return rows;}@Overridepublic long deleteMessageRefById(String id) {long rows=messageRefDao.deleteMessageRefById(id);return rows;}@Overridepublic long deleteUserMessageRef(int userId) {long rows=messageRefDao.deleteUserMessageRef(userId);return rows;}
}
Web层
一、获取分页消息列表
创建 SearchMessageByPageForm.java 类
@Data
@ApiModel
public class SearchMessageByPageForm {@NotNull@Min(1)private Integer page;@NotNull@Range(min = 1,max = 40)private Integer length;
}
创建 MessageController.java 类
@RestController
@RequestMapping("/message")
@Api("消息模块网络接口")
public class MessageController {@Autowiredprivate JwtUtil jwtUtil;@Autowiredprivate MessageService messageService;@PostMapping("/searchMessageByPage")@ApiOperation("获取分页消息列表")public R searchMessageByPage(@Valid @RequestBody SearchMessageByPageForm form, @RequestHeader("token") String token) {int userId = jwtUtil.getUserId(token);int page = form.getPage();int length = form.getLength();long start = (page - 1) * length;List<HashMap> list = messageService.searchMessageByPage(userId, start, length);return R.ok().put("result", list);}
}
二、根据ID查询消息
创建 SearchMessageByIdForm.java 类
@Data
@ApiModel
public class SearchMessageByIdForm {@NotNullprivate String id;
}
在 MessageController.java 中编写Web方法
……@PostMapping("/searchMessageById")@ApiOperation("根据ID查询消息")public R searchMessageById(@Valid @RequestBody SearchMessageByIdForm form) {HashMap map = messageService.searchMessageById(form.getId());return R.ok().put("result", map);}
三、把未读消息更新成已读消息
创建 UpdateUnreadMessageForm.java 类
@Data
@ApiModel
public class UpdateUnreadMessageForm {@NotNullprivate String id;
}
在 MessageController.java 中编写Web方法
…… @PostMapping("/updateUnreadMessage")@ApiOperation("未读消息更新成已读消息")public R updateUnreadMessage(@Valid @RequestBody UpdateUnreadMessageForm form) {long rows = messageService.updateUnreadMessage(form.getId());return R.ok().put("result", rows == 1 ? true : false);}
四、删除消息
创建 DeleteMessageRefByIdForm.java 类
@Data
@ApiModel
public class DeleteMessageRefByIdForm {@NotNullprivate String id;
}
在 MessageController.java 中编写Web方法
……@PostMapping("/deleteMessageRefById")@ApiOperation("删除消息")public R deleteMessageRefById(@Valid @RequestBody DeleteMessageRefByIdForm form) {long rows = messageService.deleteMessageRefById(form.getId());return R.ok().put("result", rows == 1 ? true : false);}