Date 2011-1-7 author:heguikun
package dao.impl;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Example;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class BaseDao extends HibernateDaoSupport {
@SuppressWarnings("unchecked")//去掉黄色警告
// 1.获取 对象
public Object get(Class clz, java.io.Serializable id) {
return super.getHibernateTemplate().get(clz, id);
}
// 2. 添加对象
public void save(Object item) {
super.getHibernateTemplate().save(item);
}
// =============3.删除,不用实例化,把主键传进来=============
public void delete(Class clz, java.io.Serializable id) {
super.getHibernateTemplate().delete(
super.getHibernateTemplate().get(clz, id));
}
//================3.3删除 直接传进对象 ,必须先实例化=============
public void delete(Object entity) {
super.getHibernateTemplate().delete(entity);
}
// 4.修改
public void update(Object entity) {
super.getHibernateTemplate().update(entity);
}
// 5.Hql 查询 并控制分页startIndex从第一条开始,pageSize每次查询的长度
public List Query(final String hql, final int startIndex,final int pageSize) {
return super.getHibernateTemplate().find(hql);
/*return super.getHibernateTemplate().executeFind(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
org.hibernate.Query query = session.createQuery(hql);
query.setFirstResult(startIndex);// 第几条 开始
query.setMaxResults(pageSize);// 查询的条数
return query.list();// 返回集合
}
});*/
}
// 6. 查询某个表的总条数
public Integer getTotalCount(Object entity) {
Session session = super.getSession();
String hqlString = "select count(*) from " + entity;// 拼接查询
System.out.println("查询语句:"+hqlString);
org.hibernate.Query query = session.createQuery(hqlString);
Long numLong=(Long) query.uniqueResult();// 返回条数
String numString=numLong+"";//不能一步帮Long转为整形
session.close();//必须关闭
return Integer.parseInt(numString);
// -以为每次查询都是一个整数值,uniqueResult()获取唯一的查询结果
}
// 7. 根据 对象 查询 Search(PetInfo.class,new PetInfo());
// newPetInfo()得到表的所有信息,放某个已经实例化好的 对象并保护值时就会取到的是单个对象
public List Search(final Class clz, final Object connection,
final int startIndex, final int pageSize) {
return super.getHibernateTemplate().executeFind(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Criteria criteria = session.createCriteria(clz).add(
Example.create(connection));
criteria.setFirstResult(startIndex);// 第几条 开始
criteria.setMaxResults(pageSize);// 查询的条数
return criteria.list();
}
});
}
//8懒加载
public void initialize(Object entity) {
super.getHibernateTemplate().initialize(entity);
}
public static void main(String[] args) {
ApplicationContext context = new FileSystemXmlApplicationContext(
"/WebRoot/WEB-INF/applicationContext.xml");// 初始化
BaseDao dao = (BaseDao) context.getBean("baseDao");
List list = dao.Query("from Psxm", 0, 100);
if (list != null) {
System.out.println(list.size());
//System.out.println(list.get(0));
} else {
System.out.println("空");
}
}}