UserFollowServiceImpl:
@Service public class UserFollowServiceImpl implements UserFollowService { @Resource UserFollowMapper userFollowMapper; @Autowired NotifyService notifyService; @Override public void userAFollowUserBById(Long userAId, Long userBId) { // 先要创建一条提示消息 notifyService.addNotify(userAId, userBId, "follow");// 关注信息 UserFollow userFollow = new UserFollow(); userFollow.setUid(userAId); userFollow.setFollowUid(userBId); userFollowMapper.insertSelective(userFollow); } @Override public void userAUnfollowUserBById(Long userAId, Long userBId) { // 首先查询到相关的记录 UserFollowExample example = new UserFollowExample(); example.or().andUidEqualTo(userAId).andFollowUidEqualTo(userBId); UserFollow userFollow = userFollowMapper.selectByExample(example).get(0); // 删除关注数据 userFollowMapper.deleteByPrimaryKey(userFollow.getId()); } }NotifyServiceImpl:
@Service public class NotifyServiceImpl implements NotifyService { @Resource NotifyMapper notifyMapper; @Override public int addNotify(Long senderId, Long reciverId, String type) { Notify notify = new Notify(null, senderId, reciverId, type, false, null); return notifyMapper.insertSelective(notify);// id和creatTime自动生成. } @Override public void readNotifyById(Long id) { Notify notify = notifyMapper.selectByPrimaryKey(id); notify.setIsRead(true); notifyMapper.updateByPrimaryKey(notify); } @Override public List<Notify> findAllNotifyByReciverId(Long id) { List<Notify> notifies = new LinkedList<>(); NotifyExample example = new NotifyExample(); example.setOrderByClause("`id` DESC");// 按id倒叙,也就是第一个数据是最新的. example.or().andReciverIdEqualTo(id); notifies.addAll(notifyMapper.selectByExample(example)); return notifies; } @Override public List<Notify> findAllUnReadNotifyByReciverId(Long id) { List<Notify> notifies = new LinkedList<>(); NotifyExample example = new NotifyExample(); example.setOrderByClause("`id` DESC");// 按id倒叙,也就是第一个数据是最新的. example.or().andReciverIdEqualTo(id).andIsReadEqualTo(false); notifies.addAll(notifyMapper.selectByExample(example)); return notifies; } }MessageServiceImpl:
@Service public class MessageServiceImpl implements MessageService { @Resource MessageMapper messageMapper; @Resource NotifyService notifyService; @Override public void addMessage(Long senderId, Long reciverId, String content) { // 先创建一条 notify 数据 Long notifyId = (long) notifyService.addNotify(senderId, reciverId, "message");// message表示私信 // 增加一条私信信心 Message message = new Message(null, notifyId, senderId, reciverId, content, null); messageMapper.insertSelective(message);// 插入非空项,id/createTime数据库自动生成 } @Override public void deleteMessageById(Long id) { messageMapper.deleteByPrimaryKey(id); } @Override public Message findMessageByNotifyId(Long id) { // 触发方法时应把消息置为已读 notifyService.readNotifyById(id); MessageExample example = new MessageExample(); example.or().andNotifyIdEqualTo(id); return messageMapper.selectByExample(example).get(0); } } 第四步:Controller 层也很简单,只是为了看效果
UserController:
@RestController public class UserController { @Autowired UserService userService; @PostMapping("/addUser") public String addUser(@RequestParam String username) { userService.addUserByUsername(username); return "Success!"; } @GetMapping("/findUser") public User findUser(@RequestParam Long id) { return userService.findUserById(id); } }UserFollowController :
@RestController public class UserFollowController { @Autowired UserFollowService userFollowService; @PostMapping("/follow") public String follow(@RequestParam Long userAId, @RequestParam Long userBId) { userFollowService.userAFollowUserBById(userAId, userBId); return "Success!"; } @PostMapping("/unfollow") public String unfollow(@RequestParam Long userAId, @RequestParam Long userBId) { userFollowService.userAUnfollowUserBById(userAId, userBId); return "Success!"; } }NotifyController :
@RestController public class NotifyController { @Autowired NotifyService notifyService; @PostMapping("/addNotify") public String addNotify(@RequestParam Long senderId, @RequestParam Long reciverId, @RequestParam String type) { notifyService.addNotify(senderId, reciverId, type); return "Success!"; } @PostMapping("/readNotify") public String readNotify(@RequestParam Long id) { notifyService.readNotifyById(id); return "Success!"; } @GetMapping("/listAllNotify") public List<Notify> listAllNotify(@RequestParam Long id) { return notifyService.findAllNotifyByReciverId(id); } @GetMapping("/listAllUnReadNotify") public List<Notify> listAllUnReadNotify(@RequestParam Long id) { return notifyService.findAllUnReadNotifyByReciverId(id); } }