二级缓存
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(); } }); }}