Hibernate中的查询策略

xiaoxiao2022-07-05  10


某平台价值19860元的编程课程资料免费领取【点我领取】


 Hibernate的基本策略查询。

创建pojo.User 类

package com.pojo; import java.util.HashSet; import java.util.Set; /** * User entity. @author MyEclipse Persistence Tools */ public class User implements java.io.Serializable { // Fields private String username; private String password; private Set goodses = new HashSet(0); // Constructors /** default constructor */ public User() { } /** minimal constructor */ public User(String username, String password) { this.username = username; this.password = password; } /** full constructor */ public User(String username, String password, Set goodses) { this.username = username; this.password = password; this.goodses = goodses; } // Property accessors public String getUsername() { return this.username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return this.password; } public void setPassword(String password) { this.password = password; } public Set getGoodses() { return this.goodses; } public void setGoodses(Set goodses) { this.goodses = goodses; } }

 

一、投影查询

@org.junit.Test public void testTouying(){ Session session = HibernateSessionFactory.getSession(); String hql = "select username ,password from User" ; Query query = session.createQuery(hql); List<Object[]> list = query.list(); for (Object[] objects : list) { System.out.println(objects[0]+"\t"+objects[1]); } } @org.junit.Test public void testTouying2(){ Session session = HibernateSessionFactory.getSession(); String hql = "select new User(username,password) from User" ; Query query = session.createQuery(hql); List<User> list = query.list(); for (User user : list) { System.out.println(user.getUsername()+"\t"+user.getPassword()); } }

上面有两种投影查询。一般使用第二种。

二、条件查询

@org.junit.Test public void testFZ(){ Session session = HibernateSessionFactory.getSession() ; String hql = "from User where username = ?" ; Query query = session.createQuery(hql) ; query.setParameter(0, "刘强") ; List<User> list = query.list() ; for (User user : list) { System.out.println(user.getUsername()+"\t"+user.getPassword()) ; } }

条件查询和sql 类似,但是hql面向的是对象。

三、分页查询

@org.junit.Test public void testFenye(){ Session session = HibernateSessionFactory.getSession(); String hql = "from User" ; Query createQuery = session.createQuery(hql); createQuery.setFirstResult(0).setMaxResults(3); List<User> list = createQuery.list(); for (User user : list) { System.out.println(user.getUsername()); } }

由于Hibernate不能通用得到页码和数量,所以Hibernate封装了mysql中的limit,setFirstResult()从那个开始,setMaxResults()得到多少个结果。

四、迫切查询(多表查询)

@org.junit.Test public void testPoQie(){ Session session = HibernateSessionFactory.getSession(); String hql = "from User u join u.goodses" ; Query query = session.createQuery(hql); List<Object[]> list = query.list() ; for (Object[] objects : list) { System.out.println(objects[0]+"\t"+objects[1]); } } @org.junit.Test public void testPoQie2(){ Session session = HibernateSessionFactory.getSession(); String hql = "from User u join fetch u.goodses" ; Query query = session.createQuery(hql); List<User> list = query.list() ; for (User user : list) { System.out.println(user.getUsername()+"\t"+user.getPassword()+"\t"+user.getGoodses()); } }

 通常用第二种。

 

转载请注明原文地址: https://www.6miu.com/read-4968464.html

最新回复(0)