山东大学创新实训9——博客论坛收藏

收藏功能不难,与之前相差不多
设计实体类

package com.wzh.springbootproject.entity;

/**
 * 收藏模块
 */
public class Collect {
    private Integer id;
    private Integer fid;
    private Integer userId;
    private String module;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getFid() {
        return fid;
    }

    public void setFid(Integer fid) {
        this.fid = fid;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getModule() {
        return module;
    }

    public void setModule(String module) {
        this.module = module;
    }
}

CollectService

package com.wzh.springbootproject.service;

import com.wzh.springbootproject.entity.Account;
import com.wzh.springbootproject.entity.Collect;
import com.wzh.springbootproject.mapper.CollectMapper;
import com.wzh.springbootproject.utils.TokenUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class CollectService {

    @Resource
    CollectMapper collectMapper;

    public void set(Collect collect) {
        Account currentUser = TokenUtils.getCurrentUser();
        collect.setUserId(currentUser.getId());
        Collect dblCollect = collectMapper.selectUserCollect(collect);
        if (dblCollect == null) {
            collectMapper.insert(collect);
        } else {
            collectMapper.deleteById(dblCollect.getId());
        }
    }

    /**
     * 查询当前用户是否收藏过
     */
    public Collect selectUserCollect(Integer fid, String module) {
        Account currentUser = TokenUtils.getCurrentUser();
        Collect collect = new Collect();
        collect.setUserId(currentUser.getId());
        collect.setFid(fid);
        collect.setModule(module);
        return collectMapper.selectUserCollect(collect);
    }

    public int selectByFidAndModule(Integer fid, String module) {
        return collectMapper.selectByFidAndModule(fid, module);
    }

}

CollectMapper

package com.wzh.springbootproject.mapper;

import com.wzh.springbootproject.entity.Collect;
import org.apache.ibatis.annotations.Param;

public interface CollectMapper {

    void insert(Collect collect);

    Collect selectUserCollect(Collect collect);

    void deleteById(Integer id);

    int selectByFidAndModule(@Param("fid") Integer fid, @Param("module") String module);
}

实现的
CollectMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wzh.springbootproject.mapper.CollectMapper">


    <insert id="insert">
        insert into collect (fid, user_id, module) values (#{fid}, #{userId}, #{module})
    </insert>

    <delete id="deleteById">
        delete from collect where id = #{id}
    </delete>

    <select id="selectUserCollect" resultType="com.wzh.springbootproject.entity.Collect">
        select * from collect where fid = #{fid} and user_id = #{userId} and module = #{module}
    </select>

    <select id="selectByFidAndModule" resultType="java.lang.Integer">
        select count(*) from collect where fid = #{fid} and module = #{module}
    </select>

</mapper>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值