欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

在一个spring配置文件中同时配置hibernate和jdbc

程序员文章站 2022-03-16 09:59:20
...

维护legacy系统时,难免碰上同时使用hibernate和jdbc的情况,那么spring中
怎么配置呢?今天试用成功,贴出配置部分备忘:

<bean id="dataSource"
   class="org.springframework.jndi.JndiObjectFactoryBean">
   <property name="jndiName">
    <value>${JDBC_JNDI}</value>
   </property>
</bean>
<bean id="jdbcTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
   <property name="dataSource"><ref local="dataSource"/></property>
</bean>
<bean id="jdbcTransactionProxy"
   class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
   abstract="true">
   <property name="transactionManager">
    <ref bean="jdbcTransactionManager"/>
   </property>
   <property name="transactionAttributes">
    <props>
     <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
     <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
     <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
     <prop key="modify*">PROPAGATION_REQUIRED</prop>
     <prop key="delete*">PROPAGATION_REQUIRED</prop>
     <prop key="add*">PROPAGATION_REQUIRED</prop>
    </props>
   </property>
</bean>
<bean id="sessionFactory"
   class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
......
</bean>
<bean id="hibernateTransactionManager"
   class="org.springframework.orm.hibernate.HibernateTransactionManager">
   <property name="sessionFactory">
    <ref local="sessionFactory"/>
   </property>
</bean>
<bean id="hibernateTransactionProxy"
   class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
   abstract="true">
   <property name="transactionManager">
    <ref bean="transactionManager"/>
   </property>
   <property name="transactionAttributes">
    <props>
     <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
     <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
     <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
     <prop key="modify*">PROPAGATION_REQUIRED</prop>
     <prop key="delete*">PROPAGATION_REQUIRED</prop>
     <prop key="add*">PROPAGATION_REQUIRED</prop>
    </props>
   </property>
</bean>

然后在service层分别引用就可以了.

private JdbcTemplate jdbcTemplate;

get ,set 方法省略