Spring学习笔记之整合hibernate

时间:2022-12-09 09:27:50

1、web.xml里边要配置好对应的springxml的路径

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>

conf/kernel/spring_kernel/spring-*.xml,

conf/business/spring_business/spring-*.xml,

conf/custom/spring_custom/spring-*.xml

</param-value>

</context-param>

2、对应的文件夹里边要有spring-hibernate.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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean id="defaultLobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler" lazy-init="true"></bean>
   
    <bean id="dataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
        <property name="targetDataSource">
            <bean class="com.**********.DataSourceFactoryBean">
                <property name="propertyPool" value="**************" />
                <property name="propertyPrefix" value="***************." />
            </bean>
        </property>
    </bean>
    
    <!-- Session工厂 - start -->
    <bean id="sessionFactory" class="com.**********.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mappingDirectoryLocations">
            <list>
                <value>classpath*:conf/kernel/hibernate_kernel/**/*.hbm.xml</value>
                <value>classpath*:conf/business/hibernate_business/**/*.hbm.xml</value>
                <value>classpath*:conf/custom/hibernate_custom/**/*.hbm.xml</value>
            </list>
        </property>
        <property name="lobHandler" ref="defaultLobHandler"></property>
    </bean>

<!-- 声明spring事务管理 - start -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

<!-- spring_JDBC - start -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
    </bean>

<!-- spring事务传播机制 - start -->
    <bean id="txProxyTemplate" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="transactionManager">
            <ref bean="transactionManager" />
        </property>
        <property name="proxyTargetClass">
            <value>true</value><!-- 指定直接对类进行代理,将属性proxyTargetClass指定为true(默认是false,对接口进行代理) -->
        </property>
        <property name="transactionAttributes">
            <props>
                <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="inquery*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="inquiry*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="validate*">PROPAGATION_REQUIRED,-java.lang.Exception,-org.springframework.dao.DataAccessException</prop>
                <prop key="delete*">PROPAGATION_REQUIRED,-java.lang.Exception,-org.springframework.dao.DataAccessException</prop>
                <prop key="remove*">PROPAGATION_REQUIRED,-java.lang.Exception,-org.springframework.dao.DataAccessException</prop>
                <prop key="save*">PROPAGATION_REQUIRED,-java.lang.Exception,-org.springframework.dao.DataAccessException</prop>
                <prop key="add*">PROPAGATION_REQUIRED,-java.lang.Exception,-org.springframework.dao.DataAccessException</prop>
                <prop key="process*">PROPAGATION_REQUIRED,-java.lang.Exception,-org.springframework.dao.DataAccessException</prop>
                <prop key="insert*">PROPAGATION_REQUIRED,-java.lang.Exception,-org.springframework.dao.DataAccessException</prop>
                <prop key="create*">PROPAGATION_REQUIRED,-java.lang.Exception,-org.springframework.dao.DataAccessException</prop>
                <prop key="update*">PROPAGATION_REQUIRED,-java.lang.Exception,-org.springframework.dao.DataAccessException</prop>
                <prop key="modify*">PROPAGATION_REQUIRED,-java.lang.Exception,-org.springframework.dao.DataAccessException</prop>
                <prop key="persist*">PROPAGATION_REQUIRED,-java.lang.Exception,-org.springframework.dao.DataAccessException</prop>
                <prop key="driven*">PROPAGATION_REQUIRED,-java.lang.Exception,-org.springframework.dao.DataAccessException</prop>
                <prop key="parse*">PROPAGATION_REQUIRED,-java.lang.Exception,-org.springframework.dao.DataAccessException</prop>
                <prop key="middleUpdate*">PROPAGATION_REQUIRES_NEW,-java.lang.Exception,-org.springframework.dao.DataAccessException</prop>
                <prop key="*">PROPAGATION_REQUIRED,-java.lang.Exception,-org.springframework.dao.DataAccessException</prop>
            </props>
        </property>
    </bean>
   
    <bean id="dynamicBeanReader" class="com.***********.DynamicBeanReader" init-method="init"> 
    </bean>
   
</beans>

3、

LOB(large object)是一种用于存储大对象的数据类型,如医学记录(如X-射线)、视频、图像等。LOB有三种类型:BLOB:Binary Large Object、CLOB:Character Large Object、DBCLOB:Double-byte Character Large Object。每个LOB可以有2GB。

4、DynamicBeanReader:bean动态加载接口.应用于运行过程中动态加载bean,比如数据源的动态加载、定时任务的动态加载等。

5、大部分情况下,每个事务代理的事务属性大同小异,事务代理的实现类都是TransactionProxyFactoryBean,事务代理bean都必须注入事务管理器。对于这种情况,Spring提供了bean与bean之间的继承,可以简化配置。将大部分的通用配置,配置成事务模板,而实际的事务代理bean,则继承事务模板。这种配置方式可以减少部分配置代码,相比前面直接采用TransactionProxyFactoryBean的事务代理配置方式,可以大大减少配置文件的代码量。每个事务代理的配置都继承事务模板,无需重复指定事务代理的实现类,无需重复指定事务传播属性——当然,如果新的事务代理有额外的事务属性,也可指定自己的事务属性,此时,子bean的属性覆盖父bean的属性。当然每个事务代理bean都必须配置自己的目标bean,这不可避免。上面的配置可看出,事务代理的配置依然是增量式的,每个事务代理都需要单独配置——虽然增量已经减少,但每个事务代理都需要单独配置。

上面spring配置中的<prop>内的值为事务属性信息,匹配格式为:PROPAGATION (传播行为) , ISOLATION (隔离级别(可选)) , readOnly (是否为只读事务(可选)) , -Exceptions (发生这些异常时回滚事务(可选)) , +Exceptions (发生这些异常时照样提交事务(可选))

6、spring默认的使用的是c3p0