hibernateTemplate 是spring 创建的使用hibernate ORM访问数据库模板,有许多hibernate访问数据库的方法,
但我们在项目中经常因为hibernate 操作批量 数据的 局限性 或其他问题,需要使用原生sql及jdbc操作数据库
这样,就需要我们使用spring 的 jdbcTemplate ,这是spring 的 Jdbc 数据库访问 模板,使用原生sql 语句 及 jdbc访问数据库,
所以 ,我们就需要在 spring 中 将两中模板 都以bean 的形式 注入,spring 配置如下
<!-- 通过property 文件方式配置数据库信息-->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClass}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="lobHandler" ref="defaultLobHandler"/>
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
<prop key="hibernate.connection.autocommit">false</prop>
<prop key="hibernate.autoReconnect">true</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">none</prop>
</props>
</property>
</bean>
<!-- hibernateTemplate配置 -->
<bean id="hiberTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- jdbcTemplate 配置 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean name="/person" class="com.ssh.person.action.PersonAction">
<property name="personService" ref="personService"></property>
</bean>
<bean id="personService" class="com.ssh.person.service.PersonServiceImpl">
<property name="tac01Dao" ref="tac01Dao"></property>
</bean>
<bean id="tac01Dao" class="com.ssh.person.dao.Tac01DaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
<property name="hibernateTemplate" ref="hiberTemplate"></property>
</bean>
hibernate.cfg.xml 文件中配置 orm文件,
<hibernate-configuration>
<session-factory>
<mapping resource="com/ssh/test/hiber/po/Tac20.hbm.xml" />
<mapping resource="com/ssh/test/hiber/po/Tace10.hbm.xml" />
<mapping resource="com/ssh/test/hiber/po/Tac01.hbm.xml" />
</session-factory>
</hibernate-configuration>
但 这样 我们 就产生 一个 问题,我们事务不能共享 ,spring提供了 事务共享
<!-- Hibernate事务 -->
<bean id="hibernateTransaction" class="org.springframework.orm.hibernate3.HibernateTransactionManager" >
<property name="sessionFactory" ref="sessionFactory" />
<property name="dataSource" ref="dataSource" />
</bean>
<!-- blob、clob设置 -->
<bean id="defaultLobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler" lazy-init="true" />
<!-- AOP事务控制 -->
<tx:advice id="txAdvice1" transaction-manager="hibernateTransaction">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="allManagerMethod" expression="execution(* com.ssh.person.service.PersonServiceImpl.*(..))" />
<aop:advisor advice-ref="txAdvice1" pointcut-ref="allManagerMethod" />
</aop:config>
代码示例 http://download.youkuaiyun.com/detail/meililiuwei/6857577