SSH动态查询具体实现之Dao

http://blog.csdn.net/qiyuexuelang/article/details/17420297

  1. package com.sxdf.manage.dao.impl;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import javax.annotation.Resource;  
  7.   
  8. import org.hibernate.Criteria;  
  9. import org.hibernate.HibernateException;  
  10. import org.hibernate.Query;  
  11. import org.hibernate.Session;  
  12. import org.hibernate.SessionFactory;  
  13. import org.springframework.stereotype.Component;  
  14. import com.sxdf.manage.dao.SnippetDao;  
  15.   
  16. @Component("snippetDao")  
  17. public class SnippetDaoImpl implements SnippetDao {  
  18.     @Resource(name = "sessionFactory")  
  19.     private SessionFactory hu;  
  20.      //统计总数(你可以使用count(*),前提是使用另一个HQL语句)  
  21.     public int count(final String hql, final Object[] param) {  
  22.   
  23.         int count = 0;  
  24.         Session session = hu.getCurrentSession();  
  25.         Query query = session.createQuery(hql);  
  26.   
  27.         if (param.length > 0) {  
  28.             for (int i = 0; i < param.length; i++) {  
  29.                 query.setString(i, param[i].toString());  
  30.             }  
  31.         }  
  32.   
  33.         List list = query.list();  
  34.   
  35.         if (list.size() > 0) {  
  36.             count = list.size();  
  37.         }  
  38.         return count;  
  39.     }  
  40.        
  41.              //查找当前页数据             
  42.         public List<?> findPublic(final String hql, final Object[] param,  
  43.             final int start, final int limit) {  
  44.         Session session = hu.getCurrentSession();  
  45.         Query query = session.createQuery(hql);  
  46.         if (param.length > 0) {  
  47.             for (int i = 0; i < param.length; i++) {  
  48.                 query.setString(i, param[i].toString());  
  49.             }  
  50.         }  
  51.   
  52.         List<?> list = query.setFirstResult(start).setMaxResults(limit).list();  
  53.         return list;  
  54.     }  
  55.              //得到后半段hql  
  56.             public String getHQL(Object[][] ob, Object[][] like  
  57.                           ,String[][] datetime  
  58.                           , String[] group,String[] asc, String[] desc) {  
  59.                 StringBuffer hql = new StringBuffer();  
  60.                 String ss = null;  
  61.                 if (null != ob) {  
  62.                         for (Object[] o : ob) {  
  63.                                  // [key][value] name='value'  
  64.                                 boolean b = ((null != o[1]) && (null != o[0])  
  65.                                                 && (!"".equals(o[1].toString()))   
  66.                                                 && (!"".equals(o[0].toString())));  
  67.                                  if (b) {  
  68.                                          hql.append("and j." + o[0] + " =? ");  
  69.                                  }  
  70.                         }  
  71.                 }  
  72.                 if (null != like) {  
  73.                         for (Object[] l : like) {  
  74.                                  // [key][value] name like '%value%'  
  75.                                 boolean bl = ((null != l[1]) && (null != l[0])  
  76.                                                  && (!"".equals(l[1].toString()))  
  77.                                                  && (!"".equals(l[0].toString())));  
  78.                                  if (bl) {  
  79.                                           hql.append("and j." + l[0] + " LIKE? ");  
  80.                                  }  
  81.                         }  
  82.                  }  
  83.                  //时间(模糊)  
  84.         if (null != datetime) {  
  85.   
  86.   
  87.             for (String[] d : datetime) {  
  88.                 // [key][value]  
  89.                 boolean bd = ((null != d[1]) && (null != d[0])  
  90.                         && (!"".equals(d[1])) && (!"".equals(d[0]  
  91.                         .toString())));  
  92.                 if (bd) {  
  93.   
  94.                     hql.append("and to_char(j."+ d[0] +",'yyyy-mm-dd')" + " LIKE ? ");  
  95.                 }  
  96.             }  
  97.   
  98.   
  99.         }  
  100.         // 分组 group by [value]  
  101.   
  102.         if (null != group && group.length > 0) {  
  103.             StringBuffer groupb = new StringBuffer();  
  104.   
  105.             for (String str1 : group) {  
  106.                 if (null != str1 && !"".equals(str1)) {  
  107.   
  108.                     groupb.append("j." + str1 + " , ");  
  109.                 }  
  110.   
  111.             }  
  112.   
  113.             if (null != groupb && "".equals("")) {  
  114.                 hql.append("group by ");  
  115.                 hql.append(groupb.substring(0, groupb.lastIndexOf(",")));  
  116.             }  
  117.   
  118.         }  
  119.         // 升序//降序(先升后降) order by [value]  
  120.         if (null != asc && asc.length > 0 || null != desc && desc.length > 0) {  
  121.   
  122.             String oy1 = null;  
  123.             String oy2 = null;  
  124.             // orderb.append("order by ");  
  125.             if (null != asc && asc.length > 0) {  
  126.                 StringBuffer orderb1 = new StringBuffer();  
  127.                 for (String str2 : asc) {  
  128.   
  129.                     if (null != str2 && !"".equals(str2)) {  
  130.                         orderb1.append("j." + str2 + " , ");  
  131.   
  132.                     }  
  133.                 }  
  134.   
  135.                 if (null != orderb1 && "".equals(orderb1)) {  
  136.                     oy1 = orderb1.substring(0, orderb1.lastIndexOf(","))  
  137.                             + " asc ";  
  138.   
  139.                 }  
  140.   
  141.             }  
  142.   
  143.             if (null != desc && desc.length > 0) {  
  144.                 StringBuffer orderb2 = new StringBuffer();  
  145.                 for (String str3 : desc) {  
  146.                     if (null != str3 && !"".equals(str3)) {  
  147.                         orderb2.append("j." + str3 + " , ");  
  148.                     }  
  149.                 }  
  150.   
  151.                 if (null != orderb2 && !"".equals(orderb2)) {  
  152.                     oy2 = orderb2.substring(0, orderb2.lastIndexOf(","))  
  153.                             + " desc ";  
  154.   
  155.                 }  
  156.             }  
  157.   
  158.             if ((null != oy2 && !"".equals(oy2))  
  159.                     || (null != oy1 && !"".equals(oy1))) {  
  160.                 hql.append("order by ");  
  161.                 if (null != oy1 && !"".equals(oy1)) {  
  162.                     hql.append(oy1);  
  163.                 }  
  164.                 if ((null != oy2 && !"".equals(oy2))  
  165.                         && (null != oy1 && !"".equals(oy1))) {  
  166.                     hql.append(" , ");  
  167.                 }  
  168.                 if (null != oy2 && !"".equals(oy2)) {  
  169.                     hql.append(oy2);  
  170.                 }  
  171.   
  172.             }  
  173.   
  174.         }  
  175.   
  176.         if (hql.toString().contains("and")) {  
  177.             ss = hql.substring(4, hql.length());  
  178.   
  179.         }  
  180.   
  181.         return ss;  
  182.     }  
  183.               //选值(=、like)  
  184.     public List<Object> getValue(Object[][] ob, Object[][] like,String[][] datetime) {  
  185.         List<Object> list = new ArrayList<Object>();  
  186.           
  187.         if (null != ob) {  
  188.   
  189.   
  190.             for (Object[] o : ob) {  
  191.                 // [key][value]  
  192.                 boolean b = ((null != o[1]) && (null != o[0])  
  193.                         && (!"".equals(o[1].toString())) && (!"".equals(o[0]  
  194.                         .toString())));  
  195.                 if (b) {  
  196.   
  197.   
  198.                     list.add(o[1]);  
  199.                 }  
  200.   
  201.   
  202.             }  
  203.         }  
  204.           
  205.         if (null != like) {  
  206.   
  207.   
  208.             for (Object[] l : like) {  
  209.                 // [key][value]  
  210.                 boolean bl = ((null != l[1]) && (null != l[0])  
  211.                         && (!"".equals(l[1].toString())) && (!"".equals(l[0]  
  212.                         .toString())));  
  213.                 if (bl) {  
  214.   
  215.   
  216.                     list.add("%" + l[1] + "%");  
  217.                 }  
  218.             }  
  219.   
  220.   
  221.         }  
  222.           
  223.         //时间(模糊)  
  224.         if (null != datetime) {  
  225.   
  226.   
  227.             for (String[] d : datetime) {  
  228.                 // [key][value]  
  229.                 boolean bd = ((null != d[1]) && (null != d[0])  
  230.                         && (!"".equals(d[1])) && (!"".equals(d[0]  
  231.                         .toString())));  
  232.                 if (bd) {  
  233.   
  234.   
  235.                     list.add("%" + d[1] + "%");  
  236.                 }  
  237.             }  
  238.   
  239.   
  240.         }  
  241.   
  242.   
  243.         return list;  
  244.     }  
  245.   
  246. }  

由于删掉一些其他无关的类方法,故可能存在 一些无用的import引入,对代码无影响
       
         总结一下:本文大量使用if-else的运用,大大降低阅读效果。 如果大家有好的方法和建议,洗耳恭听。
         对sessionFactory 使用注解,
         其中本类所继承的SnippetDao为上一篇中特意介绍的Dao接口方法,详细说明请参考上文-- SSH动态查询具体实现之service
            Action 调取示例:

         首先引入SnippetService

  1. @Resource(name = "snippetServiceImpl")  
  2.     private SnippetService ss;  

         接下来,在action方法利用本接口实现你想要的查询
  1. Object[][] aa1 =null;  
  2.    Object[][] aa2 =null;  
  3.    String[] group =null;  
  4.    String[] asc =null;  
  5.    String[] desc = null;;  
  6. ActionWriteUtil.writeStr(JsonUtil.getPurchaseJson((List<Purchase>)ss.search(Purchase.class, aa1,aa2 ,null,group,asc,desc,start, limit),  
  7.                                  ss.count(Purchase.class, aa1,aa2,null,group,asc,desc)));  

          针对任何bean,无需在其service或dao层再写查询方法(对bean属性*查询,   比较、平均值等特殊查询除外)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值