spring data jpa 分页查询怎么实现
展开全部
private long userId;
private String content;
private String image;
private String smallImage;
private int supportCount;
private int unSupportCount;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "yuLu")
private Set<YuLuComment> items = new HashSet<YuLuComment>();
}
@Entity
@Table(name="TB_JSJGlobal_YuLuComment")
public class YuLuComment {
@Id
@GeneratedValue
private long id ;
private long userId ; // 哪个用户发表的语录
private String comment ;
private String userName ; // 评论用户的名字
// optional=true:可选,表示此对象可以没有,可以为null;false表示必须存在
@ManyToOne(cascade = { CascadeType.REFRESH, CascadeType.MERGE }, optional = true)
@JoinColumn(name = "yulu_id")
private YuLu yuLu;
}
// 对语录评论进行保存
@Override
public Page<YuLuComment> findYuLuComment(long yulu_id, int page,
int pageSize) {
// 设置查询相关
Sort sort = new Sort(Direction.DESC, "id");
Pageable pageable = new PageRequest(page, pageSize, sort);
Specification<YuLuComment> spec = new UpcomingConferences(yulu_id);
return yuLuCommentDao.findAll(spec, pageable);
}
// 创建评论的分页条件
private class UpcomingConferences implements Specification<YuLuComment> {
private long yulu_id;
public UpcomingConferences(long yulu_id) {
this.yulu_id = yulu_id;
}
@Override
public Predicate toPredicate(Root<YuLuComment> root,
CriteriaQuery<?> query, CriteriaBuilder cb) {
//左连接
Join<YuLuComment,YuLu> depJoin = root.join(root.getModel().getSingularAttribute("yuLu",YuLu.class),JoinType.LEFT ) ;
Predicate p = cb.equal(depJoin.get("id").as(Long.class), yulu_id);
return p;
}
}
2014年9月01日 12:41
endual
1
0 0 9
private String content;
private String image;
private String smallImage;
private int supportCount;
private int unSupportCount;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "yuLu")
private Set<YuLuComment> items = new HashSet<YuLuComment>();
}
@Entity
@Table(name="TB_JSJGlobal_YuLuComment")
public class YuLuComment {
@Id
@GeneratedValue
private long id ;
private long userId ; // 哪个用户发表的语录
private String comment ;
private String userName ; // 评论用户的名字
// optional=true:可选,表示此对象可以没有,可以为null;false表示必须存在
@ManyToOne(cascade = { CascadeType.REFRESH, CascadeType.MERGE }, optional = true)
@JoinColumn(name = "yulu_id")
private YuLu yuLu;
}
// 对语录评论进行保存
@Override
public Page<YuLuComment> findYuLuComment(long yulu_id, int page,
int pageSize) {
// 设置查询相关
Sort sort = new Sort(Direction.DESC, "id");
Pageable pageable = new PageRequest(page, pageSize, sort);
Specification<YuLuComment> spec = new UpcomingConferences(yulu_id);
return yuLuCommentDao.findAll(spec, pageable);
}
// 创建评论的分页条件
private class UpcomingConferences implements Specification<YuLuComment> {
private long yulu_id;
public UpcomingConferences(long yulu_id) {
this.yulu_id = yulu_id;
}
@Override
public Predicate toPredicate(Root<YuLuComment> root,
CriteriaQuery<?> query, CriteriaBuilder cb) {
//左连接
Join<YuLuComment,YuLu> depJoin = root.join(root.getModel().getSingularAttribute("yuLu",YuLu.class),JoinType.LEFT ) ;
Predicate p = cb.equal(depJoin.get("id").as(Long.class), yulu_id);
return p;
}
}
2014年9月01日 12:41
endual
1
0 0 9
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询