程序员

Hibernate查询方式

2017-09-29  本文已影响0人  常威爆打来福

一 对象导航查询

查询某个客户里面所有联系人过程,使用对象导航实现
实现代码

    @Test
    public void testSelect1(){
        Transaction tx = null;
        try{
            Session session = HibernateUtils.getSessionObject();
            tx= session.beginTransaction();
            //根据cid=1客户,再查询这个客户里面所有联系人
             Customer customer= session.get(Customer.class,1);
            //在查询这个客户里面所有联系人
            //直接得到客户里面联系人的set集合
            Set<LinkMan> linkman = customer.getSetlinkMan();
            System.out.println(linkman.size());
            tx.commit();
        }catch (Exception e){
            e.printStackTrace();
            tx.rollback();
        }

    }
执行结果

二 OID查询

根据id查询记录(调用session里面的get方法)

三 hql查询

1 hql: hibernate query language 提供一种查询语言,hql语言和普通sql很相似
区别:普通sql操作数据库和字段,hql操作实体类和属性

2 常用hql语句
(1)查询所有:from 实体类名称
(2)条件查询:from 实体类名称 where 属性名称 = ?
3 使用hql操作时候,使用query对象
(1)创建Query对象,写hql语句
(2)调用query对象里面的方法得到结果

4 使用
(1)查询所有
需求:查询所有的客户记录

 @Test
    public void testSelect1(){
        Transaction tx =null;
    try{
        Session session= HibernateUtils.getSessionObject();
        tx= session.beginTransaction();
        //1 创建Query对象
        Query query= session.createQuery("from entity.Customer");
        //2 调用方法得到结果
        List<Customer> list= query.list();
        for(Customer customer :list){
            System.out.println(customer.getCid()+":"+customer.getCustName());
        }
        tx.commit();
    }catch (Exception e){
        e.printStackTrace();
        tx.rollback();
    }
    }
运行结果

(2)条件查询
hql 条件查询语句写法

from 实体类名称 where 实体类属性名称=? and 实体类属性名称=?
from 实体类名称 where 实体类属性名称 like ?

where 条件查询

 @Test
    public void testSelect2(){
        Transaction tx =null;
        try{
            Session session= HibernateUtils.getSessionObject();
            tx= session.beginTransaction();
            //1创建query对象
            Query query= session.createQuery("from entity.Customer customer where customer.cid = ? and customer.custName=?");
            //2 设置条件值 向?里面设置值
            //setParameter
            // 第一个参数:int类型是?位置,?位置从0开始
            //第二个参数是具体值
            //设置第一个?值
            query.setParameter(0,2);
            //设置第二个?值
            query.setParameter(1,"百度");
            //3 调用方法得到结果
            List<Customer> list = query.list();
            for (Customer customer:list){
                System.out.println(customer.getCid()+":"+customer.getCustName());
            }
            tx.commit();
        }catch (Exception e){
            e.printStackTrace();
            tx.rollback();
        }
    }

like模糊查询

            Query query= session.createQuery("from entity.Customer customer where customer.custName like ?");
            query.setParameter(0,"%百%");
            List<Customer> list = query.list();
            for (Customer customer:list){
                System.out.println(customer.getCid()+":"+customer.getCustName());
            }

(3)排序查询
hql排序语句写法

from 实体类名称 order by 实体类属性名称 asc/desc

   @Test
    public void testSelect4(){
        Transaction tx =null;
        try{
            Session session= HibernateUtils.getSessionObject();
            tx= session.beginTransaction();
           Query query= session.createQuery("from entity.Customer customer order by customer.cid desc");
            List <Customer> list= query.list();
            for (Customer customer : list){
                System.out.println(customer.getCid()+":"+customer.getCustName());
            }
            tx.commit();
        }catch (Exception e){
            e.printStackTrace();
            tx.rollback();
        }
    }
倒序查询

(4)分页查询

图片.png
  @Test
    public void testSelect5(){
        Transaction tx =null;
        try{
            Session session= HibernateUtils.getSessionObject();
            tx= session.beginTransaction();
            //1 创建对象
           Query query= session.createQuery("from entity.Customer");
           //2 设置分页数据
            //2.1 设置开始位置
            query.setFirstResult(0);
            //2.2 设置每页记录数
            query.setMaxResults(1);

            //3 调用方法得到结果
             List<Customer> list= query.list();

             for (Customer customer :list){
                 System.out.println(customer.getCid()+":"+customer.getCustName());
             }

            tx.commit();
        }catch (Exception e){
            e.printStackTrace();
            tx.rollback();
        }
    }
执行结果

(5)投影查询

select 实体类属性名称1 ,实体类属性名称2 from 实体类名称
select 后面不能写*,不支持

 @Test
    public void testSelect6(){
        Transaction tx =null;
        try{
            Session session= HibernateUtils.getSessionObject();
            tx= session.beginTransaction();
            //1 创建对象
            Query query= session.createQuery("select custName from entity.Customer ");

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

            for (Object object :list){
                System.out.println(object);
            }

            tx.commit();
        }catch (Exception e){
            e.printStackTrace();
            tx.rollback();
        }
    }
执行结果

(6)聚集函数使用

  @Test
    public void testSelect7(){
        Transaction tx =null;
        try{
            Session session= HibernateUtils.getSessionObject();
            tx= session.beginTransaction();
            Query query=session.createQuery("select count(*) from entity.Customer");
            Object object=query.uniqueResult();
            

            System.out.println(object);
            tx.commit();
        }catch (Exception e){
            e.printStackTrace();
            tx.rollback();
        }
    }
执行结果

四 QBC查询

    @Test
    public void testSelect1(){
        Transaction tx = null;
        try{
            Session session = HibernateUtils.getSessionObject();
            tx = session.beginTransaction();
            //1.创建Criteria对象
            Criteria criteria= session.createCriteria(Customer.class);
            //2.调用方法得到结果
            List<Customer> customers=criteria.list();
            for(Customer customer :customers){
                System.out.println(customer.getCid()+":"+customer.getCustName());
            }
            tx.commit();
        }catch (Exception e){
            e.printStackTrace();
            tx.rollback();
        }
    }
执行结果

2 条件查询(没有语句,通过方法实现)

    @Test
    public void testSelect2(){
        Transaction tx = null;
        try{
            Session session = HibernateUtils.getSessionObject();
            tx = session.beginTransaction();
            //1 创建对象
            Criteria criteria= session.createCriteria(Customer.class);
            //2 使用Criteria对象里面的方法设置条件值
             //首先使用add方法,表示设置条件值
             //在add方法里面使用类的方法实现条件设置
             //类似于cid=1
            criteria.add(Restrictions.eq("cid",1));
            criteria.add(Restrictions.eq("custName","欧亚学院"));
            List<Customer> list = criteria.list();
            for(Customer customer :list){
                System.out.println(customer.getCid()+":"+customer.getCustName());
            }
            tx.commit();
        }catch (Exception e){
            e.printStackTrace();
            tx.rollback();
        }

    }

模糊查询

   @Test
    public void testSelect3(){
        Transaction tx = null;
        try{
            Session session = HibernateUtils.getSessionObject();
            tx = session.beginTransaction();
            Criteria criteria= session.createCriteria(Customer.class);

            criteria.add(Restrictions.like("custName","%百%"));
           
            List<Customer> list = criteria.list();
            for(Customer customer :list){
                System.out.println(customer.getCid()+":"+customer.getCustName());
            }
            tx.commit();
        }catch (Exception e){
            e.printStackTrace();
            tx.rollback();
        }

    }
执行结果

3 排序查询

    @Test
    public void testSelect4(){
        Transaction tx = null;
        try{
            Session session = HibernateUtils.getSessionObject();
            tx = session.beginTransaction();
            Criteria criteria= session.createCriteria(Customer.class);

            criteria.addOrder(Order.desc("cid"));

            List<Customer> list = criteria.list();
            for(Customer customer :list){
                System.out.println(customer.getCid()+":"+customer.getCustName());
            }
            tx.commit();
        }catch (Exception e){
            e.printStackTrace();
            tx.rollback();
        }

    }
执行结果

4 分页查询

 @Test
    public void testSelect5(){
        Transaction tx = null;
        try{
            Session session = HibernateUtils.getSessionObject();
            tx = session.beginTransaction();
            Criteria criteria= session.createCriteria(Customer.class);
            // 设置分页数据
             //设置开始位置
            criteria.setFirstResult(0);
             //每页显示记录数
            criteria.setMaxResults(1);
            List<Customer> list = criteria.list();
            for(Customer customer :list){
                System.out.println(customer.getCid()+":"+customer.getCustName());
            }
            tx.commit();
        }catch (Exception e){
            e.printStackTrace();
            tx.rollback();
        }

    }
执行结果

开始位置计算公示:(当前页-1)* 每页记录数

5 统计查询

 @Test
    public void testSelect6(){
        Transaction tx = null;
        try{
            Session session = HibernateUtils.getSessionObject();
            tx = session.beginTransaction();
            Criteria criteria= session.createCriteria(Customer.class);

            //设置操作
            criteria.setProjection(Projections.rowCount());
            //调用方法得到结果
            Object object= criteria.uniqueResult();
            System.out.println(object);
            tx.commit();
        }catch (Exception e){
            e.printStackTrace();
            tx.rollback();
        }

    }
执行结果

6 离线查询(和session没有关联)
servlet调用service,servrice调用dao
(1)带dao里面对数据库crud操作
(2)在dao里面使用hibernate框架,使用hibernate框架时候,调用session里面的方法

    @Test
    public void testSelect7(){
        Transaction tx = null;
        try{
            Session session = HibernateUtils.getSessionObject();
            tx = session.beginTransaction();
            //Criteria criteria= session.createCriteria(Customer.class);
            //1 创建对象
            DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Customer.class);
            // 最终在执行的时候才需要session
            Criteria criteria= detachedCriteria.getExecutableCriteria(session);
            List<Customer> list = criteria.list();
            for (Customer customer : list){
                System.out.println(customer.getCid()+":"+customer.getCustName());
            }
            tx.commit();
        }catch (Exception e){
            e.printStackTrace();
            tx.rollback();
        }
    }

五 本地sql查询
1 mysql里面多表查询
(1)内连接

/*内连接查询*/
SELECT * FROM t_customer c,t_linkman l WHERE c.cid = l.clid;

SELECT * FROM t_customer c INNER JOIN  t_linkman l ON c.cid=l.clid; 

(2)左外连接

/*左外连接*/
SELECT * FROM t_customer c LEFT OUTER JOIN t_linkman l ON c.cid=l.clid;

(3)右外连接

/*右外连接*/
SELECT * FROM t_customer c RIGHT OUTER JOIN t_linkman l ON c.cid=l.clid;

2 HQL多表查询
(1)内连接
内连接查询hql语句写法(以客户和联系人为例)

from Customer c inner join c.setLinkMan

    @Test
    public void testSelect1(){
        Transaction tx = null;
        try {
            Session session= HibernateUtils.getSessionObject();
            tx = session.beginTransaction();
            Query query = session.createQuery("from entity.Customer c inner join c.setlinkMan");
            List list= query.list();
            for (Object object:list){
                System.out.println(object.toString());
            }
            tx.commit();
        }catch (Exception e){
            e.printStackTrace();
            tx.rollback();
        }
    }

返回list,list里面每部分都是数组形式

(2)左外连接

from Customer c left outer join c.setLinkMan

左外链接返回list中每部分是数组
(3)右外连接

from Customer c right outer join c.setLinkMan

右外链接返回list中每部分是数组

(4)迫切内连接

from entity.Customer c inner join fetch c.setlinkMan

    @Test
    public void testSelect2(){
        Transaction tx = null;
        try {
            Session session= HibernateUtils.getSessionObject();
            tx = session.beginTransaction();
            Query query = session.createQuery("from entity.Customer c inner join fetch c.setlinkMan");
            List list = query.list();
            
            tx.commit();
        }catch (Exception e){
            e.printStackTrace();
            tx.rollback();
        }
    }

(5)迫切左外连接

from Customer c left outer join fetch c.setLinkMan

迫切左外链接返回每部分是对象

上一篇下一篇

猜你喜欢

热点阅读