个人博客项目文章归档实现

网友投稿 569 2022-05-29

目录

文章目录

目录

VO类:全部采用注解,注意因为我数据库表名为article,所以不需要写@Table注解,表名为其它的话,就需要自己添加@Table注解了

package net.myblog.entity; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Temporal; import javax.persistence.TemporalType; /** * 博客系统文章信息的实体类 * @author Nicky */ @Entity public class Article { /** 文章Id,自增**/ private int articleId; /** 文章名称**/ private String articleName; /** 文章发布时间**/ private Date articleTime; /** 图片路径,测试**/ private String imgPath; /** 文章内容**/ private String articleContent; /** 查看人数**/ private int articleClick; /** 是否博主推荐。0为否;1为是**/ private int articleSupport; /** 是否置顶。0为;1为是**/ private int articleUp; /** 文章类别。0为私有,1为公开,2为仅好友查看**/ private int articleType; @GeneratedValue @Id public int getArticleId() { return articleId; } public void setArticleId(int articleId) { this.articleId = articleId; } @Column(length=100, nullable=false) public String getArticleName() { return articleName; } public void setArticleName(String articleName) { this.articleName = articleName; } @Temporal(TemporalType.DATE) @Column(nullable=false, updatable=false) public Date getArticleTime() { return articleTime; } public void setArticleTime(Date articleTime) { this.articleTime = articleTime; } @Column(length=100) public String getImgPath() { return imgPath; } public void setImgPath(String imgPath) { this.imgPath = imgPath; } @Column(nullable=false) public String getArticleContent() { return articleContent; } public void setArticleContent(String articleContent) { this.articleContent = articleContent; } public int getArticleClick() { return articleClick; } public void setArticleClick(int articleClick) { this.articleClick = articleClick; } public int getArticleSupport() { return articleSupport; } public void setArticleSupport(int articleSupport) { this.articleSupport = articleSupport; } public int getArticleUp() { return articleUp; } public void setArticleUp(int articleUp) { this.articleUp = articleUp; } @Column(nullable=false) public int getArticleType() { return articleType; } public void setArticleType(int articleType) { this.articleType = articleType; } }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

###代码实现步骤

文章表里有很多数据,要按照年月获取文章进行归档的话,我们可以使用如下SQL对数据进行分组

SELECT YEAR(articleTime) AS 'year',MONTH(articleTime) AS 'month',COUNT(*) AS 'count' FROM article GROUP BY YEAR(articleTime) DESC,MONTH(articleTime);

1

2

然后编写数据库层的Repository类,类实现Spring Data JPA提供的接口

package net.myblog.repository; import java.util.Date; import java.util.List; import net.myblog.entity.Article; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.repository.query.Param; public interface ArticleRepository extends PagingAndSortingRepository{ /** * 文章归档信息获取 * @return */ @Query(value="select year(a.articleTime) as year,month(a.articleTime) as month," + "count(a) as count from Article a group by year(a.articleTime),month(a.articleTime)", countQuery="select count(1) from (select count(1) from Article a group by year(a.articleTime),month(a.articleTime))") public List findArticleGroupByTime(); }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

然后在Service类,用@Autowired注解调用

package net.myblog.service; import java.util.Date; import java.util.List; import net.myblog.entity.Article; import net.myblog.repository.ArticleRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort.Direction; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service public class ArticleService { @Autowired ArticleRepository articleRepository; /** * 文章归档信息获取 * @return */ @Transactional public List findArticleGroupByTime(){ return articleRepository.findArticleGroupByTime(); } }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

然后在Controller里调用,用的是SpringMVC框架

package net.myblog.web.controller; import java.util.ArrayList; import java.util.List; import java.util.Vector; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.myblog.service.ArticleService; import net.sf.json.JSONArray; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.Sort.Direction; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller public class BlogIndexController extends BaseController{ @Autowired ArticleService articleService; /** * 访问博客主页 * @return */ @RequestMapping(value="/toblog",produces="text/html;charset=UTF-8") public ModelAndView toBlog(HttpServletRequest request, HttpServletResponse response, Model model)throws ClassNotFoundException{ //获取归档文章信息 List archiveArticles = articleService.findArticleGroupByTime(); model.addAttribute("archiveArticles", archiveArticles); mv.setViewName("myblog/frame/index"); return mv; } }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

个人博客项目之文章归档实现

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

在JSP页面调用显示:

文章归档

1

2

3

4

5

6

7

8

9

10

11

12

13

14

效果如图所示:

###文档归档信息查询

然后介绍点击文档归档信息后,获取文章信息的实现,其实也就是按年月查询文档信息

在Repository类里添加方法:

/** * 按月份获取文章信息 * @param month * 月份数 * @return */ @Query("from Article a where date_format(a.articleTime,'%Y%m')=date_format((:yearmonth),'%Y%m') " + "order by articleTime desc") public List

findArticleByMonth(@Param("yearmonth")Date yearmonth);

1

2

3

4

5

6

7

8

9

10

Service类里调用:

/** * 按月份获取文章信息 * @param month * @return */ @Transactional public List

findArticleByMonth(Date month){ return articleRepository.findArticleByMonth(month); }

1

2

3

4

5

6

7

8

9

在JSP页面写入,getArchiveArticles.do就是要访问的url,传入yearmonth参数就可以

文章归档

1

2

3

4

5

6

7

8

9

10

11

12

13

14

在Controller类里进行处理:

@RequestMapping("/getArchiveArticles") public ModelAndView getArticleByMonth(HttpServletRequest request){ String yearMonthString = request.getParameter("yearmonth"); System.out.println("month:"+yearMonthString); ModelAndView mv = this.getModelAndView(); Date yearmonth = DateUtils.parse("yyyy-MM", yearMonthString); List

articles = articleService.findArticleByMonth(yearmonth); mv.addObject("articles", articles); mv.setViewName("myblog/article/archive_articles"); return mv; }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

###附录(工具类、公共类代码)

DateUtils.java、BaseController.java类

DateUtil.java

package net.myblog.utils; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import net.myblog.core.Constants; public class DateUtils { public static String formatDate(Date date) throws ParseException{ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return dateFormat.format(date); } /** * 解析日期,注:此处为严格模式解析,即20151809这样的日期会解析错误 * * @param pattern * @param date * @return */ public static Date parse(String pattern, String date){ return parse(pattern, date, Constants.LOCALE_CHINA); } /** * 解析日期,注:此处为严格模式解析,即20151809这样的日期会解析错误 * * @param pattern * @param date * @param locale * @return */ public static Date parse(String pattern, String date, Locale locale){ SimpleDateFormat format = new SimpleDateFormat(pattern, locale); format.setLenient(false); Date result = null; try{ result = format.parse(date); }catch(Exception e){ e.printStackTrace(); } return result; } }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

BaseController.java:

package net.myblog.web.controller; import javax.servlet.http.HttpServletRequest; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.servlet.ModelAndView; public class BaseController { /** * 得到request对象 */ public HttpServletRequest getRequest() { HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest(); return request; } /** * 得到ModelAndView */ public ModelAndView getModelAndView(){ return new ModelAndView(); } }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

JSP

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:jQuery概述
下一篇:CAS
相关文章