package bgi.itsm.dao;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import bgi.itsm.pojo.TEmp;
import bgi.itsm.pojo.TRole;
import bgi.itsm.pojo.TType;
public class EmpDao extends HibernateDaoSupport{
public List queryEmpCount(final String hql,final String search) {
List list=getHibernateTemplate().executeFind(new HibernateCallback() {//excuteFind 返回的是一个List
public Object doInHibernate(Session session) throws HibernateException,
SQLException {
Query query = session.createQuery(hql);
query.setParameter("num", "%"+search+"%");
query.setParameter("name", "%"+search+"%");
query.setParameter("dept", "%"+search+"%");
query.setParameter("mail", "%"+search+"%");
query.setParameter("phone", search);
query.setParameter("search", search);
List list = query.list();
return list;
}
});
return list;
}
public List queryEmpList(final String hql,final String search,final int offset, final int pageSize) {
List list=getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException,
SQLException {
Query query = session.createQuery(hql);
query.setParameter("num", "%"+search+"%");
query.setParameter("name", "%"+search+"%");
query.setParameter("dept", "%"+search+"%");
query.setParameter("mail", "%"+search+"%");
query.setParameter("phone", search);
query.setParameter("search", search);
List list = query.setFirstResult(offset).setMaxResults(pageSize).list();
return list;
}
});
return list;
}
@SuppressWarnings("unchecked")
public TEmp queryEmpByEmail(String email){
TEmp emp = new TEmp();;
String hql = "from TEmp where email=? and visible='1'";
try {
List list = this.getHibernateTemplate().find(hql, email);
if(list!=null &&
list.size()>0){
emp = list.get(0);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return emp;
}
@SuppressWarnings("unchecked")
public TEmp queryEmpByNum(String empNum){
TEmp emp = new TEmp();
String hql = "from TEmp where empNum=? and visible='1'";
try {
List list = this.getHibernateTemplate().find(hql,empNum);
if(list.size()>0){
emp = list.get(0);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return emp;
}
public TEmp queryEmpById(TEmp emp) {
TEmp tEmp = null;
try {
tEmp = (TEmp)this.getHibernateTemplate().get(TEmp.class, emp.getEmpId());
} catch (Exception e) {
e.printStackTrace();
}
return tEmp;
}
@SuppressWarnings("unchecked")
public int deleteEmpsByIds(final String hql){
int i = (Integer)getHibernateTemplate().execute(new HibernateCallback() {//excute返回的是一个Object
public Object doInHibernate(Session session) throws HibernateException,
SQLException {
Query query = session.createQuery(hql);
int a = query.executeUpdate();
return a;
}
});
return i;
}
public boolean saveEmp(TEmp emp){
boolean flag = false;
try {
this.getHibernateTemplate().save(emp);
flag = true;
} catch (Exception e) {
e.printStackTrace();
flag = false;
}
return flag;
}
public boolean updateEmp(TEmp emp){
boolean flag = false;
try {
this.getHibernateTemplate().update(emp);
flag = true;
} catch (Exception e) {
e.printStackTrace();
flag = false;
}
return flag;
}
@SuppressWarnings("unchecked")
public List queryRoleList(){
List list = this.getHibernateTemplate().find("from TRole");
return list;
}
public List queryTypeList() {
List list = this.getHibernateTemplate().find("from TType t where t.visible='1'");
return list;
}
public List queryEmpListByIds(String hql){
List empList = this.getHibernateTemplate().find(hql);
return empList;
}
}