关注的数字可以点进去,
上节课把关注的数据都做了一个保存,所以这节不需要开发dao数据访问层
1.在follow-service中添加findFollowees、findFollowers方法
// 查询某用户关注的人
public List<Map<String, Object>> findFollowees(int userId, int offset, int limit) {//返回的应该是user和时间整合一起的东西。userId即为某用户
String followeeKey = RedisKeyUtil.getFolloweeKey(userId, ENTITY_TYPE_USER);//某用户userId,关注的人ENTITY_TYPE_USER
Set<Integer> targetIds = redisTemplate.opsForZSet().reverseRange(followeeKey, offset, offset + limit - 1);//reverseRange表示由大到小查询,Range由小到大。从offset开始查,截止索引limit
if (targetIds == null) {
return null;
}
List<Map<String, Object>> list = new ArrayList<>();//实例化集合
for (Integer targetId : targetIds) {//遍历targetIds,取到每一个用户
Map<String, Object> map = new HashMap<>();
User user = userService.findUserById(targetId);
map.put("user", user);
Double score = redisTemplate.opsForZSet().score(followeeKey, targetId);//查到targetId 对应的targetId,得到一个小数
map.put("followTime", new Date(score.longValue()));//将score 还原为时间。followTime为关注时间
list.add(map);//将map放入集合
}
return list;
}
// 查询某用户的粉丝
public List<Map<String, Object>> findFollowers(int userId, int offset, int limit) {
String followerKey = RedisKeyUtil.getFollowerKey(ENTITY_TYPE_USER, userId);
Set<Integer> targetIds = redisTemplate.opsForZSet().reverseRange(followerKey, offset, offset + limit - 1);
if (targetIds == null) {
return null;
}
List<Map<String, Object>> list = new ArrayList<>();
for (Integer targetId : targetIds) {
Map<String, Object> map = new HashMap<>();
User user = userService.findUserById(targetId);
map.put("user", user);
Double score = redisTemplate.opsForZSet().score(followerKey, targetId);
map.put("followTime", new Date(score.longValue()));
list.add(map);
}
return list;
}
2.在FollowController中添加:
传入user最终在这里显示
@RequestMapping(path = "/followees/{userId}", method = RequestMethod.GET)//查某个用户userId 关注的人
public String getFollowees(@PathVariable("userId") int userId, Page page, Model model) {
User user = userService.findUserById(userId);
if (user == null) {
throw new RuntimeException("该用户不存在!");
}
model.addAttribute("user", user);//把user传给页面
//对分页的条件进行设置
page.setLimit(5);//每页显示五条数据
page.setPath("/followees/" + userId);
page.setRows((int) followService.findFolloweeCount(userId, ENTITY_TYPE_USER));//查询一共多少行数据,从而查询总的页数
List<Map<String, Object>> userList = followService.findFollowees(userId, page.getOffset(), page.getLimit());
if (userList != null) {
for (Map<String, Object> map : userList) {//每次遍历userList,得到一个个map
User u = (User) map.get("user");//当前用户对 该用户的关注状态 用hasFollowed方法
map.put("hasFollowed", hasFollowed(u.getId()));
}
}
model.addAttribute("users", userList);
return "/site/followee";
}
@RequestMapping(path = "/followers/{userId}", method = RequestMethod.GET)
public String getFollowers(@PathVariable("userId") int userId, Page page, Model model) {
User user = userService.findUserById(userId);
if (user == null) {
throw new RuntimeException("该用户不存在!");
}
model.addAttribute("user", user);
page.setLimit(5);
page.setPath("/followers/" + userId);
page.setRows((int) followService.findFollowerCount(ENTITY_TYPE_USER, userId));
List<Map<String, Object>> userList = followService.findFollowers(userId, page.getOffset(), page.getLimit());
if (userList != null) {
for (Map<String, Object> map : userList) {
User u = (User) map.get("user");
map.put("hasFollowed", hasFollowed(u.getId()));
}
}
model.addAttribute("users", userList);
return "/site/follower";
}
private boolean hasFollowed(int userId) {
if (hostHolder.getUser() == null) {//若是当前每页用户登录
return false;
}
return followService.hasFollowed(hostHolder.getUser().getId(), ENTITY_TYPE_USER, userId);//把当前用户传进去
}
}
3.在profile.html中
4.在followee中配置如下:
配置thymeleaf
还有:
接着编辑:返回个人主页,即返回到profile.html:
点击:
返回到:
follower.html中配置如下:
测试:
点 个人主页