소스 검색

点击一次 点击数加一

wangdy 7 년 전
부모
커밋
bed362b58f

+ 5 - 4
src/main/java/com/uas/cloud/mall/shop/news/data/NewsRepository.java

@@ -1,10 +1,7 @@
 package com.uas.cloud.mall.shop.news.data;
 
 import com.uas.cloud.mall.shop.news.model.News;
-import org.springframework.data.jpa.repository.JpaRepository;
-import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
-import org.springframework.data.jpa.repository.Query;
-import org.springframework.data.jpa.repository.QueryHints;
+import org.springframework.data.jpa.repository.*;
 import org.springframework.data.repository.query.Param;
 import org.springframework.stereotype.Repository;
 
@@ -29,4 +26,8 @@ public interface NewsRepository extends JpaSpecificationExecutor<News>, JpaRepos
             "where 1=1 and t.slug= :name ",nativeQuery = true)
     @QueryHints({@QueryHint(name = "org.hibernate.cacheable", value ="true")})
     List<Long> findNewsIdByTaxonomySlug (@Param("name")String taskName);
+
+    @Modifying
+    @Query("update News set viewCount = viewCount + 1 where id = :id")
+    void addCount(@Param("id")Long Id);
 }

+ 3 - 0
src/main/java/com/uas/cloud/mall/shop/news/service/impl/NewsServiceImpl.java

@@ -11,6 +11,7 @@ import org.springframework.data.domain.PageRequest;
 import org.springframework.data.domain.Pageable;
 import org.springframework.data.domain.Sort;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.StringUtils;
 
 import javax.persistence.criteria.Predicate;
@@ -27,8 +28,10 @@ public class NewsServiceImpl implements NewsService {
     private NewsRepository newsRepository;
 
     @Override
+    @Transactional
     public News findone(Long id) {
         News news = newsRepository.findOne(id);
+        newsRepository.addCount(id);
         //判断summary字段是否为空
         if (news != null && StringUtils.isEmpty(news.getSummary())) {
             news.setSummary(NewsCloumnCutUtil.getHtmlText(100,news.getContent()));