欢迎技术交流。 QQ:138986722
接口——IDao:
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
/**
* <li>DAO接口</li>
* <li>该类只是提供一个公用接口,以便HQL查询与SQL查询重写与重用</li>
* <li>具体实现接口有IDao_Hql.java和IDao_Sql.java</li>
*
* @author 旦旦而学
* @version v1.6
* @since jdk5.0
* @createDate May 26, 201010:45:22 AM
*/
@SuppressWarnings("unchecked")
public interface IDao {
/**
* <li>获取一个会话</li>
*
* @return Session
* @author 旦旦而学
* @throws HibernateException
*/
Session getCurrSession() throws HibernateException;
/**
* <li>开始一个事务</li>
*
* @author 旦旦而学
* @throws HibernateException
* @return 成功=true
*/
boolean beginTx() throws HibernateException;
/**
* <li>提交事务</li>
*
* @author 旦旦而学
* @throws HibernateException
* @return 成功=true
*/
boolean commitTx() throws HibernateException;
/**
* <li>回滚事务</li>
*
* @author 旦旦而学
* @throws HibernateException
* @return 成功=true
*/
boolean rollbackTx() throws HibernateException;
/**
* <li>关闭Session对象</li>
*
* @author 旦旦而学
* @throws HibernateException
* @return 成功=true
*/
boolean closeSession() throws HibernateException;
/**
* <li>通过查询语句删除或更新信息</li>
*
* @author 旦旦而学
* @param args
* 查询语句
* @param params
* 参数列表,与语句中的'?'号对应,没有则填null
* @throws HibernateException
* @return 成功=true
*/
boolean delOrUpdate(String args, List params) throws HibernateException;
/**
* <li>通过查询语句(集合)删除或更新信息</li>
*
* @author 旦旦而学
* @param args
* 查询语句
* @param params
* 参数列表,与语句中的'?'号对应,没有则填null
* @throws HibernateException
* @return 成功=true
*/
boolean delOrUpdate(List args, List params) throws HibernateException;
/**
* <li>查询</li>
*
* @author 旦旦而学
* @param args
* 查询语句
* @throws HibernateException
* @return 返回List结果集
*/
<T> List<T> find(String args) throws HibernateException;
/**
* <li>查询</li>
*
* @author 旦旦而学
* @param args:查询语句
* @param params:参数列表,与语句中的'?'号对应,没有则填null
* @throws HibernateException
* @return 返回List结果集
*/
<T> List<T> find(String args, List params) throws HibernateException;
/**
* <li>查询,带分页效果</li>
*
* @author旦旦而学
* @param args:查询语句
* @param row:每页显示多少行
* @param pages:当前显示第几页,从1开始
* @throws HibernateException
* @return 返回List结果集
*/
<T> List<T> find(String args, int row, int pages) throws HibernateException;
/**
* <li>查询,带分页效果</li>
*
* @author 旦旦而学
* @param args:查询语句
* @param params:参数列表,与语句中的'?'号对应,没有则填null
* @param row:每页显示多少行
* @param pages:当前显示第几页,从1开始
* @throws HibernateException
* @return 返回List结果集
*/
<T> List<T> find(String args, List params, int row, int pages)
throws HibernateException;
/**
* <li>查询数据总数</li>
*
* @author 旦旦而学
* @param args:查询语句,select
* count(x)...语句
* @throws HibernateException
* @throws NumberFormatException
* @return int
*/
int listCount(String args) throws HibernateException, NumberFormatException;
/**
* <li>查询数据总数</li>
*
* @author 旦旦而学
* @param args:查询语句,select
* count(x)...语句
* @param params:参数列表,与语句中的'?'号对应,没有则填null
* @throws HibernateException
* @throws NumberFormatException
* @return int
*/
int listCount(String args, List params) throws HibernateException,
NumberFormatException;
}
接口——IDao_Hql:
import java.io.Serializable;
import java.util.List;
import org.hibernate.HibernateException;
/**
* <li>通过Hibernate语句对持久层操作接口</li>
* <li>提供对数据库的增、删、改、查等一系列操作。</li>
* <li>事务管理交由业务层完成</li>
*
* @author 旦旦而学
* @version 1.0
* @since jdk5.0
* @createDate May 26, 201010:53:33 AM
*/
public interface IDao_Hql extends IDao {
/**
* <li>插入一条数据</li>
*
* @author 旦旦而学
* @param obj:Hibernate实体对象
* @exception HibernateException
* @return true:成功
*/
boolean save(Object obj) throws HibernateException;
/**
* <li>删除一条数据</li>
*
* @author 旦旦而学
* @param obj:Hibernate实体对象
* @exception HibernateException
* @return true:成功
*/
boolean del(Object obj) throws HibernateException;
/**
* <li>更新一条数据</li>
*
* @author 旦旦而学
* @param obj:Hibernate实体对象
* @exception HibernateException
* @return true:成功
*/
boolean update(Object obj) throws HibernateException;
boolean update(List obj, List params) throws HibernateException;
/**
* <li>通过数据库表主键,得到一个对象,如果对象不存在将返回null</li>
*
* @author 旦旦而学
* @param c:要查询的实体类
* @param s:对应主键值
* @exception HibernateException
* @return 对应的实体类实例
*/
<T> T get(Class<T> c, Serializable s) throws HibernateException;
/**
* <li>QBC查询</li>
*
* @author 旦旦而学
* @param c:查询的类
* @param obj:查询的对象
* @param orber:按那几个字段排序
* @param row:每页显示多少行
* @param pages:当前显示第几页,从1开始
* @exception HibernateException
* @return List
*/
<T> List<T> find(Class<T> c, T obj, String[] orders, int row, int pages)
throws HibernateException;
/**
* <li>QBC查询数据总数</li>
*
* @author 旦旦而学
* @param c
* 查询的对象
* @param obj
* 查询的对象,如果没有值则填null
* @exception HibernateException
* @return int
*/
<T> int listCount(Class<T> c, Object obj) throws HibernateException;
}
接口——IDao_Sql:
import org.hibernate.HibernateException;
/**
* <li>通过SQL SERVER查询语句对持久层操作接口</li>
* <li>提供对数据库的增、删、改、查等一系列操作。</li>
* <li>事务管理交由业务层完成</li>
*
* @author 旦旦而学
* @version 1.0
* @since jdk5.0
* @createDate May 26, 201010:54:34 AM
*/
public interface IDao_Sql extends IDao {
/**
* <li>插入一条数据</li>
*
* @author 旦旦而学
* @param sql
* SQL的insert语句
* @exception HibernateException
* @return true:成功
*/
boolean save(String sql) throws HibernateException;
}
IDao_Hql实现类IDao_HqlImp:
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:
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;
}
}