微信小程序开发,诗词鉴赏app,诗词推荐实现(二)

  1. 微信小程序开发,诗词鉴赏app(一): https://blog.csdn.net/jky_yihuangxing/article/details/143501681
  2. 微信小程序开发,诗词鉴赏app,诗词推荐实现(二):https://blog.csdn.net/jky_yihuangxing/article/details/143562367
  3. 微信小程序开发,诗词鉴赏app,诗词搜索实现(三):https://blog.csdn.net/jky_yihuangxing/article/details/143566194

1. 诗词推荐实现原理

通过用户的浏览量,来实现诗词的推荐

2. 代码实现过程

  1. 因为推荐是通过浏览量来实现的,所以需要创建一个history_table历史记录表,如下图所示:

在这里插入图片描述
其中poetry_id为诗词表里面的主键id,history_table会通过amount这个浏览量字段,获取到当前浏览量对应的poetry_id,然后通过poetry_id查询诗词表里面的诗词所有信息(这里涉及到多表联查)

  1. springBoot服务端接口设计
/**
     * 添加浏览历史
     */
    @RequestMapping(value = "addHistory", method = RequestMethod.GET)
    @ResponseBody
    public HttpResponse<HistoryInfo> addHistory(HistoryInfo historyInfo) {
    
    
        HttpResponse<HistoryInfo> httpResponse = new HttpResponse<>(StatusCode.Success);

        //判断是否添加过
        HistoryInfo history = userService.queryHistory(historyInfo);
        if (history == null) {
    
    
            int row = userService.addHistory(historyInfo);
            if (row > 0) {
    
    
                httpResponse.setMsg("添加成功");
                httpResponse.setCode(StatusCode.Success.getCode());
            } else {
    
    
                httpResponse.setMsg("添加失败");
                httpResponse.setCode(StatusCode.Fail.getCode());
            }
        } else {
    
    
            int row = userService.updateHistory(historyInfo);
            if (row > 0) {
    
    
                httpResponse.setMsg("更新成功");
                httpResponse.setCode(StatusCode.Success.getCode());
            } else {
    
    
                httpResponse.setMsg("更新失败");
                httpResponse.setCode(StatusCode.Fail.getCode());
            }
        }
        httpResponse.setData(new HistoryInfo());
        return httpResponse;

    }

推荐接口设计

    /**
     * 推荐
     */
    @RequestMapping(value = "queryRecommend", method = RequestMethod.GET)
    @ResponseBody
    public HttpResponse<HistoryListInfo> queryRecommend() {
    
    
        HttpResponse<HistoryListInfo> httpResponse = new HttpResponse<>(StatusCode.Success);
        List<HistoryInfo> historyInfoList = userService.queryRecommend();
        httpResponse.setData(new HistoryListInfo(historyInfoList));
        return httpResponse;
    }

代码应该不难理解,也写关键点的注释。首先要判断history_table历史记录表里面有没有添加过(也就是浏览过没有),如果没有就添加历史记录,否则就是更新浏览历史记录表,也就是修改amount字段+1

  1. sql语句编写
    <!--   添加浏览历史-->
    <insert id="addHistory" parameterType="com.app.chinese.poetry.domain.HistoryInfo">
        insert into history_table
        values
        (#{history_id},#{poetry_id},#{amount})
    </insert>

    <!--   判断是否存在记录-->
    <select id="queryHistory" parameterType="java.lang.Integer" resultType="com.app.chinese.poetry.domain.HistoryInfo">
        select * from history_table where poetry_id = #{poetry_id}
    </select>

    <!--   更新浏览历史-->
    <update id="updateHistory" parameterType="com.app.chinese.poetry.domain.HistoryInfo">
        update history_table
        <set>
            amount = amount + 1
        </set>
        where poetry_id = #{poetry_id}
    </update>

    <!--    推荐-->
    <select id="queryRecommend" resultMap="historyLists">
        select h.*,p.*
        from history_table h
        JOIN poetry_table p ON h.poetry_id = p.poetry_id
        where h.amount > 10
    </select>

where h.amount > 10 这里指 amount 浏览量大于10就查出来,这个可以根据自己项目需求来设置大小即可

JOIN ON 实现多表联查

这里注意:数据库的增,删,改,查语法,使用的是MyBatis来实现的

3. 实现效果

在这里插入图片描述

4. 关于本人其它项目的介绍

本人在b站录制的一些视频教程项目,免费供大家学习

  1. Android新闻资讯app实战:https://www.bilibili.com/video/BV1CA1vYoEad/?vd_source=984bb03f768809c7d33f20179343d8c8
  2. Androidstudio开发购物商城实战:https://www.bilibili.com/video/BV1PjHfeXE8U/?vd_source=984bb03f768809c7d33f20179343d8c8
  3. Android开发备忘录记事本实战:https://www.bilibili.com/video/BV1FJ4m1u76G?vd_source=984bb03f768809c7d33f20179343d8c8&spm_id_from=333.788.videopod.sections

猜你喜欢

转载自blog.csdn.net/jky_yihuangxing/article/details/143562367