Hibernate动态条件查询并分页(一)

应用实例截图:

前台提交数据,用户可以有选择的进行填写

好吧~ 还是直接上代码吧.....

1.用户接收前台页面提交的数据DTO,该类用户接收商品Commodity的信息

[java]  view plain  copy
  1. public class CommodityDTO {  
  2.       
  3.     private Integer categoryId; // 一级分类id  
  4.     private Integer subCategoryId; // 二级分类id  
  5.     private String name;    // 商品名称  
  6.     private Float fromPrice; // 起始价格  
  7.     private Float toPrice;  
  8.     private Integer fromSalesVolume; // 起始销售量  
  9.     private Integer toSalesVolume;  
  10.       
  11.     // 此处省略set/get方法  
  12. }     

2.DAOImpl层

[java]  view plain  copy
  1. @Override  
  2.     public List<Commodity> queryByRestrictions(CommodityDTO commodityDTO, int pageOffset, int pageSize) {  
  3.         Session session = this.getSession();  
  4.         DetachedCriteria dc = myDetachedCriteria(commodityDTO);  
  5.   
  6.         List<Commodity> commodities = dc.getExecutableCriteria(session)  
  7.                                 .setFirstResult(pageOffset).setMaxResults(pageSize).list();  
  8.         return commodities;  
  9.     }  



[java]  view plain  copy
  1.      /** 
  2.  * 添加动态条件 
  3.  *  
  4.  * @param commodityDTO 
  5.  * @return 
  6.  */  
  7. private DetachedCriteria myDetachedCriteria(CommodityDTO commodityDTO) {  
  8.   
  9.     DetachedCriteria dc = DetachedCriteria.forClass(Commodity.class);  
  10.   
  11.     Integer categoryId = commodityDTO.getCategoryId();  
  12.     Integer subCategoryId = commodityDTO.getSubCategoryId();  
  13.     String name = commodityDTO.getName();  
  14.     Float fromPrice = commodityDTO.getFromPrice();  
  15.     Float toPrice = commodityDTO.getToPrice();  
  16.     Integer fromSalesVolume = commodityDTO.getFromSalesVolume();  
  17.     Integer toSalesVolume = commodityDTO.getToSalesVolume();  
  18.   
  19.     if (categoryId != null && categoryId.intValue() > 0) {  
  20.         dc.add(Restrictions.eq("category.id", categoryId));  
  21.     }  
  22.     if (subCategoryId != null && subCategoryId.intValue() > 0) {  
  23.         dc.add(Restrictions.eq("subCategory.id", subCategoryId));  
  24.     }  
  25.   
  26.     if (name != null && !"".equals(name.trim())) {  
  27.         dc.add(Restrictions.eq("name", name));  
  28.     }  
  29.   
  30.     if (fromPrice != null && fromPrice.intValue() > 0) {  
  31.         dc.add(Restrictions.ge("price", fromPrice));  
  32.     }  
  33.     if (toPrice != null && toPrice.intValue() > 0) {  
  34.         dc.add(Restrictions.le("price", toPrice));  
  35.     }  
  36.   
  37.     if (fromSalesVolume != null && fromSalesVolume.intValue() > 0) {  
  38.         dc.add(Restrictions.ge("salesVolume", fromSalesVolume));  
  39.     }  
  40.     if (toSalesVolume != null && toSalesVolume.intValue() > 0) {  
  41.         dc.add(Restrictions.le("salesVolume", toSalesVolume));  
  42.     }  
  43.     return dc;  
  44. }  
3.Service层就是将DAOImpl层的数据封装成一个Pager类供Controller使用

猜你喜欢

转载自blog.csdn.net/weixin_42124622/article/details/80458476
今日推荐