1. hibernate 提供了多种查询方式,使得可以更好的适应各种应用场景,常见的有,hql查询,本地sql查询, Criteria查询等。
2. hql:hibernate query language, hibernate查询语言,它是一种完全面向对象的查询语言,在hql中,关键字时不区分大小写的,而类名和属性名时区分大小写的。
3.查询案例:
public class HqlTest { private Session session=null; @Before public void setUp(){ session = HibernateUtil.getSession(); } @After public void tearDown(){ HibernateUtil.close(); } @Test public void testCreateDB(){ Configuration cfg = new Configuration().configure(); SchemaExport export = new SchemaExport(cfg); //第一个参数 是否打印 数据库脚本 //第二个参数 是否将脚本导入数据库执行 export.create(true, true); } //查询所有 @Test public void testFindAll(){ //该hql语言等同于select * from t_student //hql中Student是类名 String hql="from Student"; //createQuery指使用hql查询 List<Student> list = session.createQuery(hql).list(); for(Student s:list){ System.out.println(s); } } //条件查询 @Test public void testCondition(){ //如果是条件查询 可以在hql中添加查询的条件 //在hql中添加别名的方式和sql一样 可以使用空格也可以使用as String hql="from Student s where s.age<40"; List<Student> list = session.createQuery(hql).list(); for(Student s:list){ System.out.println(s); } } //使用占位符的条件查询 @Test public void testConditionParam(){ String hql="from Student s where s.age<?"; List<Student> list = session.createQuery(hql) .setInteger(0, 40)//为占位符设置参数 下标从0开始 .list(); for(Student s:list){ System.out.println(s); } } //使用指定名称来进行条件查询 @Test public void testConditionNameParam(){ //使用名称来代表参数时 需要在名称前加冒号“:” String hql="from Student s where s.age<:age"; List<Student> list = session.createQuery(hql) .setInteger("age", 40)//为指定名称的参数设置 .list(); for(Student s:list){ System.out.println(s); } } //查询多条件 @Test public void testConditionNameParams(){ //使用名称来代表参数时 需要在名称前加冒号“:” String hql="from Student s where s.age<:age and s.sex=:sex"; Map<String,Object> map = new HashMap<String,Object>(); map.put("age", 40); map.put("sex", "女"); List<Student> list = session.createQuery(hql) .setProperties(map) .list(); for(Student s:list){ System.out.println(s); } } //查询指定列 @Test public void testColumn(){ String hql="select s.name,s.sex from Student s"; List<Object[]> list =session.createQuery(hql).list(); for(Object[] objs:list){ System.out.println(objs[0]+"----"+objs[1]); } } //查询指定列 并且将返回值封装 @Test public void testColumnObject(){ String hql="select new Student(s.name,s.sex) from Student s"; List<Student> list = session.createQuery(hql) .list(); for(Student s:list){ System.out.println(s); } } //分页查询 @Test public void testPage(){ String hql="from Student"; List<Student> list = session.createQuery(hql) .setFirstResult(0)//(currentPage-1)*pageSize .setMaxResults(2)//pageSize .list(); for(Student s:list){ System.out.println(s); } } //uniqueResult 当结果唯一时 使用uniqueResult返回一个对象 @Test public void testUniqueResult(){ String hql="from Student where id=1"; Student stu=(Student)session.createQuery(hql).uniqueResult(); System.out.println(stu); } //聚合查询 @Test public void testCount(){ String hql="select count(id) from Student"; Long result = (Long)session.createQuery(hql).uniqueResult(); System.out.println(result); } //in子查询 @Test public void testIn(){ String hql="from Student where id in(:ids)"; List<Student> list = session.createQuery(hql) .setParameterList("ids", new Integer[]{1,2}) .list(); for(Student s:list){ System.out.println(s); } } //模糊查询 @Test public void testLike(){ String hql="from Student where name like :name"; List<Student> list = session.createQuery(hql) .setString("name", ("h"+"%")) .list(); for(Student s:list){ System.out.println(s); } } //关联查询 @Test public void testJoin(){ String hql="from Student s where s.teacher.name=:name"; List<Student> list = session.createQuery(hql) .setString("name", "nico") .list(); for(Student s:list){ System.out.println(s); } } //外联 @Test public void testLeftOuterJoin(){ String hql="select new Student(s.id,s.name,s.sex,s.age) from Student s left join s.teacher"; List<Student> list = session.createQuery(hql) .list(); for(Student s:list){ System.out.println(s); } } //命名查询 //需要在映射文件中 添加映射信息 /** * 名称在应用中唯一 <query name="findStudent"> from Student </query> */ @Test public void testNamedQuery(){ List<Student> list = session.getNamedQuery("findStudent") .list(); for(Student s:list){ System.out.println(s); } } }
4. 本地sql 查询,hql语句是hibernate提供查询语言,hibernate可以跨数据库平台,但是hibernate不能使用某个特定数据库的特定功能,使用本地sql可以解决这个问题。
public class SqlTest { private Session session=null; @Before public void setUp(){ session = HibernateUtil.getSession(); } @After public void tearDown(){ HibernateUtil.close(); } //查询所有 @Test public void testFindAll(){ String sql="select * from t_student"; List<Object[]> list = session.createSQLQuery(sql) .list(); for(Object[] objs:list){ System.out.println(objs[1]+"----"+objs[2]); } } //查询后封装为对象 @Test public void testFindObject(){ String sql="select * from t_student"; List<Student> list = session.createSQLQuery(sql) .addEntity(Student.class) .list(); for(Student s:list){ System.out.println(s); } } //查询指定列时----返回的是数组 @Test public void testColumn(){ String sql="select name,sex from t_student"; List<Object[]> list = session.createSQLQuery(sql) .list(); for(Object[] objs:list){ System.out.println(objs[0]+"----"+objs[1]); } } //条件查询 @Test public void testCondition(){ String sql="select * from t_student where age<:age"; List<Student> list=session.createSQLQuery(sql) .addEntity(Student.class) .setInteger("age", 40) .list(); for(Student s:list){ System.out.println(s); } } //本地命名查询 @Test public void testFindStudents(){ List<Object[]> list = session.getNamedQuery("findStudents") .list(); for(Object[] objs:list){ System.out.println(objs[0]+"----"+objs[1]); } } //分页 @Test public void testPage(){ String sql="select * from t_student"; List<Student> list = session.createSQLQuery(sql) .addEntity(Student.class) .setFirstResult(0) .setMaxResults(2) .list(); for(Student s:list){ System.out.println(s); } } }
5. 对象查询 Criteria 查询:
public class CriteriaTest { private Session session=null; @Before public void setUp(){ session = HibernateUtil.getSession(); } @After public void tearDown(){ HibernateUtil.close(); } //查询所有 @Test public void testFindAll(){ List<Student> list = session.createCriteria(Student.class) .list(); for(Student s:list){ System.out.println(s); } } //条件查询 @Test public void testCodition(){ List<Student> list=session.createCriteria(Student.class) .add(Restrictions.lt("age", 40)) .list(); for(Student s:list){ System.out.println(s); } } //多条件查询 @Test public void testCoditions(){ List<Student> list=session.createCriteria(Student.class) .add(Restrictions.lt("age", 40)) .add(Restrictions.eq("sex", "女")) .list(); for(Student s:list){ System.out.println(s); } } //或查询 @Test public void testCoditionOr(){ List<Student> list=session.createCriteria(Student.class) .add(Restrictions.or( Restrictions.lt("age", 40), Restrictions.eq("sex", "女"))) .list(); for(Student s:list){ System.out.println(s); } } //与或查询 @Test public void testCoditionAndOr(){ List<Student> list=session.createCriteria(Student.class) .add(Restrictions.or( Restrictions.and( Restrictions.lt("age", 40), Restrictions.eq("sex", "男")), Restrictions.eq("sex", "女"))) .list(); for(Student s:list){ System.out.println(s); } } //模糊查询 @Test public void testLike(){ List<Student> list = session.createCriteria(Student.class) .add(Restrictions.like("name", "h", MatchMode.START)) .list(); for(Student s:list){ System.out.println(s); } } //排序 @Test public void testOrder(){ List<Student> list = session.createCriteria(Student.class) .addOrder(Order.asc("age")) .list(); for(Student s:list){ System.out.println(s); } } //聚合查询 @Test public void testCount(){ Long result = (Long)session.createCriteria(Student.class) .setProjection(Projections.rowCount()) .uniqueResult(); System.out.println(result); } //分页 @Test public void testPage(){ List<Student> list = session.createCriteria(Student.class) .setFirstResult(0) .setMaxResults(2) .list(); for(Student s:list){ System.out.println(s); } } //关联查询 @Test public void testJoin(){ List<Student> list = session.createCriteria(Student.class) .createAlias("teacher", "t") .add(Restrictions.eq("t.id", 1)) .list(); for(Student s:list){ System.out.println(s); } } }