Hibernate中Criteria的完整用法
Hibernate - DetachedCriteria 的完整用法
Hibernate Projections
统计
![image.png](https://upload-images.jianshu.io/upload_images/4994935-cd026cb7a3490dc6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
public SoftCollect findCollect(Integer softwareId){
Criteria criteria = createCriteria();
ProjectionList projections = Projections.projectionList();
projections.add(Projections.alias(
Projections.sum("duration"), "totalDuration"));
projections.add(Projections.alias(
Projections.countDistinct("memberId"), "userVolume"));
criteria.setProjection(projections);
criteria.setResultTransformer(Transformers.aliasToBean(SoftCollect.class));
criteria.add(Restrictions.eq("softwareId", softwareId));
return (SoftCollect) criteria.uniqueResult();
}
/*public class SoftCollect implements Serializable {
private static final long serialVersionUID = 1L;
private int totalDuration;
private int userVolume;
*/
}
获取一个数据
![image.png](https://upload-images.jianshu.io/upload_images/4994935-af53a3861e3b64ff.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
测试用例
package com.wanggs.dao.market;
/**
* SoftwareDao
*/
@Repository
@SuppressWarnings("unchecked")
public class SoftweDao extends BasicDao {
private static final long serialVersionUID = 1L;
public Pagination findPage(Softwe search, Integer userId, int curPage, int pageSize) {
Criteria criteria = createCriteria();
if (search != null) {
if (StringUtils.isNotBlank(search.getSname())) {
criteria.add(Restrictions.like("sname", search.getSname(), MatchMode.ANYWHERE));
}
if (StringUtils.isNotBlank(search.getSncode())) {
criteria.add(Restrictions.eq("sncode", search.getSncode()));
}
if(search.getOneCategoryId() != null){
criteria.add(Restrictions.eq("oneCategoryId", search.getOneCategoryId()));
}
}
if (userId != null) {
criteria.createCriteria("userIds")
.add(Restrictions.sqlRestriction("{alias}.user_id=?", userId, IntegerType.INSTANCE));
}
criteria.addOrder(Order.desc("id"));
return findPage(criteria, curPage, pageSize);
}
public Pagination removeGames(Softwe search, Integer userId, int curPage, int pageSize) {
Criteria criteria = createCriteria();
if (search != null) {
if (StringUtils.isNotBlank(search.getSname())) {
criteria.add(Restrictions.like("sname", search.getSname(), MatchMode.ANYWHERE));
}
if (StringUtils.isNotBlank(search.getSncode())) {
criteria.add(Restrictions.eq("sncode", search.getSncode()));
}
if(search.getOneCategoryId() != null){
criteria.add(Restrictions.ne("oneCategoryId", search.getOneCategoryId()));
}
}
if (userId != null) {
criteria.createCriteria("userIds")
.add(Restrictions.sqlRestriction("{alias}.user_id=?", userId, IntegerType.INSTANCE));
}
criteria.addOrder(Order.desc("id"));
return findPage(criteria, curPage, pageSize);
}
public List findByCategory(Integer oneCategoryId, Integer twoCategoryId, PlatType platType) {
Criteria criteria = createCriteria();
if (oneCategoryId != null) {
criteria.add(Restrictions.eq("oneCategoryId", oneCategoryId));
}
if (twoCategoryId != null) {
criteria.add(Restrictions.eq("twoCategoryId", twoCategoryId));
}
if (platType != null && platType == PlatType.PRESENT) {
criteria.add(Restrictions.eq("platType", platType));
}
criteria.add(Restrictions.ne("dtype", AppType.SYSTEM));
ProjectionList projections = Projections.projectionList();
projections.add(Projections.id().as("id"));
projections.add(Projections.property("publishTime").as("publishTime"));
criteria.setProjection(projections);
criteria.add(Restrictions.eq("statu", PublishStatu.ISSUE));
criteria.setResultTransformer(Transformers.aliasToBean(Softwe.class));
return criteria.list();
}
public List findNewerSoftwe(Softwe search) {
if (search != null && search.getPlatType() != null) {
Criteria criteria = createCriteria();
if (search.getPlatType() != null && search.getPlatType() == PlatType.PRESENT) {
criteria.add(Restrictions.eq("platType", search.getPlatType()));
}
criteria.add(Restrictions.ne("dtype", AppType.SYSTEM));
ProjectionList projections = Projections.projectionList();
projections.add(Projections.id().as("id"));
projections.add(Projections.property("publishTime").as("publishTime"));
criteria.setProjection(projections);
criteria.add(Restrictions.eq("statu", PublishStatu.ISSUE));
criteria.addOrder(Order.desc("updatedAt"));
criteria.setMaxResults(8);
criteria.setResultTransformer(Transformers.aliasToBean(Softwe.class));
return criteria.list();
}
return null;
}
public List findRankSoftwe(Softwe search) {
if (search != null && search.getPlatType() != null) {
Criteria criteria = createCriteria();
if (search.getPlatType() != null && search.getPlatType() == PlatType.PRESENT) {
criteria.add(Restrictions.eq("platType", search.getPlatType()));
}
criteria.add(Restrictions.ne("dtype", AppType.SYSTEM));
ProjectionList projections = Projections.projectionList();
projections.add(Projections.id().as("id"));
projections.add(Projections.property("publishTime").as("publishTime"));
criteria.setProjection(projections);
criteria.add(Restrictions.eq("statu", PublishStatu.ISSUE));
criteria.addOrder(Order.desc("dloadVolume"));
criteria.setMaxResults(8);
criteria.setResultTransformer(Transformers.aliasToBean(Softwe.class));
return criteria.list();
}
return null;
}
public List findHotterSoftwe(Softwe search) {
if (search != null && search.getPlatType() != null) {
Criteria criteria = createCriteria();
if (search.getPlatType() != null && search.getPlatType() == PlatType.PRESENT) {
criteria.add(Restrictions.eq("platType", search.getPlatType()));
}
criteria.add(Restrictions.ne("dtype", AppType.SYSTEM));
ProjectionList projections = Projections.projectionList();
projections.add(Projections.id().as("id"));
projections.add(Projections.property("publishTime").as("publishTime"));
criteria.setProjection(projections);
criteria.add(Restrictions.eq("statu", PublishStatu.ISSUE));
criteria.setMaxResults(8);
criteria.setResultTransformer(Transformers.aliasToBean(Softwe.class));
return criteria.list();
}
return null;
}
public int findCount(String packName) {
Criteria criteria = createCriteria();
criteria.add(Restrictions.eq("packName", packName));
criteria.setProjection(Projections.rowCount());
return ((Long) criteria.uniqueResult()).intValue();
}
public Softwe find(String packName) {
Criteria criteria = createCriteria();
criteria.add(Restrictions.eq("packName", packName));
return (Softwe) criteria.uniqueResult();
}
public Integer findId(String packName) {
Criteria criteria = createCriteria();
criteria.setProjection(Projections.id());
criteria.add(Restrictions.eq("packName", packName));
return (Integer) criteria.uniqueResult();
}
public List findByPackName(String packName) {
Criteria criteria = createCriteria();
criteria.add(Restrictions.eq("packName", packName));
return criteria.list();
}
public Softwe findCode(String sncode) {
Criteria criteria = createCriteria();
criteria.add(Restrictions.eq("sncode", sncode));
return (Softwe) criteria.uniqueResult();
}
public List findPublishSoftwe() {
Criteria criteria = createCriteria();
criteria.add(Restrictions.eq("statu", PublishStatu.ISSUE));
return criteria.list();
}
public List findByCondition(Softwe search) {
Criteria criteria = createCriteria();
if (search != null) {
if (StringUtils.isNotBlank(search.getSname())) {
criteria.add(Restrictions.like("sname", search.getSname(), MatchMode.ANYWHERE));
}
if (StringUtils.isNotBlank(search.getSncode())) {
criteria.add(Restrictions.eq("sncode", search.getSncode()));
}
}
criteria.addOrder(Order.desc("id"));
return criteria.list();
}
public List findByOneCategoryId(Integer oneCategoryId){
Criteria criteria = createCriteria();
if(oneCategoryId!=null){
criteria.add(Restrictions.eq("oneCategoryId", oneCategoryId));
}
criteria.addOrder(Order.desc("id"));
return criteria.list();
}
}