五、持久层框架(Hibernate)

一、分页查询

  使用Criteria进行分页查询,无论是使用Oracle,MySQL,NoSQL,DB2,分页查询的代码写法都相同。

分页查询代码示例:

package com.demo.test;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;

import com.demo.pojo.Product;

public class TestHibernate{
    public static void main(String[] args){
        SessionFactory sf=new Configuration().configure().buildSessionFactory();
        Session session=sf.openSession();
        session.beginTransaction();
        
        String name="demo";
        Criteria c=session.createCriteria(Produt.class);
        c.add(Restriction.like("name","%"+name+"%"));
        c.setFirstResult(2);//从第二条数据开始
        c.setMaxResults(5);//一共查询5条数据
        
        List<Product> list=c.list();
        for(Product p:list){
            System.out.println(p.getName()+"\t");
        }
        
        session.getTransaction().commit();
        session.close();
        sf.close();
    }
}
View Code

二、Hibernate获取session的两种方式

1、openSession和getCurrentSession

区别:

1.1、获取的是否是同一个session对象

openSession:每次会得到新的Session对象

getCurrentSession:在同一个线程中,每次都获取相同的Session对象。在不同的线程中,获取的是不同的Session对象。

1.2、事务提交的必要性

openSession:只有在增,删,改的时候需要提交事务,查询不要提交事务

getCurrentSession:所有操作必须放在事务中进行,并且提交事务后,session会自动关闭。

===》在同一个线程中的代码如下:

package com.demo.test;

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

public class TestHibernate{
    public static void main(String[] args){
        SessionFactory sf=new Configuration().configure().buildSessionFactory();
        
        Session session1=sf.openSession();
        Session session2=sf.openSession();
        System.out.println(session1==session2);//flase
        session1.close();
        session2.close();
        Session session3=sf.getCurrentSession();
        Session session4=sf.getCurrentSession();
        System.out.println(session3==session4);//true
        sessjon3.close();
        //session4.close();只能关闭一次,不能再次进行关闭
        sf.close();
    }
}
View Code

===》在不同的线程中:

package com.demo.test;

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

public class TestHibernate{
    public static void main(String[] args){
        SessionFactory sf=new Configuration().configure().buildSessionFactory();
        Session session1;
        Session session2;
        
        Thread thread1=new Thread(){
            public void run(){
                session1=sf.getCurrentSession();
            }
        };
        thread1.start();
        
        Thread thread2=new Thread(){
            public void run(){
                session2=sf.getCurrentSession();
            }
        };
        thread2.start();
        thread1.join();
        thread2.join();
        
        System.out.println(session1==session2);
    }
}
View Code

 

转载于:https://www.cnblogs.com/drq1/p/8515537.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值