【发布时间】:2019-01-14 01:48:58
【问题描述】:
我正在构建一个类似 Quora 的应用程序。后端使用spring boot、mybatis连接一个mysql数据库。当用户打开网站时,后端会返回前 10 个问题。如果用户点击“获取更多”按钮,后端应该返回接下来的 10 个问题。
mybatis的代码是
<mapper namespace="com.quora.dao.QuestionDAO">
<sql id="table">question</sql>
<sql id="selectFields">id, title, content, comment_count,created_date,user_id
</sql>
<select id="selectLatestQuestions" resultType="com.quora.model.Question">
SELECT
<include refid="selectFields"/>
FROM
<include refid="table"/>
<if test="userId != 0">
WHERE user_id = #{userId}
</if>
ORDER BY id DESC
LIMIT #{offset},#{limit}
</select>
</mapper>
目前我的逻辑是第一次#{offset}为0,第二次#{offset}为10。但是当表频繁更新时我发现这个逻辑不正确。如果表已插入新行,用户可能会得到重复数据。如何根据前端显示的最后一个问题 ID 设置 #{offset}?比如前端最后一个question id是10,那么#{offset}应该是question id 10的行号。
谁能给我一些建议?
谢谢, 彼得
【问题讨论】:
标签: mysql spring-boot mybatis spring-mybatis