背景:
数据库中counts表中count字段记录pv数.此网页每被浏览一次,pv数加一.
错误做法:
每浏览一次----->从数据库中查出pv数----->加一------>update入库.
(错误原因:并发浏览时则会出现计数误差,此方案不可行)
正确做法:
每浏览一次----->直接写库
insert into counts
(id, count)
values
<foreach collection="list" item="item" index="index" separator=",">
(
#{item.id,jdbcType=BIGINT},
#{item.count,jdbcType=BIGINT}
)
</foreach>
ON DUPLICATE KEY UPDATE
count=count+VALUES(count)