后端实现点赞功能(java web)

后端实现点赞功能(java web)

今天写了一个点赞功能,实现的主要思想是:
1.点击点赞按钮将great属性的数值增加1并记录进great表
2.再次点击则减1(即在great表中有记录时减1)
3.用遍历的方法实现对是否已经点击过进行判断
Dao层代码如下:

//点赞
    public Great findGreat(Integer aid, Integer uid) {
        String hql="from Great where aid=? and uid=? ";
        List list = this.getHibernateTemplate().find(hql,aid,uid);
        if(list!=null && list.size()>0){
            this.getHibernateTemplate().delete(list.get(0));            
            Answer answer=this.getHibernateTemplate().get(Answer.class,aid);
            answer.setAgreat(answer.getAgreat()-1);
            this.getHibernateTemplate().update(answer);
            return null;
        }
        else {
            Great great = new Great();
            Answer answer2=this.getHibernateTemplate().get(Answer.class,aid);
            great.setAnswer(answer2);
            User user=this.getHibernateTemplate().get(User.class, uid);
            great.setUser(user);
            this.getHibernateTemplate().save(great);
            answer2.setAgreat(answer2.getAgreat()+1);
            this.getHibernateTemplate().update(answer2);
            return null;
        }

    }

你可能感兴趣的:(后端实现点赞功能(java web))