HQL查询(示例)

hql查询是操作实体类和属性实现查询。查询语句必须是实体类的方法名。

(1)sql查询是表以及字段,不区分大小写,也叫做结构化的查询语句;

(2)HQL查询的是对象以及对象的属性,区分大小写的。

步骤:

(1)创建Query查询对象,编写hql语句;

(2)调用Query对象里面的方法得到结果。

案例:

卡号实体类:

package com.liuyongqi.MavenHibernateDemo7.entity;

import java.io.Serializable;
/**
 * 卡号实体类
 * @author Administrator
 * @data   2018年8月6日
 * @time   上午11:51:34
 */
public class Card implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -3229041685479626246L;
	private Integer pid;					//卡号编号
	private String cname;					//卡号名称
	private Person person;					//个人对象
	
	public Card() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Card(String cname, Integer pid) {
		super();
		this.cname = cname;
		this.pid = pid;
	}
	public String getCname() {
		return cname;
	}
	public void setCname(String cname) {
		this.cname = cname;
	}
	public Integer getPid() {
		return pid;
	}
	public void setPid(Integer pid) {
		this.pid = pid;
	}	
	public Person getPerson() {
		return person;
	}
	public void setPerson(Person person) {
		this.person = person;
	}
	@Override
	public String toString() {
		return "Card [pid=" + pid + ", cname=" + cname + ", person=" + person + "]";
	}
	
	
}	

个人实体类:

package com.liuyongqi.MavenHibernateDemo7.entity;

import java.io.Serializable;
/**
 * 个人实体类
 * @author Administrator
 * @data   2018年8月6日
 * @time   上午11:49:17
 */
public class Person implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -2013313014914594043L;
	private Integer pid;				//个人编号
	private String pname;				//个人名称
	private Card card;					//卡号对象
	
	public Person() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Person(String pname) {
		super();
		this.pname = pname;
	}
	public Person(Integer pid, String pname) {
		super();
		this.pid = pid;
		this.pname = pname;
	}
	public Integer getPid() {
		return pid;
	}
	public void setPid(Integer pid) {
		this.pid = pid;
	}
	public String getPname() {
		return pname;
	}
	public void setPname(String pname) {
		this.pname = pname;
	}	
	public Card getCard() {
		return card;
	}
	public void setCard(Card card) {
		this.card = card;
	}
	@Override
	public String toString() {
		return "Person [pid=" + pid + ", pname=" + pname + "]";
	}
	
	
}

Card.hbm.xml:





    
        
            
            
            	person
            
        
        
            
        
        
    

Person.hbm.xml:





    
        
            
            
        
        
            
        
        
    

SessionFactoryUtil工具类:

package com.liuyongqi.MavenHibernateDemo7.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 * 提供了开启和关闭session的方法
 * @author Administrator
 * @data   2018年8月1日
 * @time   下午3:32:56
 */
public class SessionFactoryUtil {
	//ThreadLocal为每个线程提供一个单独的存储空间
	private static ThreadLocal threadLocal=new ThreadLocal();
	
	//私有化静态变量,静态变量只实例化一次
	private static SessionFactory sessionFactory;
	//实例化一次sessionFactory
	static {
		Configuration configure = new Configuration().configure();
		sessionFactory=configure.buildSessionFactory();
	}
	//私有化构造方法
	private SessionFactoryUtil() {
		
	}
	//打开session的方法
	public static Session openSession() {
		//从ThreadLocal中拿取一个session
		Session session = threadLocal.get();
		if(null==session) {
			//sessionFactory打开一个session
			session=sessionFactory.openSession();
			//把session又放入ThreadLocal中
			threadLocal.set(session);
		}
		return session;
	}
	
	//关闭资源
	public static void closeSession() {
		//从ThreadLocal中拿取一个session
		Session session = threadLocal.get();
		if(null==session) {
			if(session.isOpen()) {
				//关闭session
				session.close();
			}
			threadLocal.set(null);
		}		
	}
	
	public static void main(String[] args) {
		Session session = openSession();
		System.out.println(session);
		System.out.println("ok");
	}
	
}

hql测试类:

(1)查询所有

                Session session = SessionFactoryUtil.openSession();
		String hql="from Person";
		Query query = session.createQuery(hql);
		List list = query.list();
		for (Person person : list) {
			System.out.println(person);
		}

(2)条件查询
     

        Session session = SessionFactoryUtil.openSession();
        String hql = "from Person where pname=?";  
        Query query = session.createQuery(hql);
        query.setParameter(0, "王五");
        List list=query.list();
        System.out.println(list);

模糊查询:

                Session session = SessionFactoryUtil.openSession();
                String hql="from Person where pname like'%王%'";
		Query query = session.createQuery(hql);
		List list=query.list();
		System.out.println(list);

(3)排序查询:

                Session session = SessionFactoryUtil.openSession()
                String hql="from Person order by pname desc ";
		Query query = session.createQuery(hql);
		List list=query.list();
		System.out.println(list);

(4)分页查询:

                Session session = SessionFactoryUtil.openSession();
                String hql="from Person";
		Query query = session.createQuery(hql);
		query.setFirstResult(0);
		query.setMaxResults(2);
		List list=query.list();
		System.out.println(list);

(5)投影查询:

                Session session = SessionFactoryUtil.openSession();
                String hql="select pname from Person";
		Query query = session.createQuery(hql);
		List list=query.list();
		System.out.println(list);

(6)聚集查询:

                Session session = SessionFactoryUtil.openSession();
                String hql="select count(*) from Person";
		Query query = session.createQuery(hql);
		Object obj = query.uniqueResult();
		int count =Integer.parseInt(obj.toString());
		System.out.println(count);

(7)分组查询:

                Session session = SessionFactoryUtil.openSession();
                String hql="from Person group by pname having pid>1";
		Query query = session.createQuery(hql);
		List list=query.list();
		System.out.println(list);

(8)子查询:

                Session session = SessionFactoryUtil.openSession();
                String hql="from Card where pid in(from Person where pname like '%王%')";
		Query query = session.createQuery(hql);
		List list=query.list();
		System.out.println(list);

(9)多表联查:

                Session session = SessionFactoryUtil.openSession();
                String hql="from Person p,Card c where p.pid=c.pid";
		Query query e= session.createQuery(hql);
		List list=qury.list();
		for (Object object : list) {
			Object[] obj=(Object[]) object;
			for (Object object2 : obj) {
				System.out.println(object2);
			}
			System.out.println("------------");
		}

今天的测试就到这里了,希望大家看完以后自己测试一下

如果大家想浏览我的下一篇文章,请留言

此文章属于原创,不准随意转载:https://blog.csdn.net/LYQ2332826438

 

你可能感兴趣的:(java,Hibernate,代码示例,框架)