发表于: 2017-12-15 23:18:56
1 525
今天完成的任务:
一、完成前台文章详情接口
@RequestMapping(value = "/a/u/article/{id}", method = RequestMethod.GET)
public String getArticleJson(ModelMap model, @PathVariable Long id,Long uid) throws Exception {
log.info("get data : id= " + id);
try {
Article article = articleService.getObjectById(id);
log.info("get article data is " + article);
Long cid=collectionService.getCollectionIDByUIDAndTargetIDAndType(uid,id,1);
Collection collection=collectionService.getObjectById(cid);
model.addAttribute("code", 0);
model.addAttribute("article", article);
model.addAttribute("collection",collection);
} catch (Throwable t) {
t.printStackTrace();
log.error(t.getMessage());
log.error("get article error,id is " + id);
model.addAttribute("code", -100000);
}
return "/article/json/articleDetailJson";
}
大概就根据id查询到一个对象。然后再去根据用户id,文章id,去收藏表里面查询到一个对象。返回收藏状态和点赞状态。就可以对应的显示在前台点赞和收藏的状态。
二、使用另一个方法使用sql语句。
公司框架里面使用sql大概有三种方法:
3.自己从dao.xml 开始写起,到Service,再到Controller里面去获取。
说一下第三种。
1.首先在映射文件里面添加好对应的sql。
<map name="getCollectionIDByUIDAndTargetIDAndType"
sqlitem="select id from collection where uid = ? and target_id = ? and type = ?"
keyProperty="uid,target_id,type" valueProperty="id" keyColumn="uid,target_id,type"/>
2.在Service接口里面写对应的方法。
public Long getCollectionIDByUIDAndTargetIDAndType(Long uid ,Long target_id, int type)throws ServiceException,ServiceDaoException;
3.在Service实现类写对应的是实现方法。。具体是调用dao.getMapping 的方法获得对应的返回值
@Override
public Long getCollectionIDByUIDAndTargetIDAndType(Long uid ,Long target_id, int type) throws ServiceDaoException,ServiceException{
Long id=null;
log.info("get uid ,target_id ,type uid" +uid +" , targetid"+target_id+" , type"+type);
try {
id= (Long) dao.getMapping("getCollectionIDByUIDAndTargetIDAndType",new Object[] {uid,target_id,type});
} catch (DaoException e) {
log.info("get id wrong getCollectionIDByUIDAndTargetIDAndType");
e.printStackTrace();
}
log.info("get id success " + id);
return id;
}
4.在core里面的client中添加对应的一个方法。
@Override
public Long getCollectionIDByUIDAndTargetIDAndType(Long uid ,Long target_id, int type)
throws ServiceException, ServiceDaoException {
return collectionService.getCollectionIDByUIDAndTargetIDAndType(uid,target_id,type);
}
这样就完成了。
明天完成的任务:
明天要写点赞收藏的接口。
看一下怎么配置服务器环境
遇到问题:
今天碰到一个问题卡了一下午加一晚上。。。
就是使用collection的Controller的时候,死活调用不到Service的方法,每次都会报错连接问题。
想了好久也没找到问题在哪。结果后来问超哥,是因为在resources里面少了对应的记录。。
之前是知道要配置resources的,但是疏忽了,漏了两个。。。太粗心了。
收获:
接口写了一个。sql语句知道怎么写。报错。。。
评论