`
liufangmeng
  • 浏览: 10260 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

springmvc3注解+mybatis+JTA+jboss7.1下多个数据源配置(二)

 
阅读更多


上一章中,简单的说了配置jboss7的xa数据源

这一章主要是说springmvc注解mybatis 和jta事务


首先,web容器jboss已经有了我们需要的数据源

那么,我们在spring配置里就可以直接使用jndi获取数据源了

先贴配置后讲解吧



 

<?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
     xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
        ">

	<!-- ***************资源文件 ************** -->
	<!--<context:property-placeholder location="classpath*:dataSourceConfig.properties"/>  -->
	<!-- ************** DataSource:  连接池 ****************-->
	<jee:jndi-lookup jndi-name="java:/oracle119" id="oracledataSource"
		lookup-on-startup="true">
	</jee:jndi-lookup>
	
	<jee:jndi-lookup jndi-name="java:jboss/mysql127" id="mssqldataSource"
		lookup-on-startup="true">
	</jee:jndi-lookup>
	
	<!-- ***************事务配置************** -->
	<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" />
	
	<aop:config>  
             <aop:advisor pointcut="execution(* service..*.*(..))"  advice-ref="txAdvice" />  
    </aop:config>  
  
    <tx:advice id="txAdvice" transaction-manager="transactionManager">  
        <tx:attributes>  
            <tx:method name="get*" read-only="true" />  
            <tx:method name="query*" read-only="true" />  
            <tx:method name="find*" read-only="true" />  
            <tx:method name="load*" read-only="true" />
            <tx:method name="select*" read-only="true" />  
            <tx:method name="*" propagation="REQUIRED" rollback-for="Exception" />  
        </tx:attributes>  
    </tx:advice>  
	
	<tx:annotation-driven transaction-manager="transactionManager" />
	
	<!-- MyBatis 把mysql和oracle的mybatis映射xml 分开来放   -->
	<bean id="oraclesqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="oracledataSource" />
	    <property name="mapperLocations"  value="classpath*:dao/oracleDb/**/*.xml"/> 
	</bean>
		<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">    
	    <property name="basePackage" value="dao.oracleDb" />
	    <property name="sqlSessionFactoryBeanName" value="oraclesqlSessionFactory" />
	</bean>
    	<bean id="mssqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="mssqldataSource" />
	    <property name="mapperLocations"  value="classpath*:dao/mssqlDb/**/*.xml"/> 
	</bean>
		<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">    
	    <property name="basePackage" value="dao.mssqlDb" />
	    <property name="sqlSessionFactoryBeanName" value="mssqlSessionFactory" />
	</bean>
    
    
</beans>

 

 

 这个配置文件 是springmvc3里面只是核心的配置供大家参考。 其余还有jsp路径,还有controller  service层的注解,还有拦截器 在另外一个配置里面 ,分开写好调试

 

首先 前面使用了jee:jndi-lookup 这种方式获取了数据源, 要在xml最上面的声明那里 加入xmlns:jee="http://www.springframework.org/schema/jee 和下面xsi:schemaLocation里的 jee

才能正常使用

 

其次 bean  id="transactionManager "  这个需要使用 

org.springframework.transaction.jta.JtaTransactionManager

这里并不需要指定数据源datasource属性 

详细请见:http://docs.spring.io/spring/docs/3.0.x/reference/transaction.html

 

Note
[Note]

If you use JTA , then your transaction manager definition will look the same regardless of what data access technology you use, be it JDBC, Hibernate JPA or any other supported technology. This is due to the fact that JTA transactions are global transactions, which can enlist any transactional resource.

 

 最后 核心的mybatis和springmvc结合的地方,<bean id="oraclesqlSessionFactory"到最后

是我以前采用咿呀网 狼哥提供的单数据源配置修改而来,这种方式 好处就是 把不同的数据库里需要映射的mybatis配置,分开放,启动应用的时候 可以直接扫描,实现自动加载,(看过几片文章,发现这种方式不是最好的,但是 英文才 也只能勉强到这里了。)

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics