设为首页 收藏本站
查看: 1044|回复: 0

[经验分享] springmvc3注解+mybatis+JTA+jboss7.1下多个数据源配置(二)

[复制链接]

尚未签到

发表于 2016-11-28 08:18:51 | 显示全部楼层 |阅读模式

上一章中,简单的说了配置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

[/td]
Note
[/tr]

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结合的地方,[size=1em]<bean id="oraclesqlSessionFactory"[size=1em]到最后
是我以前采用咿呀网 狼哥提供的单数据源配置修改而来,这种方式 好处就是 把不同的数据库里需要映射的mybatis配置,分开放,启动应用的时候 可以直接扫描,实现自动加载,(看过几片文章,发现这种方式不是最好的,但是 英文才 也只能勉强到这里了。)

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-306371-1-1.html 上篇帖子: MyBatis中出现Mapped Statements collection does not contain value异常解决方案 下篇帖子: mybatis调用存储过程 多个参数并返回多个复杂结果
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表