博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
二级缓存&批量处理&管理session
阅读量:5354 次
发布时间:2019-06-15

本文共 6476 字,大约阅读时间需要 21 分钟。

二级缓存

package helloworld;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.hibernate.service.ServiceRegistry;import org.hibernate.service.ServiceRegistryBuilder;import org.junit.After;import org.junit.Before;import org.junit.Test;public class TestHello {		private SessionFactory sessionFactory;	private Session sess;	private Transaction trans;		@Before	public void before(){		Configuration configuration = new Configuration().configure();		ServiceRegistry registry = new ServiceRegistryBuilder()										.applySettings(configuration.getProperties())										.buildServiceRegistry();		sessionFactory = configuration.buildSessionFactory(registry);		sess = sessionFactory.openSession();		trans = sess.beginTransaction();	}		@After	public void after(){		trans.commit();		sess.close();		sessionFactory.close();	}	/**1,二级缓存使用方法(helloworld)	 * 导入对应的ehcache的jar包	 * 写ehcache.xml	 * 在cfg.xml中配置(三条)	 * 
true
*
org.hibernate.cache.ehcache.EhCacheRegionFactory
*
* * * 类级别的缓存 * */ @Test public void testSecondCache() { Employee e = (Employee) sess.get(Employee.class, 2); System.out.println(e.getName()); trans.commit(); sess.close(); sess = sessionFactory.openSession(); trans = sess.beginTransaction(); Employee e2 = (Employee) sess.get(Employee.class, 2); System.out.println(e2.getName()); } /** * 集合级别的缓存(需要建立在集合中的元素的类在二级缓存中的前提上,否则只是缓存了对应的id,会多出n多条查询) * * */ @Test public void testCollectionCache() { Department d = (Department) sess.get(Department.class, 1); System.out.println(d.getSet().size()); trans.commit(); sess.close(); sess = sessionFactory.openSession(); trans = sess.beginTransaction(); Department d2 = (Department) sess.get(Department.class, 1); System.out.println(d2.getSet().size()); } /** * 查询缓存 * 默认情况下, 设置的缓存对 HQL 及 QBC 查询时无效的, 但可以通过以下方式使其是有效的 I. 在 hibernate 配置文件中声明开启查询缓存
true
II. 调用 Query 或 Criteria 的 setCacheable(true) 方法 III. 查询缓存依赖于二级缓存 * */ @Test public void testQueryCache() { } /** * 时间戳缓存 * */ /** * Query 接口的 iterate() 方法 * */}

ehcache.xml

管理session

HibernateUtil

package manageSession;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import org.hibernate.service.ServiceRegistry;import org.hibernate.service.ServiceRegistryBuilder;public class HibernateUtil {	private HibernateUtil() {	}	private static HibernateUtil instance = new HibernateUtil();	public static HibernateUtil getInstance() {		return instance;	}	private SessionFactory sessionFactory;	public SessionFactory getSessionFactory() {		if (sessionFactory == null) {			Configuration configuration = new Configuration().configure();			ServiceRegistry registry = new ServiceRegistryBuilder()					.applySettings(configuration.getProperties())					.buildServiceRegistry();			sessionFactory = configuration.buildSessionFactory(registry);		}		return sessionFactory;	}	public Session getSession(){		return instance.getSessionFactory().getCurrentSession();	}}

EmployeeDao

package manageSession;import org.hibernate.Session;import helloworld.Employee;public class EmployeeDao {	/**	 * 内部获取 Session 对象	 * 获取和当前线程绑定的 Session 对象	 * 1. 不需要从外部传入 Session 对象	 * 2. 多个 DAO 方法也可以使用一个事务!	 * */	public void save(Employee emp){		Session session = HibernateUtil.getInstance().getSession();		System.out.println(session.hashCode());				session.save(emp);	}}

TestManageSession

package manageSession;import org.hibernate.Session;import org.hibernate.Transaction;import org.junit.Test;import helloworld.Department;import helloworld.Employee;public class TestManageSession {	@Test	public void test() {		Session session = HibernateUtil.getInstance().getSession();		System.out.println("##"+session.hashCode());		Transaction transaction = session.beginTransaction();				EmployeeDao dao = new EmployeeDao();				Employee e = new Employee();		Department dep = new Department();		dep.setId(1);		e.setDepartment(dep);		e.setName("jyf");		e.setSalary(50000);				dao.save(e);		dao.save(e);		dao.save(e);				//若 Session 是由 thread 来管理的, 则在提交或回滚事务时, 已经关闭 Session 了. 		System.out.println(session.isOpen());		transaction.commit();		System.out.println(session.isOpen());	}}

cfg.xml

thread

批量处理(建议使用原生的connection进行)

package batch;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLException;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.hibernate.jdbc.Work;import org.hibernate.service.ServiceRegistry;import org.hibernate.service.ServiceRegistryBuilder;import org.junit.After;import org.junit.Before;import org.junit.Test;public class TestBatch {	private SessionFactory sessionFactory;	private Session sess;	private Transaction trans;	@Before	public void before() {		Configuration configuration = new Configuration().configure();		ServiceRegistry registry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())				.buildServiceRegistry();		sessionFactory = configuration.buildSessionFactory(registry);		sess = sessionFactory.openSession();		trans = sess.beginTransaction();	}	@After	public void after() {		trans.commit();		sess.close();		sessionFactory.close();	}	/**	 * 	 * 	 * */	@Test	public void testBatch() {		sess.doWork(new Work() {			// 最推荐使用原生的JDBC进行批量操作			@Override			public void execute(Connection connection) throws SQLException {				PreparedStatement preparedStatement = connection						.prepareStatement("insert into EMPLOYEES (SALARY, NAME, DEPARTMENT_ID)  values (?, ?, ?)");				preparedStatement.setInt(1, 1);				preparedStatement.setString(2, "JI_BATCH");				preparedStatement.setInt(3, 1);				preparedStatement.addBatch();				preparedStatement.setInt(1, 2);				preparedStatement.setString(2, "YUN_BATCH");				preparedStatement.setInt(3, 2);				preparedStatement.addBatch();				preparedStatement.setInt(1, 3);				preparedStatement.setString(2, "FEI_BATCH");				preparedStatement.setInt(3, 1);				preparedStatement.addBatch();				preparedStatement.executeBatch();			}		});	}}

  

  

  

  

  

  

  

转载于:https://www.cnblogs.com/feifeiyun/p/6485492.html

你可能感兴趣的文章
十一、类型转换
查看>>
面试内容,值得一看
查看>>
UILabel
查看>>
【热门技术】三种SEO方式
查看>>
[Hades_技术]哈迪斯初级技术应用
查看>>
SQLiteOpenHelper
查看>>
Luogu P1141 01迷宫【搜索/dfs】By cellur925
查看>>
js onclick事件传参
查看>>
WiCloud 商业Wi-Fi管理平台
查看>>
团队项目--未完待续
查看>>
双重标准,我该怎么解决
查看>>
python中的网页标签等字符处理
查看>>
Mybatis输入类型和结果类型
查看>>
Linux常用命令(五)
查看>>
Linux常用命令(四)
查看>>
Linux常用命令(六)
查看>>
Linux常用命令(六)
查看>>
Linux常用命令(八)
查看>>
Linux常用命令(七)
查看>>
Linux常用命令(九)
查看>>