applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="url" value="jdbc:mysql://localhost:3306/mydb"></property> <property name="username" value="root"></property> <property name="password" value="123456"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> <!-- 是否打印sql --> <prop key="hibernate.show_sql">true</prop> <!-- 格式化sql --> <prop key="hibernate.format_sql">true</prop> <!-- 配置二级缓存 --> <prop key="hibernate.cache.use_second_level_cache">true</prop> <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop> <prop key="hibernate.net.sf.ehcache.configurationResourceName">ehcache.xml</prop> <!-- 开启查询时的二级缓存 --> <prop key="hibernate.cache.use_query_cache">true</prop> </props> </property> <property name="mappingResources"> <list> <value>com/db/entity/User.hbm.xml</value> </list> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <tx:annotation-driven transaction-manager="transactionManager" /> <bean id="UserDAO" class="com.db.entity.UserDAO"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> </beans>
如果Spring也使用cache缓存则
(1) 在spring 集成hibernate 的配置文件中,修改如下cache相关配置
<!-- 开启查询缓存 --> <prop key="hibernate.cache.use_query_cache">true</prop> <!-- 开启二级缓存 --> <prop key="hibernate.cache.use_second_level_cache">true</prop> <!-- 高速缓存提供程序 --> <!-- 由于spring也使用了Ehcache, 保证双方都使用同一个缓存管理器 --> <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</prop> <prop key="hibernate.net.sf.ehcache.configurationResourceName">ehcache.xml</prop>
由于Spring也使用ehcache, 所以也需要在spring配置文件中添加ehcache的配置
<bean id="cacheManagerEhcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation"> <value>classpath:ehcache.xml</value> </property> <!-- 由于hibernate也使用了Ehcache, 保证双方都使用同一个缓存管理器 --> <property name="shared" value="true"/> </bean>
ehcache.xml文件内容如下:
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd"> <!-- Subdirectories can be specified below the property e.g. java.io.tmpdir/one --> <diskStore path="java.io.tmpdir"/> <!-- Mandatory Default Cache configuration. These settings will be applied to caches created programmtically using CacheManager.add(String cacheName) --> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" /> <cache name="org.hibernate.cache.spi.UpdateTimestampsCache" maxElementsInMemory="5000" eternal="true" overflowToDisk="true" /> <cache name="org.hibernate.cache.internal.StandardQueryCache" maxElementsInMemory="10000" eternal="false" timeToLiveSeconds="120" overflowToDisk="true" /> </ehcache>
默认情况下二级缓存只会对load get 之类的方法缓存, 想list iterator 之类的方法也使用缓存,必须跟查询缓存一起使用, 重写查询方法
Query.setCahceable(true);
获取SessionFactory对象
法一:通过配置文件获取
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); SessionFactory sessionFactory=(SessionFactory) ac.getBean("sessionFactory"); Session ss=sessionFactory.openSession(); User user=new User(); user.setName("sara"); ss.save(user); ss.close();
法二:手动获取(必须要有对应的hibernate.cfg.xml文件)
SessionFactory sessionFactory; Configuration configuration = new Configuration(); ServiceRegistry serviceRegistry; configuration.configure(); serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); sessionFactory = configuration.buildSessionFactory(serviceRegistry);