1,访问数据库数据,多个线程访问数据库数据,有可能导致同步修改数据
1.1增加一个方法:questionService.incView(id);
package xgl.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import xgl.dto.QuestionDTO;
import xgl.service.QuestionService;
@Controller
public class QuestionController {
@Autowired
private QuestionService questionService;
@GetMapping("/question/{id}")
public String question(@PathVariable(name = "id") Integer id,
Model model) {
QuestionDTO questionDTO=questionService.getById(id);
//增加阅读数
questionService.incView(id);
model.addAttribute("question",questionDTO);
return "question";
}
}
1.2,另起一个mapper,实现上面的方法
package xgl.mapper;
import xgl.model.Question;
public interface QuestionExtMapper {
int incView(Question record);
}
1.3,对应的mapper.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="xgl.mapper.QuestionExtMapper">
<resultMap id="BaseResultMap" type="xgl.model.Question">
<id column="ID" jdbcType="INTEGER" property="id" />
<result column="TITLE" jdbcType="VARCHAR" property="title" />
<result column="GMT_CREATE" jdbcType="BIGINT" property="gmtCreate" />
<result column="GMT_MODIFIED" jdbcType="BIGINT" property="gmtModified" />
<result column="CREATOR" jdbcType="INTEGER" property="creator" />
<result column="COMMENT_COUNT" jdbcType="INTEGER" property="commentCount" />
<result column="VIEW_COUNT" jdbcType="INTEGER" property="viewCount" />
<result column="LIKE_COUNT" jdbcType="INTEGER" property="likeCount" />
<result column="TAG" jdbcType="VARCHAR" property="tag" />
</resultMap>
<!--阅读数增加-->
<update id="incView" parameterType="xgl.model.Question">
UPDATE QUESTION
SET
VIEW_COUNT=VIEW_COUNT + #{viewCount,jdbcType=INTEGER}
where id=#{id}
</update>
</mapper>
2,处理逆向工程重写覆盖问题(会把修改的覆盖掉!)
解决思路:另起炉灶
XXXExtMapper.xml
XXXExtMapper.java