Hibernate 还算完整的通用DAO类

package dao;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.Query;
import po.HibernateSessionFactory;
import po.StuInfo;
import po.StuMarks;
import java.util.*;

public class Dao {

public List query1(){
  Session session=HibernateSessionFactory.getSession();
  String sql="select si from StuInfo si";
  Query query=session.createQuery(sql);
  List list=query.list();
  session.close();
  return list;
}
//模糊查询
public List query2(){
  Session session=HibernateSessionFactory.getSession();
  String sql="select si from  StuInfo si where si.stuAddress like '%北京%'";
  Query query=session.createQuery(sql);
  List list=query.list();
  return list;
}
//预处理查询
public List query3(){
  Session session=HibernateSessionFactory.getSession();
  String sql="select si from  StuInfo si where si.stuAddress like ?";
  Query query=session.createQuery(sql);
  query.setString(0,"%北京%");
  List list=query.list();
  return list;
}

public List query4(){
  Session session=HibernateSessionFactory.getSession();
  String sql="select si from  StuInfo si where si.stuSeat>=:seat";
  Query query=session.createQuery(sql);
  query.setInteger("seat",2);
  List list=query.list();
  return list;
}
//多表查询
public List query5(){
  Session session=HibernateSessionFactory.getSession();
  String sql="select si from  StuInfo si,StuMarks sm where sm.stuInfo=si and sm.writtenExam>60";
  Query query=session.createQuery(sql);
  
  List list=query.list();
  return list;
}
       //分页
public List query6(int pageNo,int pageSize){
  Session session=HibernateSessionFactory.getSession();
  String sql="select si from  StuInfo si order by si.stuSeat asc";
  Query query=session.createQuery(sql);
  int beginnumber=pageSize*(pageNo-1);
  query.setFirstResult(beginnumber);
  query.setMaxResults(pageSize);
  List list=query.list();
  return list;
}
//统计函数
public int query7(){
  Session session=HibernateSessionFactory.getSession();
  String sql="select count(si) from StuInfo si";
  Query query=session.createQuery(sql);
  Integer count=(Integer)query.uniqueResult();
  
  return count.intValue();
}
//子查询
public List query8(){
  Session session=HibernateSessionFactory.getSession();
  String sql="select si from StuInfo as si,StuMarks sm where sm.stuInfo=si and sm.writtenExam>(select avg(sm.writtenExam) from sm)";
                Query query=session.createQuery(sql);
  List list=query.list();

  return list;
}


}

你可能感兴趣的:(Hibernate)