Hibernate dao 层

IDao_Hql实现类IDao_HqlImp:java

  
  
           
  
  
  1. package com.boxun.crm.dao.impl;     
  2. import java.io.Serializable;   
  3. import java.util.List;     
  4. import org.hibernate.Criteria;   
  5. import org.hibernate.HibernateException;   
  6. import org.hibernate.Query;   
  7. import org.hibernate.Session;   
  8. import org.hibernate.Transaction;   
  9. import org.hibernate.criterion.Example;   
  10. import org.hibernate.criterion.MatchMode;   
  11. import org.hibernate.criterion.Order;   
  12. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;     
  13. import com.boxun.crm.dao.IDao_Hql;    
  14. /**  
  15.  * <li>经过Hibernate对持久层操做接口实现类</li>  
  16.  * <li>提供对数据库的增、删、改、查等一系列操做。</li>  
  17.  *   
  18.  * @author 旦旦而学   
  19.  * @version 1.0  
  20.  * @since jdk5.0  
  21.  */   
  22. @SuppressWarnings("unchecked")   
  23. public class Dao_HqlImp extends HibernateDaoSupport implements IDao_Hql {   
  24.     // 声明事务对象   
  25.     private Transaction tran = null;   
  26.     // 声明查询对象   
  27.     private Query query = null;   
  28.     /*  
  29.      * (non-Javadoc)  
  30.      *   
  31.      * @see com.boxun.crm.dao.IDao#beginTx()  
  32.      */   
  33.     public boolean beginTx() throws HibernateException {   
  34.         if (tran == null)   
  35.             tran = this.getSession().beginTransaction();   
  36.         return true;   
  37.     }   
  38.     /*  
  39.      * (non-Javadoc)  
  40.      *   
  41.      * @see com.boxun.crm.dao.IDao#commitTx()  
  42.      */   
  43.     public boolean commitTx() throws HibernateException {   
  44.         if (tran != null)   
  45.             tran.commit();   
  46.         tran = null;   
  47.         return true;   
  48.     }   
  49.     /*  
  50.      * (non-Javadoc)  
  51.      *   
  52.      * @see com.boxun.crm.dao.IDao#rollbackTx()  
  53.      */   
  54.     public boolean rollbackTx() throws HibernateException {   
  55.         tran.rollback();   
  56.         tran = null;   
  57.         return true;   
  58.     }   
  59.     /*  
  60.      * (non-Javadoc)  
  61.      *   
  62.      * @see com.boxun.crm.dao.IDao_Hql#del(java.lang.Object)  
  63.      */   
  64.     public boolean del(Object obj) throws HibernateException {   
  65.         this.getSession().delete(obj);   
  66.         return true;   
  67.     }   
  68.     /*  
  69.      * (non-Javadoc)  
  70.      *   
  71.      * @see com.boxun.crm.dao.IDao#delOrUpdate(java.lang.String,  
  72.      *      java.util.ArrayList)  
  73.      */   
  74.     public boolean delOrUpdate(String hql, List params)   
  75.             throws HibernateException {   
  76.         if (null == hql || hql.equals("")) {   
  77.             return false;   
  78.         }   
  79.         query = this.getSession().createQuery(hql);   
  80.         if (null != params && params.size() > 0) {   
  81.             for (int i = 0; i < params.size(); i++) {   
  82.                 query.setParameter(i, params.get(i));   
  83.             }   
  84.         }   
  85.         int result = query.executeUpdate();   
  86.    
  87.         return result > 0 ? true : false;   
  88.     }   
  89.     /*  
  90.      * (non-Javadoc)  
  91.      *   
  92.      * @see com.boxun.crm.dao.IDao#find(java.lang.String)  
  93.      */   
  94.     public List<Object> find(String hql) throws HibernateException {   
  95.         if (null == hql || hql.equals("")) {   
  96.             return null;   
  97.         }   
  98.         query = this.getSession().createQuery(hql);   
  99.         return query.list();   
  100.     }   
  101.     /*  
  102.      * (non-Javadoc)  
  103.      *   
  104.      * @see com.boxun.crm.dao.IDao#find(java.lang.String, java.util.ArrayList)  
  105.      */   
  106.     public <T> List<T> find(String hql, List params) throws HibernateException {   
  107.         if (null == hql || hql.equals("")) {   
  108.             return null;   
  109.         }   
  110.         query = this.getSession().createQuery(hql);   
  111.         if (null != params && params.size() > 0) {   
  112.             // 循环给参数赋值   
  113.             for (int i = 0; i < params.size(); i++) {   
  114.                 query.setParameter(i, params.get(i));   
  115.             }   
  116.         }   
  117.         List<T> list = query.list();     
  118.         return list;   
  119.     }   
  120.     /*  
  121.      * (non-Javadoc)  
  122.      *   
  123.      * @see com.boxun.crm.dao.IDao#find(java.lang.String, int, int)  
  124.      */   
  125.     public List<Object> find(String hql, int row, int pages)   
  126.             throws HibernateException {   
  127.         if (null == hql || hql.equals("")) {   
  128.             return null;   
  129.         }   
  130.         query = this.getSession().createQuery(hql);   
  131.         if (row > 0 && pages > 0) {   
  132.             // 取得每页显示结果集   
  133.             query.setMaxResults(row);   
  134.             // 取得当前页码所要显示的结果集   
  135.             query.setFirstResult(row * (pages - 1));   
  136.         }     
  137.         return query.list();    
  138.     }   
  139.     /*  
  140.      * (non-Javadoc)  
  141.      *   
  142.      * @see com.boxun.crm.dao.IDao#find(java.lang.String, java.util.ArrayList,  
  143.      *      int, int)  
  144.      */   
  145.     public List<Object> find(String hql, List params, int row, int pages)   
  146.             throws HibernateException {   
  147.         if (null == hql || hql.equals("")) {   
  148.             return null;   
  149.         }   
  150.         query = this.getSession().createQuery(hql);   
  151.    
  152.         if (params != null && params.size() > 0) {   
  153.             // 循环给参数赋值   
  154.             for (int i = 0; i < params.size(); i++) {   
  155.                 query.setParameter(i, params.get(i));   
  156.             }   
  157.         }   
  158.         if (row > 0 && pages > 0) {   
  159.             // 取得每页显示结果集   
  160.             query.setMaxResults(row);   
  161.             // 取得当前页码所要显示的结果集   
  162.             query.setFirstResult(row * (pages - 1));   
  163.         }   
  164.         return query.list();   
  165.     }   
  166.     /*  
  167.      * (non-Javadoc)  
  168.      *   
  169.      * @see com.boxun.crm.dao.IDao_Hql#find(java.lang.Class, java.lang.Object,  
  170.      *      java.lang.String[], int, int)  
  171.      */   
  172.     public <T> List<T> find(Class<T> c, T obj, String[] orders, int row,   
  173.             int pages) throws HibernateException {   
  174.         Criteria criteria = this.getSession().createCriteria(c);   
  175.         if (obj != null) {   
  176.             Example example = Example.create(obj);   
  177.             example.enableLike(MatchMode.ANYWHERE);// 匹配模式,使用模糊查询必填项。   
  178.             example.excludeNone();// 空的不作查询条件   
  179.             example.excludeZeroes();// 0不要查询   
  180.             example.ignoreCase(); // 不区分大小写   
  181.             criteria.add(example);   
  182.         }   
  183.         if (row > 0 && pages > 0) {   
  184.             criteria.setMaxResults(row);// 最大显示记录数   
  185.             criteria.setFirstResult((pages - 1) * row);// 从第几条开始   
  186.         }   
  187.         // 判断是否有排序请求,若是有加入到排序方法中   
  188.         if (orders != null) {   
  189.             for (int i = 0; i < orders.length; i++)   
  190.                 criteria.addOrder(Order.desc(orders[i]));   
  191.         }   
  192.         return criteria.list();   
  193.     }   
  194.     /*  
  195.      * (non-Javadoc)  
  196.      *   
  197.      * @see com.boxun.crm.dao.IDao_Hql#get(java.lang.Class,  
  198.      *      java.io.Serializable)  
  199.      */   
  200.     public <T> T get(Class<T> c, Serializable s) throws HibernateException {   
  201.         return (T) this.getSession().get(c, s);   
  202.     }   
  203.     /*  
  204.      * (non-Javadoc)  
  205.      *   
  206.      * @see com.boxun.crm.dao.IDao#listCount(java.lang.String)  
  207.      */   
  208.     public int listCount(String hql) throws HibernateException,   
  209.             NumberFormatException {   
  210.         if (null == hql || hql.equals("")) {   
  211.             return 0;   
  212.         }   
  213.         query = this.getSession().createQuery(hql);   
  214.         List list = query.list();   
  215.    
  216.         return list != null && list.size() > 0 ? Integer.parseInt(list.get(0)   
  217.                 + "") : 0;   
  218.     }   
  219.     /*  
  220.      * (non-Javadoc)  
  221.      *   
  222.      * @see com.boxun.crm.dao.IDao#listCount(java.lang.String,  
  223.      *      java.util.ArrayList)  
  224.      */   
  225.     public int listCount(String hql, List params) throws HibernateException,   
  226.             NumberFormatException {   
  227.         if (null == hql || hql.equals("")) {   
  228.             return 0;   
  229.         }   
  230.         query = this.getSession().createQuery(hql);   
  231.         if (params != null && params.size() > 0) {   
  232.             // 循环给参数赋值   
  233.             for (int i = 0; i < params.size(); i++) {   
  234.                 query.setParameter(i, params.get(i));   
  235.             }   
  236.         }   
  237.         List list = query.list();   
  238.    
  239.         return list != null && list.size() > 0 ? Integer.parseInt(list.get(0)   
  240.                 + "") : 0;   
  241.    
  242.     }   
  243.     /*  
  244.      * (non-Javadoc)  
  245.      *   
  246.      * @see com.boxun.crm.dao.IDao_Hql#listCount(java.lang.Class,  
  247.      *      java.lang.Object)  
  248.      */   
  249.     public <T> int listCount(Class<T> c, Object obj) throws HibernateException,   
  250.             NumberFormatException {   
  251.         Criteria criteria = this.getSession().createCriteria(c);   
  252.         if (obj != null) {   
  253.             Example example = Example.create(obj);   
  254.             example.enableLike(MatchMode.ANYWHERE);// 匹配模式,使用模糊查询必填项。   
  255.             example.excludeNone();// 空的不作查询条件   
  256.             example.excludeZeroes();// 0不要查询   
  257.             example.ignoreCase(); // 不区分大小写   
  258.             criteria.add(example);   
  259.         }   
  260.    
  261.         List list = criteria.list();   
  262.         return list != null && list.size() > 0 ? list.size() : 0;   
  263.     }   
  264.     /*  
  265.      * (non-Javadoc)  
  266.      *   
  267.      * @see com.boxun.crm.dao.IDao_Hql#save(java.lang.Object)  
  268.      */   
  269.     public boolean save(Object obj) throws HibernateException,   
  270.             NumberFormatException {   
  271.         return Integer.parseInt(this.getSession().save(obj) + "") > 0 ? true   
  272.                 : false;   
  273.     }   
  274.     /*  
  275.      * (non-Javadoc)  
  276.      *   
  277.      * @see com.boxun.crm.dao.IDao_Hql#update(java.lang.Object)  
  278.      */   
  279.     public boolean update(Object obj) throws HibernateException {   
  280.         if (null == obj) {   
  281.             return false;   
  282.         }   
  283.         this.getSession().update(obj);   
  284.         return true;   
  285.     }    
  286.     public boolean update(List obj, List params) throws HibernateException {   
  287.         if(obj != null && obj.size() > 0) {   
  288.             for(int i = 0;i < obj.size(); i++){   
  289.                    
  290.                 this.getSession().save(obj.get(i));   
  291.    
  292.             }   
  293.         }   
  294.         return true;   
  295.     }   
  296.     /*  
  297.      * (non-Javadoc)  
  298.      *   
  299.      * @see org.springframework.orm.hibernate3.support.HibernateDaoSupport#getSession()  
  300.      */   
  301.     public Session getCurrSession() throws HibernateException {   
  302.         return this.getSession();   
  303.     }   
  304.     /*  
  305.      * (non-Javadoc)  
  306.      *   
  307.      * @see com.boxun.crm.dao.IDao#closeSession()  
  308.      */   
  309.     public boolean closeSession() throws HibernateException {   
  310.         this.getSession().close();   
  311.         return true;   
  312.     }      
  313.     public boolean delOrUpdate(List args, List params)   
  314.             throws HibernateException {   
  315.         // TODO Auto-generated method stub   
  316.         return false;   
  317.     }   
  318. }  

IDao_Sql实现类IDao_SqlImp:spring

  
  
           
  
  
  1. package com.boxun.crm.dao.impl;   
  2. import java.sql.CallableStatement;   
  3. import java.sql.Connection;   
  4. import java.util.List;   
  5. import org.hibernate.HibernateException;   
  6. import org.hibernate.Query;   
  7. import org.hibernate.Session;   
  8. import org.hibernate.Transaction;   
  9. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;     
  10. import com.boxun.crm.dao.IDao_Sql;    
  11. /**  
  12.  * <li>经过SQL查询语句对持久层操做接口</li>  
  13.  * <li>提供对数据库的增、删、改、查等一系列操做。</li>  
  14.  *   
  15.  * @author 旦旦而学   
  16.  * @version 1.0  
  17.  * @since jdk5.0  
  18.  */   
  19.    
  20. @SuppressWarnings("unchecked")   
  21. public class Dao_SqlImp extends HibernateDaoSupport implements IDao_Sql {   
  22.     // 声明事务对象   
  23.     public Transaction tran = null;   
  24.     // 声明查询对象   
  25.     public Query query = null;   
  26.     /*  
  27.      * (non-Javadoc)  
  28.      *   
  29.      * @see com.boxun.crm.dao.IDao#beginTx()  
  30.      */   
  31.     public boolean beginTx() throws HibernateException {   
  32.         if (tran == null) {   
  33.             tran = this.getSession().beginTransaction();   
  34.         }   
  35.         return true;   
  36.     }   
  37.     /*  
  38.      * (non-Javadoc)  
  39.      *   
  40.      * @see com.boxun.crm.dao.IDao#commitTx()  
  41.      */   
  42.     public boolean commitTx() throws HibernateException {   
  43.         if (tran != null)   
  44.             tran.commit();   
  45.         tran = null;   
  46.         return true;   
  47.    
  48.     }   
  49.     /*  
  50.      * (non-Javadoc)  
  51.      *   
  52.      * @see com.boxun.crm.dao.IDao#rollbackTx()  
  53.      */   
  54.     public boolean rollbackTx() throws HibernateException {   
  55.         if (tran != null) {   
  56.             tran.rollback();   
  57.         }   
  58.         tran = null;   
  59.         return true;   
  60.     }   
  61.     /*  
  62.      * (non-Javadoc)  
  63.      *   
  64.      * @see com.boxun.crm.dao.IDao_Sql#save(java.lang.String)  
  65.      */   
  66.     public boolean save(String sql) throws HibernateException {   
  67.         if (null == sql && "".equals(sql)) {   
  68.             return false;   
  69.         }   
  70.         query = this.getSession().createSQLQuery(sql);   
  71.         int res = query.executeUpdate();   
  72.         return res > 0 ? true : false;   
  73.     }   
  74.     /*  
  75.      * (non-Javadoc)  
  76.      *   
  77.      * @see com.boxun.crm.dao.IDao#delOrUpdate(java.lang.String, java.util.List)  
  78.      */   
  79.     public boolean delOrUpdate(String sql, List params)throws HibernateException {   
  80.         if (null == sql && "".equals(sql)) {   
  81.             return false;   
  82.         }   
  83.         query = this.getSession().createSQLQuery(sql);   
  84.         if (params != null && params.size() > 0) {   
  85.             for (int i = 0; i < params.size(); i++) {   
  86.                 query.setParameter(i, params.get(i));   
  87.             }   
  88.         }   
  89.         int res = query.executeUpdate();   
  90.         return res > 0 ? true : false;   
  91.     }   
  92.     /*  
  93.      * (non-Javadoc)  
  94.      *   
  95.      * @see com.boxun.crm.dao.IDao#find(java.lang.String)  
  96.      */   
  97.     public List<Object> find(String sql) throws HibernateException {   
  98.         if (null == sql && "".equals(sql)) {   
  99.             return null;   
  100.         }   
  101.         query = this.getSession().createSQLQuery(sql);   
  102.    
  103.         return query.list();   
  104.     }   
  105.     /*  
  106.      * 原来代码:  
  107.      * if (null == sql && sql.equals("")) {  
  108.      *      return null;          
  109.      * }  
  110.      * 其中&&不符合逻辑  
  111.      *   
  112.      * @see com.boxun.crm.dao.IDao#find(java.lang.String, java.util.List)  
  113.      */   
  114.     public List<Object> find(String sql, List params) throws HibernateException {   
  115.         if (null == sql || sql.equals("")) {   
  116.             return null;   
  117.         }   
  118.         query = this.getSession().createSQLQuery(sql);   
  119.         if (null != params && params.size() > 0) {   
  120.             for (int i = 0; i < params.size(); i++) {   
  121.                 query.setParameter(i, params.get(i));   
  122.             }   
  123.         }   
  124.         return query.list();   
  125.     }   
  126.     /*  
  127.      * (non-Javadoc)  
  128.      *   
  129.      * @see com.boxun.crm.dao.IDao#find(java.lang.String, int, int)  
  130.      */   
  131.     public List<Object> find(String sql, int row, int pages)   
  132.             throws HibernateException {   
  133.         if (null == sql && "".equals(sql)) {   
  134.             return null;   
  135.         }   
  136.         query = this.getSession().createSQLQuery(sql);   
  137.         if (row > 0 && pages > 0) {   
  138.             query.setMaxResults(row);// 最大显示记录数   
  139.             query.setFirstResult((pages - 1) * row);// 从第几条开始   
  140.         }   
  141.         return query.list();   
  142.     }   
  143.     /*  
  144.      * (non-Javadoc)  
  145.      *   
  146.      * @see com.boxun.crm.dao.IDao#find(java.lang.String, java.util.List, int,  
  147.      *      int)  
  148.      */   
  149.     public List<Object> find(String sql, List params, int row, int pages)   
  150.             throws HibernateException {   
  151.         if (null == sql && "".equals(sql)) {   
  152.             return null;   
  153.         }   
  154.         query = this.getSession().createSQLQuery(sql);   
  155.         if (params != null && params.size() > 0) {   
  156.             for (int i = 0; i < params.size(); i++) {   
  157.                 query.setParameter(i, params.get(i));   
  158.             }   
  159.         }   
  160.         if (row > 0 && pages > 0) {   
  161.             query.setMaxResults(row);// 最大显示记录数   
  162.             query.setFirstResult((pages - 1) * row);// 从第几条开始   
  163.         }   
  164.         return query.list();   
  165.     }   
  166.     /*  
  167.      * (non-Javadoc)  
  168.      *   
  169.      * @see com.boxun.crm.dao.IDao#listCount(java.lang.String)  
  170.      */   
  171.     public int listCount(String sql) throws HibernateException,   
  172.             NumberFormatException {   
  173.         if (null == sql && "".equals(sql)) {   
  174.             return 0;   
  175.         }   
  176.         int count = 0;   
  177.         query = this.getSession().createSQLQuery(sql);   
  178.         List list = query.list();   
  179.         if (list != null && list.size() > 0) {   
  180.             count = Integer.parseInt(list.get(0) + "");   
  181.         }   
  182.         return count;   
  183.     }   
  184.     /*  
  185.      * (non-Javadoc)  
  186.      *   
  187.      * @see com.boxun.crm.dao.IDao#listCount(java.lang.String, java.util.List)  
  188.      */   
  189.     public int listCount(String sql, List params) throws HibernateException,   
  190.             NumberFormatException {   
  191.         if (null == sql && "".equals(sql)) {   
  192.             return 0;   
  193.         }   
  194.         int count = 0;   
  195.         query = this.getSession().createSQLQuery(sql);   
  196.         if (params != null && params.size() > 0) {   
  197.             for (int i = 0; i < params.size(); i++) {   
  198.                 query.setParameter(i, params.get(i));   
  199.             }   
  200.         }   
  201.         List list = query.list();   
  202.         if (list != null && list.size() > 0) {   
  203.             if (list.get(0) != null) {  // 有时候size是1,但第一个元素的值为null就会报错   
  204.                 count = Integer.parseInt(list.get(0) + "");   
  205.             }   
  206.         }   
  207.         return count;   
  208.     }   
  209.     /*  
  210.      * (non-Javadoc)  
  211.      *   
  212.      * @see org.springframework.orm.hibernate3.support.HibernateDaoSupport#getSession()  
  213.      */   
  214.     public Session getCurrSession() throws HibernateException {   
  215.         return this.getSession();   
  216.     }   
  217.     /*  
  218.      * (non-Javadoc)  
  219.      *   
  220.      * @see com.boxun.crm.dao.IDao#closeSession()  
  221.      */   
  222.     public boolean closeSession() throws HibernateException {   
  223.         this.getSession().close();   
  224.         return true;   
  225.     }   
  226.     public boolean delOrUpdate(List args, List params)   
  227.             throws HibernateException {   
  228.         boolean flag = false;   
  229.         if(args != null && args.size() > 0) {   
  230.             for(int i = 0; i < args.size(); i++){   
  231.                 String sql = (String)args.get(i);   
  232.                 query = this.getSession().createSQLQuery(sql);   
  233.                 if (params != null && params.size() > 0) {   
  234.                     for (int j = 0; j < params.size(); j++) {   
  235.                         query.setParameter(j, params.get(j));   
  236.                     }   
  237.                 }   
  238.                 flag = query.executeUpdate() > 0 ? true : false;   
  239.             }   
  240.         }   
  241.         return flag;   
  242.     }   
  243.       

我把我csdn上的博客、所有转移到这边来、昨天csdn用户数据被***泄漏了!sql

个人个天~~~伤了个心、***真牛B!数据库