IDao_Hql實現類IDao_HqlImp:java
- package com.boxun.crm.dao.impl;
- import java.io.Serializable;
- import java.util.List;
- import org.hibernate.Criteria;
- import org.hibernate.HibernateException;
- import org.hibernate.Query;
- import org.hibernate.Session;
- import org.hibernate.Transaction;
- import org.hibernate.criterion.Example;
- import org.hibernate.criterion.MatchMode;
- import org.hibernate.criterion.Order;
- import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
- import com.boxun.crm.dao.IDao_Hql;
- /**
- * <li>經過Hibernate對持久層操做接口實現類</li>
- * <li>提供對數據庫的增、刪、改、查等一系列操做。</li>
- *
- * @author 旦旦而學
- * @version 1.0
- * @since jdk5.0
- */
- @SuppressWarnings("unchecked")
- public class Dao_HqlImp extends HibernateDaoSupport implements IDao_Hql {
- // 聲明事務對象
- private Transaction tran = null;
- // 聲明查詢對象
- private Query query = null;
- /*
- * (non-Javadoc)
- *
- * @see com.boxun.crm.dao.IDao#beginTx()
- */
- public boolean beginTx() throws HibernateException {
- if (tran == null)
- tran = this.getSession().beginTransaction();
- return true;
- }
- /*
- * (non-Javadoc)
- *
- * @see com.boxun.crm.dao.IDao#commitTx()
- */
- public boolean commitTx() throws HibernateException {
- if (tran != null)
- tran.commit();
- tran = null;
- return true;
- }
- /*
- * (non-Javadoc)
- *
- * @see com.boxun.crm.dao.IDao#rollbackTx()
- */
- public boolean rollbackTx() throws HibernateException {
- tran.rollback();
- tran = null;
- return true;
- }
- /*
- * (non-Javadoc)
- *
- * @see com.boxun.crm.dao.IDao_Hql#del(java.lang.Object)
- */
- public boolean del(Object obj) throws HibernateException {
- this.getSession().delete(obj);
- return true;
- }
- /*
- * (non-Javadoc)
- *
- * @see com.boxun.crm.dao.IDao#delOrUpdate(java.lang.String,
- * java.util.ArrayList)
- */
- public boolean delOrUpdate(String hql, List params)
- throws HibernateException {
- if (null == hql || hql.equals("")) {
- return false;
- }
- query = this.getSession().createQuery(hql);
- if (null != params && params.size() > 0) {
- for (int i = 0; i < params.size(); i++) {
- query.setParameter(i, params.get(i));
- }
- }
- int result = query.executeUpdate();
- return result > 0 ? true : false;
- }
- /*
- * (non-Javadoc)
- *
- * @see com.boxun.crm.dao.IDao#find(java.lang.String)
- */
- public List<Object> find(String hql) throws HibernateException {
- if (null == hql || hql.equals("")) {
- return null;
- }
- query = this.getSession().createQuery(hql);
- return query.list();
- }
- /*
- * (non-Javadoc)
- *
- * @see com.boxun.crm.dao.IDao#find(java.lang.String, java.util.ArrayList)
- */
- public <T> List<T> find(String hql, List params) throws HibernateException {
- if (null == hql || hql.equals("")) {
- return null;
- }
- query = this.getSession().createQuery(hql);
- if (null != params && params.size() > 0) {
- // 循環給參數賦值
- for (int i = 0; i < params.size(); i++) {
- query.setParameter(i, params.get(i));
- }
- }
- List<T> list = query.list();
- return list;
- }
- /*
- * (non-Javadoc)
- *
- * @see com.boxun.crm.dao.IDao#find(java.lang.String, int, int)
- */
- public List<Object> find(String hql, int row, int pages)
- throws HibernateException {
- if (null == hql || hql.equals("")) {
- return null;
- }
- query = this.getSession().createQuery(hql);
- if (row > 0 && pages > 0) {
- // 取得每頁顯示結果集
- query.setMaxResults(row);
- // 取得當前頁碼所要顯示的結果集
- query.setFirstResult(row * (pages - 1));
- }
- return query.list();
- }
- /*
- * (non-Javadoc)
- *
- * @see com.boxun.crm.dao.IDao#find(java.lang.String, java.util.ArrayList,
- * int, int)
- */
- public List<Object> find(String hql, List params, int row, int pages)
- throws HibernateException {
- if (null == hql || hql.equals("")) {
- return null;
- }
- query = this.getSession().createQuery(hql);
- if (params != null && params.size() > 0) {
- // 循環給參數賦值
- for (int i = 0; i < params.size(); i++) {
- query.setParameter(i, params.get(i));
- }
- }
- if (row > 0 && pages > 0) {
- // 取得每頁顯示結果集
- query.setMaxResults(row);
- // 取得當前頁碼所要顯示的結果集
- query.setFirstResult(row * (pages - 1));
- }
- return query.list();
- }
- /*
- * (non-Javadoc)
- *
- * @see com.boxun.crm.dao.IDao_Hql#find(java.lang.Class, java.lang.Object,
- * java.lang.String[], int, int)
- */
- public <T> List<T> find(Class<T> c, T obj, String[] orders, int row,
- int pages) throws HibernateException {
- Criteria criteria = this.getSession().createCriteria(c);
- if (obj != null) {
- Example example = Example.create(obj);
- example.enableLike(MatchMode.ANYWHERE);// 匹配模式,使用模糊查詢必填項。
- example.excludeNone();// 空的不作查詢條件
- example.excludeZeroes();// 0不要查詢
- example.ignoreCase(); // 不區分大小寫
- criteria.add(example);
- }
- if (row > 0 && pages > 0) {
- criteria.setMaxResults(row);// 最大顯示記錄數
- criteria.setFirstResult((pages - 1) * row);// 從第幾條開始
- }
- // 判斷是否有排序請求,若是有加入到排序方法中
- if (orders != null) {
- for (int i = 0; i < orders.length; i++)
- criteria.addOrder(Order.desc(orders[i]));
- }
- return criteria.list();
- }
- /*
- * (non-Javadoc)
- *
- * @see com.boxun.crm.dao.IDao_Hql#get(java.lang.Class,
- * java.io.Serializable)
- */
- public <T> T get(Class<T> c, Serializable s) throws HibernateException {
- return (T) this.getSession().get(c, s);
- }
- /*
- * (non-Javadoc)
- *
- * @see com.boxun.crm.dao.IDao#listCount(java.lang.String)
- */
- public int listCount(String hql) throws HibernateException,
- NumberFormatException {
- if (null == hql || hql.equals("")) {
- return 0;
- }
- query = this.getSession().createQuery(hql);
- List list = query.list();
- return list != null && list.size() > 0 ? Integer.parseInt(list.get(0)
- + "") : 0;
- }
- /*
- * (non-Javadoc)
- *
- * @see com.boxun.crm.dao.IDao#listCount(java.lang.String,
- * java.util.ArrayList)
- */
- public int listCount(String hql, List params) throws HibernateException,
- NumberFormatException {
- if (null == hql || hql.equals("")) {
- return 0;
- }
- query = this.getSession().createQuery(hql);
- if (params != null && params.size() > 0) {
- // 循環給參數賦值
- for (int i = 0; i < params.size(); i++) {
- query.setParameter(i, params.get(i));
- }
- }
- List list = query.list();
- return list != null && list.size() > 0 ? Integer.parseInt(list.get(0)
- + "") : 0;
- }
- /*
- * (non-Javadoc)
- *
- * @see com.boxun.crm.dao.IDao_Hql#listCount(java.lang.Class,
- * java.lang.Object)
- */
- public <T> int listCount(Class<T> c, Object obj) throws HibernateException,
- NumberFormatException {
- Criteria criteria = this.getSession().createCriteria(c);
- if (obj != null) {
- Example example = Example.create(obj);
- example.enableLike(MatchMode.ANYWHERE);// 匹配模式,使用模糊查詢必填項。
- example.excludeNone();// 空的不作查詢條件
- example.excludeZeroes();// 0不要查詢
- example.ignoreCase(); // 不區分大小寫
- criteria.add(example);
- }
- List list = criteria.list();
- return list != null && list.size() > 0 ? list.size() : 0;
- }
- /*
- * (non-Javadoc)
- *
- * @see com.boxun.crm.dao.IDao_Hql#save(java.lang.Object)
- */
- public boolean save(Object obj) throws HibernateException,
- NumberFormatException {
- return Integer.parseInt(this.getSession().save(obj) + "") > 0 ? true
- : false;
- }
- /*
- * (non-Javadoc)
- *
- * @see com.boxun.crm.dao.IDao_Hql#update(java.lang.Object)
- */
- public boolean update(Object obj) throws HibernateException {
- if (null == obj) {
- return false;
- }
- this.getSession().update(obj);
- return true;
- }
- public boolean update(List obj, List params) throws HibernateException {
- if(obj != null && obj.size() > 0) {
- for(int i = 0;i < obj.size(); i++){
- this.getSession().save(obj.get(i));
- }
- }
- return true;
- }
- /*
- * (non-Javadoc)
- *
- * @see org.springframework.orm.hibernate3.support.HibernateDaoSupport#getSession()
- */
- public Session getCurrSession() throws HibernateException {
- return this.getSession();
- }
- /*
- * (non-Javadoc)
- *
- * @see com.boxun.crm.dao.IDao#closeSession()
- */
- public boolean closeSession() throws HibernateException {
- this.getSession().close();
- return true;
- }
- public boolean delOrUpdate(List args, List params)
- throws HibernateException {
- // TODO Auto-generated method stub
- return false;
- }
- }
IDao_Sql實現類IDao_SqlImp:spring
- package com.boxun.crm.dao.impl;
- import java.sql.CallableStatement;
- import java.sql.Connection;
- import java.util.List;
- import org.hibernate.HibernateException;
- import org.hibernate.Query;
- import org.hibernate.Session;
- import org.hibernate.Transaction;
- import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
- import com.boxun.crm.dao.IDao_Sql;
- /**
- * <li>經過SQL查詢語句對持久層操做接口</li>
- * <li>提供對數據庫的增、刪、改、查等一系列操做。</li>
- *
- * @author 旦旦而學
- * @version 1.0
- * @since jdk5.0
- */
- @SuppressWarnings("unchecked")
- public class Dao_SqlImp extends HibernateDaoSupport implements IDao_Sql {
- // 聲明事務對象
- public Transaction tran = null;
- // 聲明查詢對象
- public Query query = null;
- /*
- * (non-Javadoc)
- *
- * @see com.boxun.crm.dao.IDao#beginTx()
- */
- public boolean beginTx() throws HibernateException {
- if (tran == null) {
- tran = this.getSession().beginTransaction();
- }
- return true;
- }
- /*
- * (non-Javadoc)
- *
- * @see com.boxun.crm.dao.IDao#commitTx()
- */
- public boolean commitTx() throws HibernateException {
- if (tran != null)
- tran.commit();
- tran = null;
- return true;
- }
- /*
- * (non-Javadoc)
- *
- * @see com.boxun.crm.dao.IDao#rollbackTx()
- */
- public boolean rollbackTx() throws HibernateException {
- if (tran != null) {
- tran.rollback();
- }
- tran = null;
- return true;
- }
- /*
- * (non-Javadoc)
- *
- * @see com.boxun.crm.dao.IDao_Sql#save(java.lang.String)
- */
- public boolean save(String sql) throws HibernateException {
- if (null == sql && "".equals(sql)) {
- return false;
- }
- query = this.getSession().createSQLQuery(sql);
- int res = query.executeUpdate();
- return res > 0 ? true : false;
- }
- /*
- * (non-Javadoc)
- *
- * @see com.boxun.crm.dao.IDao#delOrUpdate(java.lang.String, java.util.List)
- */
- public boolean delOrUpdate(String sql, List params)throws HibernateException {
- if (null == sql && "".equals(sql)) {
- return false;
- }
- query = this.getSession().createSQLQuery(sql);
- if (params != null && params.size() > 0) {
- for (int i = 0; i < params.size(); i++) {
- query.setParameter(i, params.get(i));
- }
- }
- int res = query.executeUpdate();
- return res > 0 ? true : false;
- }
- /*
- * (non-Javadoc)
- *
- * @see com.boxun.crm.dao.IDao#find(java.lang.String)
- */
- public List<Object> find(String sql) throws HibernateException {
- if (null == sql && "".equals(sql)) {
- return null;
- }
- query = this.getSession().createSQLQuery(sql);
- return query.list();
- }
- /*
- * 原來代碼:
- * if (null == sql && sql.equals("")) {
- * return null;
- * }
- * 其中&&不符合邏輯
- *
- * @see com.boxun.crm.dao.IDao#find(java.lang.String, java.util.List)
- */
- public List<Object> find(String sql, List params) throws HibernateException {
- if (null == sql || sql.equals("")) {
- return null;
- }
- query = this.getSession().createSQLQuery(sql);
- if (null != params && params.size() > 0) {
- for (int i = 0; i < params.size(); i++) {
- query.setParameter(i, params.get(i));
- }
- }
- return query.list();
- }
- /*
- * (non-Javadoc)
- *
- * @see com.boxun.crm.dao.IDao#find(java.lang.String, int, int)
- */
- public List<Object> find(String sql, int row, int pages)
- throws HibernateException {
- if (null == sql && "".equals(sql)) {
- return null;
- }
- query = this.getSession().createSQLQuery(sql);
- if (row > 0 && pages > 0) {
- query.setMaxResults(row);// 最大顯示記錄數
- query.setFirstResult((pages - 1) * row);// 從第幾條開始
- }
- return query.list();
- }
- /*
- * (non-Javadoc)
- *
- * @see com.boxun.crm.dao.IDao#find(java.lang.String, java.util.List, int,
- * int)
- */
- public List<Object> find(String sql, List params, int row, int pages)
- throws HibernateException {
- if (null == sql && "".equals(sql)) {
- return null;
- }
- query = this.getSession().createSQLQuery(sql);
- if (params != null && params.size() > 0) {
- for (int i = 0; i < params.size(); i++) {
- query.setParameter(i, params.get(i));
- }
- }
- if (row > 0 && pages > 0) {
- query.setMaxResults(row);// 最大顯示記錄數
- query.setFirstResult((pages - 1) * row);// 從第幾條開始
- }
- return query.list();
- }
- /*
- * (non-Javadoc)
- *
- * @see com.boxun.crm.dao.IDao#listCount(java.lang.String)
- */
- public int listCount(String sql) throws HibernateException,
- NumberFormatException {
- if (null == sql && "".equals(sql)) {
- return 0;
- }
- int count = 0;
- query = this.getSession().createSQLQuery(sql);
- List list = query.list();
- if (list != null && list.size() > 0) {
- count = Integer.parseInt(list.get(0) + "");
- }
- return count;
- }
- /*
- * (non-Javadoc)
- *
- * @see com.boxun.crm.dao.IDao#listCount(java.lang.String, java.util.List)
- */
- public int listCount(String sql, List params) throws HibernateException,
- NumberFormatException {
- if (null == sql && "".equals(sql)) {
- return 0;
- }
- int count = 0;
- query = this.getSession().createSQLQuery(sql);
- if (params != null && params.size() > 0) {
- for (int i = 0; i < params.size(); i++) {
- query.setParameter(i, params.get(i));
- }
- }
- List list = query.list();
- if (list != null && list.size() > 0) {
- if (list.get(0) != null) { // 有時候size是1,但第一個元素的值爲null就會報錯
- count = Integer.parseInt(list.get(0) + "");
- }
- }
- return count;
- }
- /*
- * (non-Javadoc)
- *
- * @see org.springframework.orm.hibernate3.support.HibernateDaoSupport#getSession()
- */
- public Session getCurrSession() throws HibernateException {
- return this.getSession();
- }
- /*
- * (non-Javadoc)
- *
- * @see com.boxun.crm.dao.IDao#closeSession()
- */
- public boolean closeSession() throws HibernateException {
- this.getSession().close();
- return true;
- }
- public boolean delOrUpdate(List args, List params)
- throws HibernateException {
- boolean flag = false;
- if(args != null && args.size() > 0) {
- for(int i = 0; i < args.size(); i++){
- String sql = (String)args.get(i);
- query = this.getSession().createSQLQuery(sql);
- if (params != null && params.size() > 0) {
- for (int j = 0; j < params.size(); j++) {
- query.setParameter(j, params.get(j));
- }
- }
- flag = query.executeUpdate() > 0 ? true : false;
- }
- }
- return flag;
- }
- }
我把我csdn上的博客、所有轉移到這邊來、昨天csdn用戶數據被***泄漏了!sql
個人個天~~~傷了個心、***真牛B!數據庫