hibernate框架学习笔记10:HQL查询详解

HQL语句中不可以出现与表有关的内容,而是对象的属性

实体类(注意配置文件):

package domain;

import java.util.HashSet;
import java.util.Set;

//客户实体
public class Customer {

    private Long cust_id;
    private String cust_name;
    private String cust_source;
    private String cust_industry;
    private String cust_level;
    private String cust_linkman;
    private String cust_phone;
    private String cust_mobile;
    //使用set集合,表达一对多关系
    private Set<LinkMan> linkMens = new HashSet<LinkMan>();

    public Set<LinkMan> getLinkMens() {
        return linkMens;
    }
    public void setLinkMens(Set<LinkMan> linkMens) {
        this.linkMens = linkMens;
    }
    public Long getCust_id() {
        return cust_id;
    }
    public void setCust_id(Long cust_id) {
        this.cust_id = cust_id;
    }
    public String getCust_name() {
        return cust_name;
    }
    public void setCust_name(String cust_name) {
        this.cust_name = cust_name;
    }
    public String getCust_source() {
        return cust_source;
    }
    public void setCust_source(String cust_source) {
        this.cust_source = cust_source;
    }
    public String getCust_industry() {
        return cust_industry;
    }
    public void setCust_industry(String cust_industry) {
        this.cust_industry = cust_industry;
    }
    public String getCust_level() {
        return cust_level;
    }
    public void setCust_level(String cust_level) {
        this.cust_level = cust_level;
    }
    public String getCust_linkman() {
        return cust_linkman;
    }
    public void setCust_linkman(String cust_linkman) {
        this.cust_linkman = cust_linkman;
    }
    public String getCust_phone() {
        return cust_phone;
    }
    public void setCust_phone(String cust_phone) {
        this.cust_phone = cust_phone;
    }
    public String getCust_mobile() {
        return cust_mobile;
    }
    public void setCust_mobile(String cust_mobile) {
        this.cust_mobile = cust_mobile;
    }
    @Override
    public String toString() {
        return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + "]";
    }
}

抽取工具类:

package utils;

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

public class HibernateUtils {
    private static SessionFactory sf;

    static{
        //1 创建,调用空参构造
        Configuration conf = new Configuration().configure();
        //2 根据配置信息,创建 SessionFactory对象
         sf = conf.buildSessionFactory();
    }

    //获得session => 获得全新session
    public static Session openSession(){
                //3 获得session
                Session session = sf.openSession();

                return session;

    }
    //获得session => 获得与线程绑定的session
    public static Session getCurrentSession(){
        //3 获得session
        Session session = sf.getCurrentSession();

        return session;
    }
}

测试类:

基本语法:

package hql;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import domain.Customer;
import utils.HibernateUtils;

//学习HQL语法
public class Demo {

    //基本语法
    @Test
    public void fun1(){
        Session session = HibernateUtils.openSession();
        Transaction tx = session.beginTransaction();
        //----------------------------------------------------
        String hql = " from  domain.Customer ";//完整写法
        String hql2 = " from  Customer "; //简单写法
        String hql3 = " from java.lang.Object "; //查询所有Object对象,即所有对象

        Query query = session.createQuery(hql);

        List list = query.list();

        System.out.println(list);
        //----------------------------------------------------
        tx.commit();
        session.close();

    }

    @Test
    //排序
    public void fun2(){
        Session session = HibernateUtils.openSession();
        Transaction tx = session.beginTransaction();
        //----------------------------------------------------
        String hql1 = " from  domain.Customer order by cust_id asc ";//完整写法升序
        String hql2 = " from  domain.Customer order by cust_id desc ";//完整写法降序

        Query query = session.createQuery(hql2);

        List list = query.list();

        System.out.println(list);
        //----------------------------------------------------
        tx.commit();
        session.close();

    }

    @Test
    //条件查询
    public void fun3(){
        Session session = HibernateUtils.openSession();
        Transaction tx = session.beginTransaction();
        //----------------------------------------------------
        String hql1 = " from  domain.Customer where cust_id =? ";//?按所有传值
        String hql2 = " from  domain.Customer where cust_id = :id ";//:按名称传值

        Query query = session.createQuery(hql2);

        //query.setParameter(0, 5l);
        query.setParameter("id", 5l);

        List list = query.list();

        System.out.println(list);
        //----------------------------------------------------
        tx.commit();
        session.close();

    }

    @Test
    //分页查询
    public void fun4(){
        Session session = HibernateUtils.openSession();
        Transaction tx = session.beginTransaction();
        //----------------------------------------------------
        String hql1 = " from  domain.Customer  ";//完整写法

        Query query = session.createQuery(hql1);

        //类似SQL中的limit ?,?
        // (当前页数-1)*每页条数
        query.setFirstResult(0);
        query.setMaxResults(2);

        List list = query.list();

        System.out.println(list);
        //----------------------------------------------------
        tx.commit();
        session.close();

    }

    @Test
    //统计查询
    //count    计数
    //sum     求和
    //avg    平均数
    //max
    //min
    public void fun5(){
        Session session = HibernateUtils.openSession();
        Transaction tx = session.beginTransaction();
        //----------------------------------------------------
        String hql1 = " select count(*) from  domain.Customer  ";
        String hql2 = " select sum(cust_id) from  domain.Customer  ";
        String hql3 = " select avg(cust_id) from  domain.Customer  ";
        String hql4 = " select max(cust_id) from  domain.Customer  ";
        String hql5 = " select min(cust_id) from  domain.Customer  ";

        Query query = session.createQuery(hql3);

        Number number  = (Number) query.uniqueResult();

        System.out.println(number);
        //----------------------------------------------------
        tx.commit();
        session.close();

    }

    @Test
    //投影查询
    //查询对象某一个属性
    public void fun6(){
        Session session = HibernateUtils.openSession();
        Transaction tx = session.beginTransaction();
        //----------------------------------------------------
        String hql1 = " select cust_name from  domain.Customer  ";
        String hql2 = " select cust_name,cust_id from  domain.Customer  ";
        String hql3 = " select new Customer(cust_id,cust_name) from  domain.Customer  ";
        //如果实体类有相应构造方法,也可以执行

        Query query = session.createQuery(hql2);

        List list = query.list();

        System.out.println(list);

        //----------------------------------------------------
        tx.commit();
        session.close();

    }
}

多表查询:

再创建一个实体类:

package domain;

//联系人实体
public class LinkMan {
    private Long lkm_id;
    private Character lkm_gender;
    private String lkm_name;
    private String lkm_phone;
    private String lkm_email;
    private String lkm_qq;
    private String lkm_mobile;
    private String lkm_memo;
    private String lkm_position;

    // 表达多对一关系
    private Customer customer;

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public Long getLkm_id() {
        return lkm_id;
    }

    public void setLkm_id(Long lkm_id) {
        this.lkm_id = lkm_id;
    }

    public Character getLkm_gender() {
        return lkm_gender;
    }

    public void setLkm_gender(Character lkm_gender) {
        this.lkm_gender = lkm_gender;
    }

    public String getLkm_name() {
        return lkm_name;
    }

    public void setLkm_name(String lkm_name) {
        this.lkm_name = lkm_name;
    }

    public String getLkm_phone() {
        return lkm_phone;
    }

    public void setLkm_phone(String lkm_phone) {
        this.lkm_phone = lkm_phone;
    }

    public String getLkm_email() {
        return lkm_email;
    }

    public void setLkm_email(String lkm_email) {
        this.lkm_email = lkm_email;
    }

    public String getLkm_qq() {
        return lkm_qq;
    }

    public void setLkm_qq(String lkm_qq) {
        this.lkm_qq = lkm_qq;
    }

    public String getLkm_mobile() {
        return lkm_mobile;
    }

    public void setLkm_mobile(String lkm_mobile) {
        this.lkm_mobile = lkm_mobile;
    }

    public String getLkm_memo() {
        return lkm_memo;
    }

    public void setLkm_memo(String lkm_memo) {
        this.lkm_memo = lkm_memo;
    }

    public String getLkm_position() {
        return lkm_position;
    }

    public void setLkm_position(String lkm_position) {
        this.lkm_position = lkm_position;
    }

}

测试类:

package hql;

import java.util.Arrays;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import domain.Customer;
import utils.HibernateUtils;

//学习HQL语法(不常用) - 多表查询语法
public class Demo2 {
    //原生SQL中:
    // 交叉连接-笛卡尔积(避免)
//        select * from A,B
    // 内连接
//        |-隐式内连接
//            select * from A,B  where b.aid = a.id
//        |-显式内连接
//            select * from A inner join B on b.aid = a.id
    // 外连接
//        |- 左外
//            select * from A left [outer] join B on b.aid = a.id
//        |- 右外
//            select * from A right [outer] join B on b.aid = a.id
//---------------------------------------------------------------------
//HQL的多表查询
        //内连接(迫切)
        //外连接
//            |-左外(迫切)
//            |-右外(迫切)

    @Test
    //HQL 内连接 => 将连接的两端对象分别返回.放到数组中.
    public void fun1(){
        Session session = HibernateUtils.openSession();
        Transaction tx = session.beginTransaction();
        //----------------------------------------------------
        String hql = " from Customer c inner join c.linkMens ";
        //c相当于别名
        Query query = session.createQuery(hql);

        List<Object[]> list = query.list();

        for(Object[] arr : list){
            System.out.println(Arrays.toString(arr));
        }
        //----------------------------------------------------
        tx.commit();
        session.close();

    }

    @Test
    //HQL 迫切内连接 => 帮我们进行封装.返回值就是一个对象
    public void fun2(){
        Session session = HibernateUtils.openSession();
        Transaction tx = session.beginTransaction();
        //----------------------------------------------------
        String hql = " from Customer c inner join fetch c.linkMens ";

        Query query = session.createQuery(hql);

        List<Customer> list = query.list();

        System.out.println(list);
        //----------------------------------------------------
        tx.commit();
        session.close();

    }

    @Test
    //HQL 左外连接 => 将连接的两端对象分别返回.放到数组中.
    public void fun3(){
        Session session = HibernateUtils.openSession();
        Transaction tx = session.beginTransaction();
        //----------------------------------------------------
        String hql = " from Customer c left join c.linkMens ";

        Query query = session.createQuery(hql);

        List<Object[]> list = query.list();

        for(Object[] arr : list){
            System.out.println(Arrays.toString(arr));
        }
        //----------------------------------------------------
        tx.commit();
        session.close();

    }
    @Test
    //HQL 右外连接 => 将连接的两端对象分别返回.放到数组中.
    public void fun4(){
        Session session = HibernateUtils.openSession();
        Transaction tx = session.beginTransaction();
        //----------------------------------------------------
        String hql = " from Customer c right join c.linkMens ";

        Query query = session.createQuery(hql);

        List<Object[]> list = query.list();

        for(Object[] arr : list){
            System.out.println(Arrays.toString(arr));
        }
        //----------------------------------------------------
        tx.commit();
        session.close();

    }

}

补上内连接与外连接区别:

原文地址:https://www.cnblogs.com/xuyiqing/p/8454835.html

时间: 2024-10-29 17:46:23

hibernate框架学习笔记10:HQL查询详解的相关文章

hibernate框架学习笔记2:配置文件详解

实体类: package domain; public class Customer { private Long cust_id; private String cust_name; private String cust_source; private String cust_industry; private String cust_level; private String cust_linkman; private String cust_phone; private String c

struts2框架学习笔记2:配置详解

核心配置文件: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <

mybatis学习笔记(10)-一对一查询

mybatis学习笔记(10)-一对一查询 mybatis学习笔记10-一对一查询 resultType实现 resultMap实现 resultType和resultMap实现一对一查询小结 本文使用两种方式(resultType和resultMap)实现一对一查询,查询订单信息,关联查询创建订单的用户信息 resultType实现 sql语句 确定查询的主表:订单表 确定查询的关联表:用户表 关联查询使用内连接?还是外连接? 因为orders表中有一个外键(user_id),通过外键关联查询

hibernate框架学习笔记7:HQL查询、Criteria查询

HQL查询:hibernate独有的查询语言 适用于不复杂的多表查询 示例: 实体类: package domain; public class Customer { private Long cust_id; private String cust_name; private String cust_source; private String cust_industry; private String cust_level; private String cust_linkman; priv

j2ee开发之hibernate框架学习笔记

hibernate框架技术重点学习笔记 1.针对不同的数据库,有不同的数据库实现类,使其符号对应的数据库? mysqlDaoImpl oracleDaoImpl ... ... 2.对象和表记录的转换存在着转换问题->orm,对象和关系的一种映射 3.框架:解决一种问题的方案集合! 4..配置文件 Xxx.cfg.xml  主配置文件 xxx.hbm.xml映射文件: 映射基础 普通属性 主键 集合属性 关联关系:一对多  多对一 多对多  一对一 继承结构 5.其他特性:数据库连接池  懒加载

hibernate 框架学习笔记---网上摘抄的一个非常好的例子

编写Spring+Hibernate框架下的应用,总是离不了编写一个通用的泛型GenericHibernateDao.查阅了网上不少的GenericHibernateDao实现,归纳整理为如下实现,供后续编码参考. 首先定义接口泛型DAO接口 GenericDao package com.th.huz; import java.io.Serializable;import java.util.Collection;import java.util.Iterator;import java.uti

hibernate框架学习笔记1:搭建与测试

hibernate框架属于dao层,类似dbutils的作用,是一款ORM(对象关系映射)操作 使用hibernate框架好处是:操作数据库不需要写SQL语句,使用面向对象的方式完成 这里使用eclipse工具搭建: 官网下载:https://sourceforge.net/projects/hibernate/files/hibernate-orm/5.0.7.Final/ 下载.zip文件后解压: lib文件夹下的required文件夹内的jar包为必须包: 另外还需要MySQL的驱动包:

C#学习笔记二: C#类型详解

前言 这次分享的主要内容有五个, 分别是值类型和引用类型, 装箱与拆箱,常量与变量,运算符重载,static字段和static构造函数. 后期的分享会针对于C#2.0 3.0 4.0 等新特性进行. 再会有三篇博客  这个系列的就会结束了. 也算是自己对园子中@Learning Hard出版的<<C#学习笔记>>的一个总结了. 博客内容基本上都是白天抽空在公司写好的了, 但是由于公司内部网络不能登录博客园所以只能够夜晚拿回来修改,  写的不好或者不对的地方也请各位大神指出. 在下感

[学习笔记]struts.xml配置详解(所有基本配置,包括通配符)

1.<include> 利用include标签,可以将一个struts.xml配置文件分割成多个配置文件,然后在struts.xml中使用<include>标签引入其他配置文件.比如一个网上购物程序,可以把用户配置.商品配置.订单配置分别放在3个配置文件user.xml.goods.xml和order.xml中,然后在struts.xml中将这3个配置文件引入: struts.xml: [html] view plaincopy <?xml version="1.0