ssm 配置多个数据源
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" 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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.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/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <!-- 加载jdbc.properties文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:config/datasource.properties</value>
</list>
</property>
</bean> <!-- 配置DataSource数据源 -->
<!-- <bean id="dataSource" -->
<!-- class="org.springframework.jdbc.datasource.DriverManagerDataSource"> -->
<!-- <property name="driverClassName" value="${driver}" /> -->
<!-- <property name="url" value="${url}" /> -->
<!-- <property name="username" value="${username}" /> -->
<!-- <property name="password" value="${password}" /> -->
<!-- </bean> --> <!-- JNDI连接池配置(jboss:java:/socket tomcat : java:comp/env/jdbc/socket)-->
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/socket</value>
</property>
</bean> <!-- 配置Mybatis SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:config/mybatis.xml" />
<!-- mapper和resultmap配置路径 -->
<property name="mapperLocations">
<list>
<!-- 表示在com/smart/包或以下所有目录中,以_sql.xml结尾所有文件 -->
<value>classpath*:com/bd/**/*_sql.xml</value>
</list>
</property>
</bean> <!-- 框架 -->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean> <bean id="dataSourceHr" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/hr</value>
</property>
</bean> <!-- 配置Mybatis SqlSessionFactoryBean -->
<bean id="sqlSessionFactoryHr" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSourceHr" />
<property name="configLocation" value="classpath:config/mybatis.xml" />
<!-- mapper和resultmap配置路径 -->
<property name="mapperLocations">
<list>
<!-- 表示在com/smart/包或以下所有目录中,以_sql.xml结尾所有文件 -->
<value>classpath*:com/bd/**/*_sql.xml</value>
</list>
</property>
</bean> <!-- 框架 -->
<bean id="sqlSessionHr" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactoryHr" />
</bean> <bean id="batisDao" class="com.bd.core.base.dao.mybatis.BatisDaoImpl">
<property name="sqlSession">
<ref bean="sqlSession" />
</property>
<property name="sqlSessionHr">
<ref bean="sqlSessionHr" />
</property>
</bean> <!--事务相关控制-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean> <tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" propagation="REQUIRED"
rollback-for="Exception,BusinessException" />
<tx:method name="find*" propagation="REQUIRED"
rollback-for="Exception,BusinessException" />
<tx:method name="query*" propagation="REQUIRED"
rollback-for="Exception,BusinessException" />
<tx:method name="add*" propagation="REQUIRED" rollback-for="Exception,BusinessException" />
<tx:method name="save*" propagation="REQUIRED"
rollback-for="Exception,BusinessException" />
<tx:method name="update*" propagation="REQUIRED"
rollback-for="Exception,BusinessException" />
<tx:method name="edit*" propagation="REQUIRED"
rollback-for="Exception,BusinessException" />
<tx:method name="delete*" propagation="REQUIRED"
rollback-for="Exception,BusinessException" />
<tx:method name="remove*" propagation="REQUIRED"
rollback-for="Exception,BusinessException" />
<tx:method name="audit*" propagation="REQUIRED"
rollback-for="Exception,BusinessException" />
<tx:method name="modify*" propagation="REQUIRED"
rollback-for="Exception,BusinessException" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="interceptorPointCuts"
expression="execution(* com.bd.smart.*.*.service..*.*(..)) || execution(* com.bd.smart.*.service..*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" />
</aop:config> </beans>
配置两个数据源和session工厂类和session对象,只需在dao的bean 中注入这些session对象,当调用的是哪个session 就会获取哪个数据库的连接。
ssm 配置多个数据源的更多相关文章
- ssm框架使用详解&配置两个数据源
学习ssm框架已经快一年了,今天把这个框架总结一下. SSM 就是指 spring.SpringMVC和Mybatis.先说一下基本概念(百度上搜的) 1.基本概念 1.1.Spring Spring ...
- ssm项目配置多个数据源
在项目中到一些问题,一些查询模块需要链接另一个数据库,这时,就可以配置两个数据源进行操作. 1.创建jdbc.properties jdbc.url = jdbc:mysql://localhost: ...
- spring-data-mongodb一个系统xml文件里面配置两个数据源
spring-data-mongodb一个系统xml文件里面配置两个数据源 参考文档如下: http://www.iteye.com/problems/92789 http://stackoverfl ...
- 用JAVA代码获取Weblogic配置的JNDI 数据源连接
第一步:生成与JDK版本对应的weblogicjar,利用cmd 进入到weblogic_home 路径下进入到server/lib目录,然后运行 JDK 1.6 命令 "java -j ...
- SpringMVC+Mybatis 如何配置多个数据源并切换?
最近公司一个项目需要连接两个数据库(A和B)操作,有的模块查询A库,有的模块查询B库,因此需要改造下,项目后台用的是SpringMVC+Mybatis+MySQL架构,折腾了两天后终于搞定了,在这里记 ...
- SpringBoot入门之基于Druid配置Mybatis多数据源
上一篇了解了Druid进行配置连接池的监控和慢sql处理,这篇了解下使用基于基于Druid配置Mybatis多数据源.SpringBoot默认配置数据库连接信息时只需设置url等属性信息就可以了,Sp ...
- 20. Spring Boot 默认、自定义数据源 、配置多个数据源 jdbcTemplate操作DB
Spring-Boot-2.0.0-M1版本将默认的数据库连接池从tomcat jdbc pool改为了hikari,这里主要研究下hikari的默认配置 0. 创建Spring Boot项目,选中 ...
- 配置多个数据源,spring profile 多环境配置管理
针对生产环境,测试环境,以及本地调试开发有时会配置多套数据库,在一个数据配置文件进行修改,往往有时发布到生成环境会忘记修改,或者本地调试时还是生产环境的库,会导致生产环境数据被污染. ps--刚开始配 ...
- Springbooot +Mybaties 配置数据库多数据源
前言 在实际项目中,我们可能会碰到在一个项目中会访问多个数据库的情况.针对这种情况,我们就需要配置动态的数据源了.一般按照以下步骤即可 一.在启动类上添加注解 二.在application.prope ...
随机推荐
- leetcode-algorithms-2 Add Two Numbers
leetcode-algorithms-2 Add Two Numbers You are given two non-empty linked lists representing two non- ...
- HDU-2767-tarjan/Kosaraju求scc
http://acm.hdu.edu.cn/showproblem.php?pid=2767 问最少添加几条边使得图为强连通. tarjan跑一下,然后对强连通分量缩点,找下此时出度为零和入度为零的点 ...
- 121. Best Time to Buy and Sell Stock 买卖股票的最佳时机
网址:https://leetcode.com/problems/Best-Time-to-Buy-and-Sell-Stock/ 第一想法是滑动窗口法,稍微尝试后发现不可行,至少我不会... 而后想 ...
- 七、持久层框架(MyBatis)
一.MyBatis学习 平时我们都用JDBC访问数据库,除了自己需要写SQL,还要操作Connection,Statement,ResultSet这些. 使用MyBatis,只需要自己提供SQL语句, ...
- Linux中环境变量文件
一.环境变量文件介绍 转自:http://blog.csdn.net/cscmaker/article/details/7261921 Linux中环境变量包括系统级和用户级,系统级的环境变量是每个登 ...
- Maven 99.0-does-not-exist构建空包,排查依赖
空包作用 作用:强制排除所有对该包的依赖: 空包制作 构建一个空包pom.xml,如下图所示: <?xml version="1.0" encoding="UTF- ...
- 从零开始学习Vue(四)
这里引入一个概念SPA(single Page Application), 接着上次的例子,我们在页面底部做了一个Tab的菜单,点击不同的按钮应该是显示不同的内容. 按传统的MVC的思维,我要在Con ...
- [IOS微信] Unicode码 转化为字符串
最近在研究IOS手机备份的数据,里面的微信数据中,每一个微信账号对应一个文件:mmsetting.archive 用来保存此账号的详细信息. 该文件是一个加强版的plist文件(此文件使用的是plis ...
- linux网络操作 netstat命令
关闭与启动网卡 ifdown 网卡设备名 #禁用该网卡设备 ifup网卡设备名 #启用该网卡设备 查看网络状态 netstat 命令 -t 列出tcp协议端口 -u 列出udp协议端口 -n 不 ...
- Win10系列:UWP界面布局基础9
在XAML中,样式.模板.画笔和动画等都被看做是资源,它们最终的作用都是相同的,就是让设计人员可以使用这些资源创建更好的视觉效果,对其设计的产品外观进行标准化统一,那么如何使用这些资源并组织和重用呢, ...