`
wangkebin
  • 浏览: 75048 次
  • 性别: Icon_minigender_1
  • 来自: 贵阳
社区版块
存档分类
最新评论

Spring+Struts+ibatis下配置数据读写分离及事务(三)

阅读更多

接Spring+Struts+ibatis下配置数据读写分离及事务(二) 

6.配置spring-transaction.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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
	 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
	
	<!--切面BEAN(数据源切换)-->
	<bean id="dataSourceAdvice" class="com.ssi222.wilr.util.datasource.DataSourceAdvice"/>
	
	<!--配置Service监控 -->
	<aop:config>
	   <aop:advisor pointcut="execution(* com.ssi222.wilr.service..*Service.*(..))" advice-ref="dataSourceAdvice"/>
	</aop:config>
	
	<!--配置事务管理器-->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
	</bean>	
	
	<!--配置事务控制特性-->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
	    <tx:attributes>
	       <!--对数据UPDATE操作支持事务-->
	       <tx:method name="add*" propagation="REQUIRED" rollback-for="Exception"/>
	       <tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception"/>
	       <tx:method name="create*" propagation="REQUIRED" rollback-for="Exception"/>
	       <tx:method name="save*" propagation="REQUIRED" rollback-for="Exception"/>
	       <tx:method name="del*" propagation="REQUIRED" rollback-for="Exception"/>
	       <tx:method name="edit*" propagation="REQUIRED" rollback-for="Exception"/>
	       <tx:method name="modeify*" propagation="REQUIRED" rollback-for="Exception"/>
	       <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
	       <!--对其他方法只支持只读事务-->
			<tx:method name="*" propagation="SUPPORTS" read-only="true"/>
	    </tx:attributes>
	</tx:advice>
	
	<!--配置Service事务支持-->
	<aop:config>
	   <aop:advisor pointcut="execution(* com.ssi222.wilr.service..*Service.*(..))" advice-ref="txAdvice"/>
	</aop:config>
</beans>

 

 7.配置bean对应的ibatis配置文件(只写了一个简单的测试)

 

 

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="System">
	<typeAlias alias="Admin" type="com.ssi222.wilr.domain.bg.system.AdminInfo" />
	<select id="loadLoginAdmin" resultClass="Admin" parameterClass="String">
	<![CDATA[
	  	SELECT * FROM tbl_admin WHERE account=#account#
	]]>
	</select>
</sqlMap>

 

 8.配置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>
   <settings	cacheModelsEnabled		= "true"
    			enhancementEnabled		= "true"
    			lazyLoadingEnabled		= "true" 
    			errorTracingEnabled		= "true" 
    			useStatementNamespaces	= "true"
    			maxRequests				= "512"   
    			maxSessions				= "128"
    			maxTransactions			= "16"/>
    <typeAlias alias="sql.date" 		type="java.sql.Date"/>
	<typeAlias alias="sql.timestamp" 	type="java.sql.Timestamp"/>	
	<typeAlias alias="map" 				type="java.util.Map"/>  
	<typeAlias alias="list" 			type="java.util.List"/>
	<typeAlias alias="hashmap" 			type="java.util.HashMap"/>
	<typeAlias alias="hashset" 			type="java.util.HashSet"/>
   <!--导入后台系统Ibatis配置-->
   <sqlMap resource="config/ibatis/bg/ibatis-system.xml"/>
   <!--导入前台系统Ibatis配置-->
</sqlMapConfig>   

 9.配置spring.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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
	 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    <context:annotation-config />
	<!-- 自动加载service presentation action-->
	<context:component-scan base-package="com.ssi222.wilr.presentation.*" />
	<context:component-scan base-package="com.ssi222.wilr.service.*" />
    <context:component-scan base-package="com.ssi222.wilr.action.*" />
    
    <!--导入数据源-->
    <import resource="classpath:config/spring-datasource.xml"/>
    <!--导入事务控制-->
    <import resource="classpath:config/spring-transaction.xml"/>
    <!--导入后台系统Service/Action配置-->
    <import resource="classpath:config/spring/spring-bg.xml"/>
	
</beans>
 

 

 

 

 

1
4
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics