Pārlūkot izejas kodu

阅读量接口

yusm 6 mēneši atpakaļ
vecāks
revīzija
519899bd78

+ 8 - 0
fhKeeper/formulahousekeeper/webttkuaiban/src/main/java/com/firerock/webttkuaiban/demos/controller/ArticleController.java

@@ -90,4 +90,12 @@ public class ArticleController {
         return map;
     }
 
+    @GetMapping("/viewArticle")
+    public Object viewArticle(@RequestParam("id") Integer id){
+        articleService.updateViewCountById(id);
+        Map<String, Object> map = new HashMap<>();
+        map.put("data", "success");
+        return map;
+    }
+
 }

+ 7 - 0
fhKeeper/formulahousekeeper/webttkuaiban/src/main/java/com/firerock/webttkuaiban/demos/mapper/ArticleMapper.java

@@ -4,6 +4,7 @@ import com.firerock.webttkuaiban.demos.pojo.Article;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Param;
 import org.apache.ibatis.annotations.Select;
+import org.apache.ibatis.annotations.Update;
 
 import java.util.List;
 
@@ -19,4 +20,10 @@ public interface ArticleMapper {
 
     @Select("select id,title,content,category_ids,create_time,product_id,profile from article where id = #{id}")
     Article getArticleById(Integer id);
+
+    @Update(" update article set view_count = view_count + 1 where id= #{id}")
+    void updateViewCountById(Integer id);
+
+    @Select("SELECT id,view_count FROM article WHERE id = #{articleId} FOR UPDATE")
+    Article selectForUpdate(Integer articleId);
 }

+ 1 - 0
fhKeeper/formulahousekeeper/webttkuaiban/src/main/java/com/firerock/webttkuaiban/demos/pojo/Article.java

@@ -37,6 +37,7 @@ public class Article {
     private String baseImage;
     private String productId;
     private String coverImgUrl;
+    private Integer viewCount;
 
     @JsonFormat(pattern = "yyyy-MM-dd")
     @DateTimeFormat(pattern = "yyyy-MM-dd")

+ 2 - 0
fhKeeper/formulahousekeeper/webttkuaiban/src/main/java/com/firerock/webttkuaiban/demos/service/ArticleService.java

@@ -16,4 +16,6 @@ public interface ArticleService {
     List<Article> relatedList(Integer id);
 
     Article getArticleById(Integer id);
+
+    void updateViewCountById(Integer id);
 }

+ 11 - 0
fhKeeper/formulahousekeeper/webttkuaiban/src/main/java/com/firerock/webttkuaiban/demos/service/impl/ArticleServiceImpl.java

@@ -9,6 +9,7 @@ import com.firerock.webttkuaiban.demos.pojo.Category;
 import com.firerock.webttkuaiban.demos.service.ArticleService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.StringUtils;
 import java.util.ArrayList;
 import java.util.List;
@@ -73,4 +74,14 @@ public class ArticleServiceImpl implements ArticleService {
         }
         return article;
     }
+
+
+    @Override
+    @Transactional
+    public void updateViewCountById(Integer articleId) {
+        // 先锁定行
+        articleMapper.selectForUpdate(articleId);
+        // 然后更新浏览量
+        articleMapper.updateViewCountById(articleId);
+    }
 }