hibernate的查询
1.HQL方式:所有查询都是根据java对象名来完成,对象名替换表名
2.SQL方式:保留原来的sql查询风格
3.可以通过设置第一条和最大条数来实现各种数据库的分页查询
4.通过BaseDAO来实现dao的代码优化
实例1:hql、sql查询
Test.java
package com.dao; import java.util.Date; import java.util.List; import org.hibernate.*; import org.hibernate.cfg.*; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import com.pojo.Student; public class Test { public static void main(String[] args) { // 配置对象,用来获取核心配置文件的 // 如果核心配置文件名是:hibernate.cfg.xml,并且该文件在src根目录下,configure()可以不带参数 Configuration configuration = new Configuration().configure(); ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(configuration.getProperties()) .buildServiceRegistry(); SessionFactory sf = configuration.buildSessionFactory(sr); Session session = sf.openSession(); //1.hql:是hibernate的专用查询语句,是针对对象的查询方法,其中没有表名,全都是对象名 String hql = "from Student"; // String hql = "select count(*) from Student a"; Query query = session.createQuery(hql); query.setFirstResult(5); query.setMaxResults(5); List<Student> list = query.list(); // System.out.println(list.get(0)); // for (Student stu : list) { // System.out.println(stu.getClassId()+"\t"+stu.getStudentName()); // } //2.sql:用传统的sql语句来完成查询 String sql = "select * from Student_info"; SQLQuery squery = session.createSQLQuery(sql); squery.addEntity(Student.class);//指明结果封装对象 squery.setFirstResult(5); squery.setMaxResults(5); List<Student> list2 = squery.list(); for (Student stu : list2) { System.out.println(stu.getClassId()+"\t"+stu.getStudentName()); } session.flush(); session.close(); } }
实例2:写一个分页查询的dao方法
Test2.java
package com.dao; import java.util.List; import com.pojo.Student; public class Test2 { public static void main(String[] args) { IStudentDAO stuDAO = new StudentDAO();
List<Student> list = stuDAO.queryStudentByPage(2, 5, "from Student"); for (Student stu : list) { System.out.println(stu.getStudentName()); } } }
IStudentDAO.java
package com.dao; import java.util.List; import com.pojo.Student; public interface IStudentDAO extends IBaseDAO<Student>{ public List<Student> queryStudentByPage(int index,int pagecount,String hql); }
Student.java
package com.dao; import java.util.List; import org.hibernate.Query; import org.hibernate.SQLQuery; import org.hibernate.Session; import com.pojo.Student; public class StudentDAO extends BaseDAO<Student> implements IStudentDAO{ @Override public List<Student> queryStudentByPage(int index, int pagecount,String hql) { Session session = getSession();
Query hq = session.createQuery(hql); hq.setFirstResult((index-1)*pagecount); hq.setMaxResults(pagecount);
List<Student> list = hq.list();
session.flush();
session.close();
return list; } }
附:IBaseDao.java
package com.dao; import java.util.List; import org.hibernate.Session; public interface IBaseDAO<T> { Session getSession(); void saveObject(T t); void delObject(T t); void updateObject(T t); List<T> queryObjectByHql(String hql); List<T> queryObjectBySql(String sql, Class t); }
BaseDao.java
package com.dao; import java.util.List; import org.hibernate.Query; import org.hibernate.SQLQuery; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import com.pojo.Student; public class BaseDAO<T> implements IBaseDAO<T> { public Session getSession() { // 配置对象,用来获取核心配置文件的 // 如果核心配置文件名是:hibernate.cfg.xml,并且该文件在src根目录下,configure()可以不带参数 Configuration configuration = new Configuration().configure(); ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(configuration.getProperties()) .buildServiceRegistry(); SessionFactory sf = configuration.buildSessionFactory(sr); Session session = sf.openSession(); return session; } public void saveObject(T t) { Session session = getSession(); session.save(t); session.beginTransaction().commit(); session.close(); } public void delObject(T t) { Session session = getSession(); session.delete(t); session.beginTransaction().commit(); session.close(); } public void updateObject(T t) { Session session = getSession(); session.update(t); session.beginTransaction().commit(); session.close(); } public List<T> queryObjectByHql(String hql) { Session session = getSession(); Query query = session.createQuery(hql); List<T> list = query.list(); session.flush(); session.close(); return list; } public List<T> queryObjectBySql(String sql,Class t) { Session session = getSession(); SQLQuery sq = session.createSQLQuery(sql); sq.addEntity(t); List<T> list = sq.list(); session.flush(); session.close(); return list; } }