码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2018-02-18 18:20:13      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:session   parameter   对象   实体类   iter   fun   ram   count   创建   

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;
    private String cust_phone;
    private String cust_mobile;
    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 + "]";
    }
}
View Code

自定义工具类:

技术分享图片
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;
    }    
}
View Code

测试类:

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(){
        //1 获得session
        Session session = HibernateUtils.openSession();
        //2 控制事务
        Transaction tx = session.beginTransaction();
        //3执行操作
        //-------------------------------------------
        //1> 书写HQL语句
        //String hql = " from domain.Customer ";
        String hql = " from Customer "; // 如果查询所有Customer对象,可省略全名
        //2> 根据HQL语句创建查询对象
        Query query = session.createQuery(hql);
        //3> 根据查询对象获得查询结果
        List<Customer> list = query.list();    // 返回list结果
        //query.uniqueResult();//接收唯一的查询结果
        
        System.out.println(list);
        //-------------------------------------------
        //4提交事务.关闭资源
        tx.commit();
        session.close();
    }
    
    @Test
    //条件查询
    //HQL语句中,不可能出现任何数据库相关的信息的
    //不会出现数据库的表名类名字段名,使用的是实体类的属性名
    public void fun2(){
        //1 获得session
        Session session = HibernateUtils.openSession();
        //2 控制事务
        Transaction tx = session.beginTransaction();
        //3执行操作
        //-------------------------------------------
        //1> 书写HQL语句
        String hql = " from Customer where cust_id = 1 "; // 查询所有Customer对象
        //2> 根据HQL语句创建查询对象
        Query query = session.createQuery(hql);
        //3> 根据查询对象获得查询结果
        Customer c = (Customer) query.uniqueResult();
        
        System.out.println(c);
        //-------------------------------------------
        //4提交事务.关闭资源
        tx.commit();
        session.close();
        
        
    }
    
    @Test
    //条件查询
    //问号占位符
    public void fun3(){
        //1 获得session
        Session session = HibernateUtils.openSession();
        //2 控制事务
        Transaction tx = session.beginTransaction();
        //3执行操作
        //-------------------------------------------
        //1> 书写HQL语句
        String hql = " from Customer where cust_id = ? "; // 查询所有Customer对象
        //2> 根据HQL语句创建查询对象
        Query query = session.createQuery(hql);
        //设置参数
        //query.setLong(0, 1l);
        query.setParameter(0, 1l);
        //3> 根据查询对象获得查询结果
        Customer c = (Customer) query.uniqueResult();
        
        System.out.println(c);
        //-------------------------------------------
        //4提交事务.关闭资源
        tx.commit();
        session.close();
        
        
    }
    
    @Test
    //条件查询
    //命名占位符
    public void fun4(){
        //1 获得session
        Session session = HibernateUtils.openSession();
        //2 控制事务
        Transaction tx = session.beginTransaction();
        //3执行操作
        //-------------------------------------------
        //1> 书写HQL语句
        String hql = " from Customer where cust_id = :cust_id "; // 查询所有Customer对象
        //2> 根据HQL语句创建查询对象
        Query query = session.createQuery(hql);
        //设置参数
        query.setParameter("cust_id", 1l);
        //3> 根据查询对象获得查询结果
        Customer c = (Customer) query.uniqueResult();
        
        System.out.println(c);
        //-------------------------------------------
        //4提交事务.关闭资源
        tx.commit();
        session.close();
        
        
    }
    
    @Test
    //分页查询
    public void fun5(){
        //1 获得session
        Session session = HibernateUtils.openSession();
        //2 控制事务
        Transaction tx = session.beginTransaction();
        //3执行操作
        //-------------------------------------------
        //1> 书写HQL语句
        String hql = " from Customer  "; // 查询所有Customer对象
        //2> 根据HQL语句创建查询对象
        Query query = session.createQuery(hql);
        //设置分页信息 limit ?,?
        query.setFirstResult(0);
        query.setMaxResults(1);
        //3> 根据查询对象获得查询结果
        List<Customer> list =  query.list();
        
        System.out.println(list);
        //-------------------------------------------
        //4提交事务.关闭资源
        tx.commit();
        session.close();
        
        
    }
}

 

Criteria查询:

hibernate独创的,无语句面向对象查询

适用于单表查询

示例:

package criteria;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.junit.Test;

import domain.Customer;
import utils.HibernateUtils;

//测试Criteria查询
public class Demo {

    @Test
    //基本查询
    public void fun1(){
        //1 获得session
        Session session = HibernateUtils.openSession();
        //2 控制事务
        Transaction tx = session.beginTransaction();
        //3执行操作
        //-------------------------------------------
        
        //查询所有的Customer对象
        Criteria criteria = session.createCriteria(Customer.class);
        
        List<Customer> list = criteria.list();
        
        System.out.println(list);
        
//        Customer c = (Customer) criteria.uniqueResult();
        
        //-------------------------------------------
        //4提交事务.关闭资源
        tx.commit();
        session.close();
        
        
    }
    
    @Test
    //条件查询
    //HQL语句中,不可能出现任何数据库相关的信息的
    // >                 gt
    // >=                ge
    // <                lt
    // <=                le
    // ==                eq
    // !=                ne
    // in                in
    // between and        between
    // like             like
    // is not null         isNotNull
    // is null            isNull
    // or                or
    // and                and
    public void fun2(){
        //1 获得session
        Session session = HibernateUtils.openSession();
        //2 控制事务
        Transaction tx = session.beginTransaction();
        //3执行操作
        //-------------------------------------------
        //创建criteria查询对象
        Criteria criteria = session.createCriteria(Customer.class);
        //添加查询参数 => 查询cust_id为1的Customer对象
        criteria.add(Restrictions.gt("cust_id", 1l));
        //执行查询获得结果
        Customer c = (Customer) criteria.uniqueResult();
        System.out.println(c);
        //-------------------------------------------
        //4提交事务.关闭资源
        tx.commit();
        session.close();
        
        
    }
    
    
    
    @Test
    //分页查询
    public void fun3(){
        //1 获得session
        Session session = HibernateUtils.openSession();
        //2 控制事务
        Transaction tx = session.beginTransaction();
        //3执行操作
        //-------------------------------------------
        //创建criteria查询对象
        Criteria criteria = session.createCriteria(Customer.class);
        //设置分页信息 limit ?,?
        criteria.setFirstResult(1);
        criteria.setMaxResults(2);
        //执行查询
        List<Customer> list = criteria.list();
        
        System.out.println(list);
        //-------------------------------------------
        //4提交事务.关闭资源
        tx.commit();
        session.close();
        
        
    }
    
    @Test
    //查询总记录数
    public void fun4(){
        //1 获得session
        Session session = HibernateUtils.openSession();
        //2 控制事务
        Transaction tx = session.beginTransaction();
        //3执行操作
        //-------------------------------------------
        //创建criteria查询对象
        Criteria criteria = session.createCriteria(Customer.class);
        //设置查询的聚合函数 => 总行数
        criteria.setProjection(Projections.rowCount());
        //执行查询
        Long count = (Long) criteria.uniqueResult();
        
        System.out.println(count);
        //-------------------------------------------
        //4提交事务.关闭资源
        tx.commit();
        session.close();
        
        
    }
}

 

 

原生SQL查询:

适用于相当复杂的业务

package sql;

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

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

import domain.Customer;
import utils.HibernateUtils;

//测试原生SQL查询
public class Demo {

    @Test
    //基本查询
    public void fun1(){
        //1 获得session
        Session session = HibernateUtils.openSession();
        //2 控制事务
        Transaction tx = session.beginTransaction();
        //3执行操作
        //-------------------------------------------
        //1 书写sql语句
        String sql = "select * from cst_customer";
        
        //2 创建sql查询对象
        SQLQuery query = session.createSQLQuery(sql);
        
        //3 调用方法查询结果
        List<Object[]> list = query.list();
        //query.uniqueResult();
        
        for(Object[] objs : list){
            System.out.println(Arrays.toString(objs));
        }
        
        //-------------------------------------------
        //4提交事务.关闭资源
        tx.commit();
        session.close();
        
        
    }
    
    @Test
    //基本查询
    public void fun2(){
        //1 获得session
        Session session = HibernateUtils.openSession();
        //2 控制事务
        Transaction tx = session.beginTransaction();
        //3执行操作
        //-------------------------------------------
        //1 书写sql语句
        String sql = "select * from cst_customer";
        
        //2 创建sql查询对象
        SQLQuery query = session.createSQLQuery(sql);
        //指定将结果集封装到哪个对象中
        query.addEntity(Customer.class);
        
        //3 调用方法查询结果
        List<Customer> list = query.list();
        
        System.out.println(list);
        //-------------------------------------------
        //4提交事务.关闭资源
        tx.commit();
        session.close();
        
        
    }
    
    @Test
    //条件查询
    public void fun3(){
        //1 获得session
        Session session = HibernateUtils.openSession();
        //2 控制事务
        Transaction tx = session.beginTransaction();
        //3执行操作
        //-------------------------------------------
        //1 书写sql语句
        String sql = "select * from cst_customer where cust_id = ? ";
        
        //2 创建sql查询对象
        SQLQuery query = session.createSQLQuery(sql);
        
        query.setParameter(0, 1l);
        //指定将结果集封装到哪个对象中
        query.addEntity(Customer.class);
        
        //3 调用方法查询结果
        List<Customer> list = query.list();
        
        System.out.println(list);
        //-------------------------------------------
        //4提交事务.关闭资源
        tx.commit();
        session.close();
        
        
    }
    
    @Test
    //分页查询
    public void fun4(){
        //1 获得session
        Session session = HibernateUtils.openSession();
        //2 控制事务
        Transaction tx = session.beginTransaction();
        //3执行操作
        //-------------------------------------------
        //1 书写sql语句
        String sql = "select * from cst_customer  limit ?,? ";
        
        //2 创建sql查询对象
        SQLQuery query = session.createSQLQuery(sql);
        
        query.setParameter(0, 0);
        query.setParameter(1, 1);
        //指定将结果集封装到哪个对象中
        query.addEntity(Customer.class);
        
        //3 调用方法查询结果
        List<Customer> list = query.list();
        
        System.out.println(list);
        //-------------------------------------------
        //4提交事务.关闭资源
        tx.commit();
        session.close();
        
        
    }
}

 

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

标签:session   parameter   对象   实体类   iter   fun   ram   count   创建   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!