spring和hibernate整合是事物处理配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <!-- sessionFactory --> <bean id="hello" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"> </property> <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"> </property> <property name="username" value="hr"></property> <property name="password" value="hr"></property> </bean> <bean id="sessionfactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="hello" /> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.Oracle9Dialect </prop> </props> </property> <property name="mappingResources"> <list> <value>com/yidu/dao/Customer.hbm.xml</value> <value>com/yidu/dao/Ord.hbm.xml</value></list> </property></bean> <bean id="CustomerDAO" class="com.yidu.dao.CustomerDAO"> <property name="sessionFactory"> <ref bean="sessionfactory" /> </property> </bean> <bean id="OrdDAO" class="com.yidu.dao.OrdDAO"> <property name="sessionFactory"> <ref bean="sessionfactory" /> </property> </bean> <!-- VO对象 --> <bean id="cus" class="com.yidu.dao.Customer"> <property name="CId" value="4"></property> <property name="name" value="xiaoqi"></property> </bean> <!-- 业务逻辑 --> <bean id="CustomerBiz" class="com.yidu.biz.CustomerBiz"> <property name="customerDao" ref="CustomerDAO"></property> <property name="customer" ref="cus"></property> </bean> <!-- 事物处理 --> <!-- 定义事物管理器通知 --> <bean id="transManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionfactory"> </property> </bean> <!-- 定义一个切入点 --> <bean id="cutPointTran" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <property name="transactionManager" ref="transManager"></property> <property name="transactionAttributes"> <props> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> </beans>
spring和ibatis整合事务处理的配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <!-- 数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"> </property> <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"> </property> <property name="username" value="hr"></property> <property name="password" value="hr"></property> </bean> <!-- sqlMapClient --> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="file:src/Ibatis.xml"></property> </bean> <!--SqlMapClientTemplate --> <bean id="SqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate"> <property name="sqlMapClient" ref="sqlMapClient"></property> <!-- VO对象 --> </bean> <bean id="customer" class="com.yidu.bean.Customer"> <property name="cid" value="21"></property> <property name="name" value="马子"></property> </bean> <!-- Dao层代码 --> <!-- 继承了SqlMapClientTemplate模板对象的 --> <bean id="cusSupport" class="com.yidu.dao.CusSupportImpl"> <property name="customer" ref="customer"></property> <property name="sqlMapClient" ref="sqlMapClient"></property> </bean> <!-- 没继承的,需要注入 --> <bean id="cusTimp" class="com.yidu.dao.CustimplateImpl"> <property name="customer" ref="customer"></property> <property name="sct" ref="SqlMapClientTemplate"></property> </bean> <!-- 注入cusSupp DAO --> <bean id="cusBiz" class="com.yidu.biz.CustomerBiz"> <property name="cd" ref="cusSupport"></property> </bean> <!-- 注入templateDao --> <bean id="templatebiz" class="com.yidu.biz.CustomerBiz"> <property name="cd" ref="cusTimp"></property> </bean> <!-- 定义事物管理器(专门由于处理事物的通知) --> <bean id="tranct" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 定义事务拦截器(事物切入点) --> <bean id="trInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <property name="transactionManager" ref="tranct"></property> <property name="transactionAttributes"> <props> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> <!-- 自动代理 --> <bean id="autoProxy" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames"> <list> <value>*Dao</value><!-- bean的id名称 --> </list> </property> <property name="interceptorNames"> <list> <value>trInterceptor</value> </list> </property> </bean> </beans>
ibatis的配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> <sqlMapConfig> <transactionManager type="JDBC"> <dataSource type="SIMPLE"> <property name="JDBC.Driver" value="com.microsoft.jdbc.sqlserver.SQLServerDriver"/><!-- 只能用该驱动程序 --> <property name="JDBC.ConnectionURL" value="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=javaEE;SelectMethod=Cursor"/> <property name="JDBC.Username" value="sa"/> <property name="JDBC.Password" value="123"/> </dataSource> </transactionManager> <sqlMap resource="com/yidu/bean/Classes.xml" /> <sqlMap resource="com/yidu/bean/Student.xml"/> </sqlMapConfig>