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
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;
}
代码实现步骤
文章表里有很多数据,要按照年月获取文章进行归档的话,我们可以使用如下SQL对数据进行分组
SELECT YEAR(articleTime) AS 'year',MONTH(articleTime) AS 'month',COUNT(*) AS 'count' FROM article
GROUP BY YEAR(articleTime) DESC,MONTH(articleTime);
然后编写数据库层的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<Article,Integer>{
* 文章归档信息获取
* @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<Object[]> findArticleGroupByTime();
}