@Override
@Transactional
public String getGoodByMySQL(Long id) {
Suser suser=suserMapper.findByIdForUpdate(id);
if(suser.getNum()<=0){
return "商品数量不足";
}
suser.setNum(suser.getNum()-1);
suserMapper.updateById(suser);
return "抢购成功";
}
事务中间查询时加入了排它锁
<select id="findByIdForUpdate" resultType="org.monitor.bean.Suser">
select *
from suser
<where>
id=#{id}
</where>
for update
</select>