博客中的文章归档是如何实现的

我的效果图如下(这个是我实现的):

wordpress的效果图如下:

sql代码:

1
SELECT DISTINCT YEAR(post.post_modified)AS `year`,MONTH(post.post_modified) AS `month` FROM wp_posts AS post

xml代码:

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
<?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="com.blog.springboot.dao.PostsDao">

<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.blog.springboot.entity.Posts">
<id column="ID" property="id" />
<result column="post_author" property="postAuthor" />
<result column="post_date" property="postDate" />
<result column="post_date_gmt" property="postDateGmt" />
<result column="post_content" property="postContent" />
<result column="post_title" property="postTitle" />
<result column="post_excerpt" property="postExcerpt" />
<result column="post_status" property="postStatus" />
<result column="comment_status" property="commentStatus" />
<result column="ping_status" property="pingStatus" />
<result column="post_password" property="postPassword" />
<result column="post_name" property="postName" />
<result column="to_ping" property="toPing" />
<result column="pinged" property="pinged" />
<result column="post_modified" property="postModified" />
<result column="post_modified_gmt" property="postModifiedGmt" />
<result column="post_content_filtered" property="postContentFiltered" />
<result column="post_parent" property="postParent" />
<result column="guid" property="guid" />
<result column="menu_order" property="menuOrder" />
<result column="post_type" property="postType" />
<result column="post_mime_type" property="postMimeType" />
<result column="comment_count" property="commentCount" />
<result column="year" property="year"/>
<result column="month" property="month"/>
</resultMap>

<!-- 通用查询结果列 -->
<sql id="Base_Column_List" >
ID AS id, post_author AS postAuthor, post_date AS postDate,
post_date_gmt AS postDateGmt, post_content AS postContent, post_title
AS postTitle, post_excerpt AS postExcerpt, post_status AS postStatus,
comment_status AS commentStatus, ping_status AS pingStatus,
post_password AS postPassword, post_name AS postName, to_ping AS
toPing, pinged, post_modified AS postModified, post_modified_gmt AS
postModifiedGmt, post_content_filtered AS postContentFiltered,
post_parent AS postParent, guid, menu_order AS menuOrder, post_type AS
postType, post_mime_type AS postMimeType, comment_count AS
commentCount
</sql>


<!-- 文章归档 -->
<select id="articleArchive" resultMap="BaseResultMap">
SELECT DISTINCT YEAR(post.post_modified)AS `year`,MONTH(post.post_modified) AS `month` FROM wp_posts AS post
</select>


</mapper>

实体:

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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package com.blog.springboot.entity;

import java.io.Serializable;
import java.util.List;

import com.baomidou.mybatisplus.activerecord.Model;
import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
import com.baomidou.mybatisplus.enums.IdType;


@TableName("wp_posts")
public class Posts extends Model<Posts> {

private static final long serialVersionUID = 1L;

@TableId(value = "ID", type = IdType.AUTO)
private Integer id;
@TableField("post_author")
private Long postAuthor;
@TableField("post_date")
private String postDate;
@TableField("post_date_gmt")
private String postDateGmt;
@TableField("post_content")
private String postContent;
@TableField("post_title")
private String postTitle;
@TableField("post_excerpt")
private String postExcerpt;
@TableField("post_status")
private String postStatus;
@TableField("comment_status")
private String commentStatus;
@TableField("ping_status")
private String pingStatus;
@TableField("post_password")
private String postPassword;
@TableField("post_name")
private String postName;
@TableField("to_ping")
private String toPing;
private String pinged;
@TableField("post_modified")
private String postModified;
@TableField("post_modified_gmt")
private String postModifiedGmt;
@TableField("post_content_filtered")
private String postContentFiltered;
@TableField("post_parent")
private Long postParent;
private String guid;
@TableField("menu_order")
private Integer menuOrder;
@TableField("post_type")
private String postType;
@TableField("post_mime_type")
private String postMimeType;
@TableField("comment_count")
private Integer commentCount;

@TableField(exist=false)
private List<TermTaxonomy> termTaxonomy;

@TableField(exist=false)
private List<Terms> terms;

@TableField(exist=false)
private List<Users> users;

@TableField("year")
private String year;

@TableField("month")
private String month;




public String getMonth() {
return month;
}

public void setMonth(String month) {
this.month = month;
}

public String getYear() {
return year;
}

public void setYear(String year) {
this.year = year;
}



public List<Users> getUsers() {
return users;
}

public void setUsers(List<Users> users) {
this.users = users;
}

public List<TermTaxonomy> getTermTaxonomy() {
return termTaxonomy;
}

public void setTermTaxonomy(List<TermTaxonomy> termTaxonomy) {
this.termTaxonomy = termTaxonomy;
}

public List<Terms> getTerms() {
return terms;
}

public void setTerms(List<Terms> terms) {
this.terms = terms;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public Long getPostAuthor() {
return postAuthor;
}

public void setPostAuthor(Long postAuthor) {
this.postAuthor = postAuthor;
}

public String getPostDate() {
return postDate;
}

public void setPostDate(String postDate) {
this.postDate = postDate;
}

public String getPostDateGmt() {
return postDateGmt;
}

public void setPostDateGmt(String postDateGmt) {
this.postDateGmt = postDateGmt;
}

public String getPostContent() {
return postContent;
}

public void setPostContent(String postContent) {
this.postContent = postContent;
}

public String getPostTitle() {
return postTitle;
}

public void setPostTitle(String postTitle) {
this.postTitle = postTitle;
}

public String getPostExcerpt() {
return postExcerpt;
}

public void setPostExcerpt(String postExcerpt) {
this.postExcerpt = postExcerpt;
}

public String getPostStatus() {
return postStatus;
}

public void setPostStatus(String postStatus) {
this.postStatus = postStatus;
}

public String getCommentStatus() {
return commentStatus;
}

public void setCommentStatus(String commentStatus) {
this.commentStatus = commentStatus;
}

public String getPingStatus() {
return pingStatus;
}

public void setPingStatus(String pingStatus) {
this.pingStatus = pingStatus;
}

public String getPostPassword() {
return postPassword;
}

public void setPostPassword(String postPassword) {
this.postPassword = postPassword;
}

public String getPostName() {
return postName;
}

public void setPostName(String postName) {
this.postName = postName;
}

public String getToPing() {
return toPing;
}

public void setToPing(String toPing) {
this.toPing = toPing;
}

public String getPinged() {
return pinged;
}

public void setPinged(String pinged) {
this.pinged = pinged;
}

public String getPostModified() {
return postModified;
}

public void setPostModified(String postModified) {
this.postModified = postModified;
}

public String getPostModifiedGmt() {
return postModifiedGmt;
}

public void setPostModifiedGmt(String postModifiedGmt) {
this.postModifiedGmt = postModifiedGmt;
}

public String getPostContentFiltered() {
return postContentFiltered;
}

public void setPostContentFiltered(String postContentFiltered) {
this.postContentFiltered = postContentFiltered;
}

public Long getPostParent() {
return postParent;
}

public void setPostParent(Long postParent) {
this.postParent = postParent;
}

public String getGuid() {
return guid;
}

public void setGuid(String guid) {
this.guid = guid;
}

public Integer getMenuOrder() {
return menuOrder;
}

public void setMenuOrder(Integer menuOrder) {
this.menuOrder = menuOrder;
}

public String getPostType() {
return postType;
}

public void setPostType(String postType) {
this.postType = postType;
}

public String getPostMimeType() {
return postMimeType;
}

public void setPostMimeType(String postMimeType) {
this.postMimeType = postMimeType;
}

public Integer getCommentCount() {
return commentCount;
}

public void setCommentCount(Integer commentCount) {
this.commentCount = commentCount;
}

@Override
protected Serializable pkVal() {
return this.id;
}

@Override
public String toString() {
return "Posts{" +
", id=" + id +
", postAuthor=" + postAuthor +
", postDate=" + postDate +
", postDateGmt=" + postDateGmt +
", postContent=" + postContent +
", postTitle=" + postTitle +
", postExcerpt=" + postExcerpt +
", postStatus=" + postStatus +
", commentStatus=" + commentStatus +
", pingStatus=" + pingStatus +
", postPassword=" + postPassword +
", postName=" + postName +
", toPing=" + toPing +
", pinged=" + pinged +
", postModified=" + postModified +
", postModifiedGmt=" + postModifiedGmt +
", postContentFiltered=" + postContentFiltered +
", postParent=" + postParent +
", guid=" + guid +
", menuOrder=" + menuOrder +
", postType=" + postType +
", postMimeType=" + postMimeType +
", commentCount=" + commentCount +
"}";
}
}

dao代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.blog.springboot.dao;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.blog.springboot.entity.Posts;


public interface PostsDao extends BaseMapper<Posts> {
//文章归档
public List<Posts> articleArchive();


}

service代码:

1
2
3
4
public interface PostsService extends IService<Posts> {
//文章归档
public List<Posts> articleArchive();
}

实现类:

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
package com.blog.springboot.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.blog.springboot.dao.PostsDao;
import com.blog.springboot.entity.Posts;
import com.blog.springboot.service.PostsService;



@Service
public class PostsServiceImpl extends ServiceImpl<PostsDao, Posts> implements PostsService {

@Autowired
private PostsDao postDao;


@Override
public List<Posts> articleArchive() {

return postDao.articleArchive();
}



}

后台Controller:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* 文章归档
* @return
*/
@GetMapping(value="/articleArchive")
public JSONObject writePost() {

List<Posts> post = postService.articleArchive();

if(!post.isEmpty()) {

json.put("code", "000000");
json.put("msg", "获取文章归档");
json.put("post", post);
}else {

json.put("code", "222222");
json.put("msg", "暂无文章归档");
}
return json;
}

前端js实现:

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
//文章归档
function articleArchive(){

$.ajax({
url: Blog.url.api.articleArchive,
type: "GET",
dataType: 'json',
success: function(data) {

var rows = "";
$.each(data.post, function(index, post) {
var year = post.year+"年";
var month = post.month+"月";
var date = year + month;
rows= rows +"";
rows= rows +"<li><a href='#'>"+date+"</a></li>";
rows= rows +"<hr/>";
});

$("#articleArchive").html(rows);

},
error: function(XMLHttpRequest, textStatus, errorThrown) {

console.log(XMLHttpRequest.status);

console.log(XMLHttpRequest.readyState);

console.log(textStatus);
}
});
}

文章目录