文章目录
Controller层
- Controller层
根据十次方项目的api可以看到, 点赞操作为put请求,只需传递点赞的吐槽id就行 controller层接收参数,调用service层进行点赞操作
/**
* 方法名: thumbup
* 方法描述: 进行点赞
* 修改日期: 2019/1/19 18:01
* @param spitId
* @return entity.Result
* @author taohongchao
* @throws
*/
@RequestMapping(value = "/thumbup/{spitId}",method = RequestMethod.PUT)
public Result thumbup(@PathVariable String spitId){
//调用service层,进行点赞
spitService.thumbup(spitId);
return new Result(true, StatusCode.OK, "点赞成功");
}
```
### service层
```java
/**
* 方法名: thumbup
* 方法描述: 点赞
* 修改日期: 2019/1/19 18:02
* @param spitId 吐槽的id
* @return void
* @author taohongchao
* @throws
*/
public void thumbup(String spitId) {
//通过吐槽的id,查询出实体类
Spit spit = spitDao.findById(spitId).get();
//首先获取该实体类的点赞数,判断是否为null,如果为null就给0, 不为null就直接获取值
//最后就把点赞字段数加一, 赋值给实体类
spit.setThumbup( (spit.getThumbup()==null ? 0 :spit.getThumbup())+1);
//保存点赞的实体
spitDao.save(spit);
}
```
### 测试
发送如下的put请求
`http://localhost:9006/spit/thumbup/1`
返回点赞成功.
通过id查询. thumbup字段加一了

### 效率问题
在service层的代码中,有两步操作dao层代码.这样效率是比较低的,因此要进行优化.在下一节中,进行优化
