package com.hr.dao.impl;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
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.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.hr.util.MyPage;
public class SpringDao extends HibernateDaoSupport {
/**
* 查找单个对象
*
* @param cls
* @param id
* @return
*/
public Object get(Class cls, Serializable id) {
return super.getHibernateTemplate().get(cls, id);
}
public int del(Class cls, Serializable id) {
int row = 0;
try {
getHibernateTemplate().delete(get(cls, id));
row = 1;
} catch (Exception e) {
e.printStackTrace();
} finally {
}
return row;
}
public int edit(Object o) {
int row = 0;
try {
getHibernateTemplate().update(o);
row = 1;
} catch (Exception e) {
e.printStackTrace();
} finally {
}
return row;
}
public int add(Object o) {
int row = 0;
try {
getHibernateTemplate().save(o);
row = 1;
} catch (Exception e) {
e.printStackTrace();
} finally {
}
return row;
}
public Object getUniqueValue(String hql,Object[] values) {
Object o=null;
int row = 0;
try {
o =getHibernateTemplate().find(hql,values).get(0);
} catch (Exception e) {
e.printStackTrace();
} finally {
}
return o;
}
/**
* 执行批量删除修改
*
* @param sql
* @return
*/
public int executeUpdate( final String hql,final Object[] values) {
int row = 0;
row=(Integer)getHibernateTemplate().execute(new HibernateCallback<Object>() {
@Override
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
int row=0;
Query q=session.createSqlQuery(hql); //建议用sqlQuery
// Query q=session.createQuery(hql); 这种方式可能会出现缓存与数据库不同步问题
if(values!=null){
for (int i = 0; i < values.length; i++) {
q.setParameter(i, values[i]);
}
}
row= q.executeUpdate();
return row;
}
});
return row;
}
/**
* 通用的查询方法
*
* @param sql
* @return
*/
public List executeQuery(String hql ,Object[] values) {
Session session = null;
List list = new ArrayList();
int row = 0;
try {
list = getHibernateTemplate().find(hql,values);
} catch (Exception e) {
e.printStackTrace();
} finally {
}
return list;
}
public List executeQuery( final String hql ,final Object[] values,final MyPage page) {
List list = new ArrayList();
int row = 0;
list=(List) getHibernateTemplate().execute(new HibernateCallback<Object>() {
@Override
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query q = session.createQuery(hql);
if(values!=null){
for (int i = 0; i < values.length; i++) {
q.setParameter(i, values[i]);
}
}
if(page!=null){
q.setFirstResult((page.getCurpage()-1)*page.getPagesize());
q.setMaxResults(page.getPagesize());
}
List l=q.list();
return l;
}
});
page.setResult(list);
return list;
}
public List executeQuery(final String hql ,final Object[] values,final int curpage,final int pagesize) {
List list = new ArrayList();
int row = 0;
list=(List) getHibernateTemplate().execute(new HibernateCallback<Object>() {
@Override
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query q = session.createQuery(hql);
if(values!=null){
for (int i = 0; i < values.length; i++) {
q.setParameter(i, values[i]);
}
}
q.setFirstResult((curpage-1)*pagesize);
q.setMaxResults(pagesize);
List l=q.list();
return l;
}
});
return list;
}
}