diff --git a/len-blog/src/main/java/com/len/controller/ArticleController.java b/len-blog/src/main/java/com/len/controller/ArticleController.java index 9676bab..3be6821 100644 --- a/len-blog/src/main/java/com/len/controller/ArticleController.java +++ b/len-blog/src/main/java/com/len/controller/ArticleController.java @@ -15,6 +15,7 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import tk.mybatis.mapper.entity.Condition; import javax.servlet.http.HttpServletRequest; import java.text.SimpleDateFormat; @@ -96,6 +97,14 @@ public class ArticleController { return articleService.detail(code, ip); } + /** + * 根据标签code获取文章 + * + * @param tagName + * @param page + * @param limit + * @return + */ @GetMapping("/article/list/{tagName}") public ReType getArticleByTag(@PathVariable("tagName") String tagName, Integer page, Integer limit) { limit = limit > 100 ? 100 : limit; @@ -104,4 +113,19 @@ public class ArticleController { List articles = articleService.selectArticleByTag(tagName); return new ReType(startPage.getTotal(), startPage.getPageNum(), articles); } + + @GetMapping("/article/list/order/read") + public ReType getArticleByReadNumber() { + Condition condition = new Condition(BlogArticle.class); + PageHelper.startPage(1, 5); + condition.createCriteria(); + condition.orderBy("readNumber").desc(); + List articles = articleService.selectByExample(condition); + articles.forEach(s -> { + s.setContent(null); + }); + ReType reType = new ReType(); + reType.setData(articles); + return reType; + } } diff --git a/len-blog/src/main/java/com/len/service/impl/BlogArticleServiceImpl.java b/len-blog/src/main/java/com/len/service/impl/BlogArticleServiceImpl.java index 740a34b..4215077 100644 --- a/len-blog/src/main/java/com/len/service/impl/BlogArticleServiceImpl.java +++ b/len-blog/src/main/java/com/len/service/impl/BlogArticleServiceImpl.java @@ -108,7 +108,10 @@ public class BlogArticleServiceImpl extends BaseServiceImpl } BlogArticle blogArticle = detail.getArticle(); //点击次数 - addArticleReadNum(ip, blogArticle.getId()); + int clickNum = addArticleReadNum(ip, blogArticle.getId()); + if (clickNum > 0) { + blogArticle.setReadNumber(clickNum); + } //上一篇 PageHelper.startPage(1, 1); @@ -157,7 +160,7 @@ public class BlogArticleServiceImpl extends BaseServiceImpl * @param ip 访问者ip * @param articleId 文章id */ - private void addArticleReadNum(String ip, String articleId) { + private int addArticleReadNum(String ip, String articleId) { String str = ip + "_" + articleId; if (!StringUtils.isBlank(str)) { if (StringUtils.isEmpty(redisService.get(str))) { @@ -165,7 +168,9 @@ public class BlogArticleServiceImpl extends BaseServiceImpl BlogArticle article = selectByPrimaryKey(articleId); article.setReadNumber(article.getReadNumber() + 1); updateByPrimaryKey(article); + return article.getReadNumber(); } } + return -1; } } diff --git a/len-blog/src/main/resources/mapper/BlogArticleMapper.xml b/len-blog/src/main/resources/mapper/BlogArticleMapper.xml index 6ed8654..dfbc54b 100644 --- a/len-blog/src/main/resources/mapper/BlogArticleMapper.xml +++ b/len-blog/src/main/resources/mapper/BlogArticleMapper.xml @@ -65,6 +65,7 @@ bat.tag_id=bt.id where bt.tag_code=#{tagCode} + order by ba.top_num desc,ba.create_date desc