项目二08
1.攻略评论
实体类
@Setter
@Getter
@Document("strategy_comment")
@ToString
public class StrategyComment {
@Id
private String id;
private Long strategyId; //攻略(明细)id
private String strategyTitle; //攻略标题
private Long userId; //用户id
private String nickname; //用户名
private String city;
private int level;
private String headImgUrl; //头像
private Date createTime; //创建时间
private String content; //评论内容
private int thumbupnum; //点赞数
private List<Long> thumbuplist = new ArrayList<>();
}
添加逻辑(使用了对象复制的工具类)
@RequiredLogin
@PostMapping("/commentAdd")
public Object commentAdd(StrategyComment strategyComment, HttpServletRequest request) {
String token = request.getHeader("token");
UserInfo userInfo = userInfoRedisSerivce.getUserByToken(token);
BeanUtils.copyProperties(userInfo,strategyComment);
strategyComment.setUserId(userInfo.getId());
strategyCommentService.save(strategyComment);
//评论数加1
strategyStatisVOService.replyIncrease(strategyComment.getStrategyId());
return JsonResult.success();
}
查询逻辑
@Override
public Page<StrategyComment> queryPage(StrategyCommentQuery qo) {
Query query = new Query();
if(qo.getStrategyId() != null){
query.addCriteria(Criteria.where("strategyId").is(qo.getStrategyId()));
}
long count = mongoTemplate.count(query, StrategyComment.class);
Pageable pageable = PageRequest.of(qo.getCurrentPage() -1,qo.getPageSize());
query.with(pageable);
List<StrategyComment> strategyComments = mongoTemplate.find(query, StrategyComment.class);
return new PageImpl<StrategyComment>(strategyComments,pageable,count);
}
实现类
@Override
public void save(StrategyComment strategyComment) {
//id如果是""空串,spring-data之后,不会自动将id添加到id属性中
strategyComment.setId(null);
strategyComment.setCreateTime(new Date());
repository.save(strategyComment);
}
去除class属性,在Core项目中配置
@Bean
public MappingMongoConverter mappingMongoConverter(MongoDatabaseFactory factory, MongoMappingContext context, BeanFactory beanFactory) {
DbRefResolver dbRefResolver = new DefaultDbRefResolver(factory);
MappingMongoConverter mappingConverter = new MappingMongoConverter(dbRefResolver, context);
try {
mappingConverter.setCustomConversions(beanFactory.getBean(CustomConversions.class));
} catch (NoSuchBeanDefinitionException ignore) {
}
// Don't save _class to mongo
mappingConverter.setTypeMapper(new DefaultMongoTypeMapper(null));
return mappingConverter;
}
2.游记评论
实体类
@Setter
@Getter
@Document("travel_comment")
public class TravelComment {
public static final int TRAVLE_COMMENT_TYPE_COMMENT = 0; //普通评论
public static final int TRAVLE_COMMENT_TYPE = 1; //评论的评论
@Id
private String id; //id
private Long travelId; //游记id
private String travelTitle; //游记标题
private Long userId; //用户id
private String nickname; //用户名
private String city;
private int level;
private String headImgUrl; // 用户头像
private int type = TRAVLE_COMMENT_TYPE_COMMENT; //评论类别
private Date createTime; //创建时间
private String content; //评论内容
private TravelComment refComment; //关联的评论
}
添加逻辑
@PostMapping("/commentAdd")
public Object commentAdd(TravelComment travelComment, HttpServletRequest request){
//通过token获取User对象
String token = request.getHeader("token");
UserInfo userInfo = userInfoRedisSerivce.getUserByToken(token);
//复制对象内容
BeanUtils.copyProperties(userInfo,travelComment);
//插入TravelComment
travelCommentService.save(travelComment);
return JsonResult.success();
}
实现逻辑
@Override
public List<TravelComment> queryByTravelId(Long travelId) {
return repository.findByTravelId(travelId);
}
3.参数解析器
@Component
public class UserInfoArgumentResolver implements HandlerMethodArgumentResolver {
@Autowired
private IUserInfoRedisSerivce userInfoRedisSerivce;
//只能解析器能解析的类型
@Override
public boolean supportsParameter(MethodParameter methodParameter) {
return methodParameter.getParameterType() == UserInfo.class && methodParameter.hasParameterAnnotation(UserParm.class);
}
//上面的方法为true后才会去执行下面的方法
@Override
public Object resolveArgument(MethodParameter methodParameter,
ModelAndViewContainer modelAndViewContainer,
NativeWebRequest webRequest,
WebDataBinderFactory webDataBinderFactory) throws Exception {
//获取当前登录的用户对象
HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
String token = request.getHeader("token");
UserInfo userByToken = userInfoRedisSerivce.getUserByToken(token);
return userByToken;
}
}
为了区分是SpringMVC封装前端的数据还是用户自定义对象,需要使用注解来标识
@RequiredLogin
@PostMapping("/favor")
public Object favor(Long sid, @UserParm UserInfo userInfo) {
boolean ret = strategyStatisVOService.favor(sid,userInfo.getId());
return JsonResult.success(ret);
}
//--------添加注解---------------
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface UserParm{
}
4.数据统计
缓存技术:map集合,Ehcache框架,redis和memecache组件
使用redis需要注意如何设计key
方案一:使用hash类型
方案二:使用String类型
代码截图
@Override
public void increateViewnum(Long id) {
//拼接key
//String key = RedisKeys.STRATEGY_STATIC_VO.join(id.toString());
StrategyStatisVO vo = this.getStatisVO(id);
////根据key找vo对象
////不存在,初始化vo对象
//if (!template.hasKey(key)) {
// Strategy strategy = strategyService.getById(id);
// vo = new StrategyStatisVO();
//
// BeanUtils.copyProperties(strategy, vo);
// vo.setStrategyId(strategy.getId());
//
// //缓存
// template.opsForValue().set(key, JSON.toJSONString(vo));
//}
////存在则获取
//else {
// String str = template.opsForValue().get(key);
// //template.opsForValue()
// vo = JSON.parseObject(str, StrategyStatisVO.class);
//}
//修改vo对象的属性值
vo.setViewnum(vo.getViewnum() + 1);
//重新设置
//template.opsForValue().set(key, JSON.toJSONString(vo));
setStatisVO(vo);
}