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-35 Search Insert Position
leetcode-algorithms-35 Search Insert Position Given a sorted array and a target value, return the in ...
- 如何使a标签打开新页面并阻止刷新当前页面
错误: HTML中,使用href属性时,当前页面和新页面均跳转到URL指定的页面,即当前页面也刷新: <li id='goToBack'><a href='**.action' ta ...
- 【Python】基础知识
一.基本概念 1.变量与运算符 Python允许给多个变量同时赋值,等号 (=) 右边的值将赋予左边对应位置的变量. # 将a, b, c的值依次赋予b, c, a b, c, a = a, b, c ...
- 从使用角度看 ReentrantLock 和 Condition
java 语言中谈到锁,少不了比较一番 synchronized 和 ReentrantLock 的原理,本文不作分析,只是简单介绍一下 ReentrantLock 的用法,从使用中推测其内部的一些原 ...
- FastDFS安装教程
1.下载 FastDFS下载:https://codeload.github.com/happyfish100/fastdfs/zip/master 库文件下载:https://codeload.gi ...
- Spring Boot + Spring Cloud 实现权限管理系统 后端篇(十一):集成 Shiro 框架
Apache Shiro 优势特点 它是一个功能强大.灵活的,优秀开源的安全框架. 它可以处理身份验证.授权.企业会话管理和加密. 它易于使用和理解,相比Spring Security入门门槛低. 主 ...
- 利用模板导出文件(二)之jacob利用word模板导出word文件(Java2word)
https://blog.csdn.net/Fishroad/article/details/47951061?locationNum=2&fps=1 先下载jacob.jar包.解压后将ja ...
- Struts初步入门(四)
1.默认Action-深入Struts struts.xml 文件: <struts> <package name="default" namespace=&qu ...
- http协议与浏览器缓存
http协议与浏览器缓存 F5刷新与在地址栏回车的区别 链接
- 执行这些代码, Edit1只能输入数字,小数点和负号,负号和小数点只能输入一个,负号必须在最前,粘贴的数字必须完全正确.
执行这些代码, Edit1只能输入数字,小数点和负号,负号和小数点只能输入一个,负号必须在最前,粘贴的数字必须完全正确. type TForm1 = class(TForm) Edit1: TEdit ...