在Spring-Mybatis-Restful中配置多数据源的properties文件
在Spring的配置文件中,如下:
<?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/beans http://www.springframework.org/schema/beans/spring-beans-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/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
default-autowire="byName" default-lazy-init="false">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/WEB-INF/database.properties</value>
</list>
</property>
</bean> <bean id="ds1" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${cc.driver}"/>
<property name="url" value="${cc.url}"/>
<property name="username" value="${cc.username}"/>
<property name="password" value="${cc.password}"/>
</bean>
<bean id="ds2" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${tt.driver}"/>
<property name="url" value="${tt.url}"/>
<property name="username" value="${tt.username}"/>
<property name="password" value="${tt.password}"/>
</bean>
<bean id="dataSource" class="cn.ac.iscas.pebble.ufe.ds.DynamicDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry value-ref="ds1" key="ds1"></entry>
<entry value-ref="ds2" key="ds2"></entry>
</map>
</property>
<property name="defaultTargetDataSource" ref="ds1"></property> <!-- 默认使用ds1的数据源 -->
</bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean> <bean id="ysSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath*:cn/ac/iscas/pebble/ufe/mapper/*Mapper.xml"/>
</bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="ysSqlSessionFactory" />
</bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="ysSqlSessionFactory"/>
<property name="basePackage" value="cn.ac.iscas.pebble.ufe.inter"/>
</bean>
</beans>
注意上述黄色背景的部分,注意事项:
1. 其中必须是 sqlSessionFactoryBeanName 而不能是 sqlSessionFactoryBean
2. 如果是sqlSessionFactoryBeanName 则其后应该用value,而不是ref(ref对应于sqlSessionFactoryBean)
3. org.mybatis.spring.SqlSessionFactoryBean 的id 不能是 SqlSessionFactory,也不能类似SqlSessionFactory2,sqlSessionFactory或者MySqlSessionFactory,可以是ySqlSessionFactory等。
主要原因是mybatis的问题,在有mybatis的时候,Spring会先加载MapperScannerConfigurer,而使用sqlSessionFactoryBean就会报错找不到DatabaseID,因为此时还没有读取database.properties文件,sqlSessionFactoryBean在实例化的时候报错,可以使用sqlSessionFactoryBeanName,此参数不会实例化,只是声明。
在Spring-Mybatis-Restful中配置多数据源的properties文件的更多相关文章
- 在Spring MVC项目中,注解方式使用 .properties 文件及 UTF-8编码问题
xml配置 <!-- 配置文件 --> <bean id="configProperties" class="org.springframework.b ...
- Spring MVC 使用tomcat中配置的数据源
Spring MVC 使用tomcat中配置的数据源 配置tomcat数据源 打开tomcat目录下的conf目录,编辑sever.xml目录.在<GlobalNamingResources&g ...
- spring,mybatis事务管理配置与@Transactional注解使用[转]
spring,mybatis事务管理配置与@Transactional注解使用[转] spring,mybatis事务管理配置与@Transactional注解使用 概述事务管理对于企业应用来说是至关 ...
- spring,mybatis事务管理配置与@Transactional注解使用
spring,mybatis事务管理配置与@Transactional注解使用[转] spring,mybatis事务管理配置与@Transactional注解使用 概述事务管理对于企业应用来说是 ...
- Spring+MyBatis双数据库配置
Spring+MyBatis双数据库配置 近期项目中遇到要调用其它数据库的情况.本来仅仅使用一个MySQL数据库.但随着项目内容越来越多,逻辑越来越复杂. 原来一个数据库已经不够用了,须要分库分表.所 ...
- WebSphere中配置的数据源在Web应用中引用的写法
WebSphere中配置的数据源在Web应用中引用时名称一定要和数据源的JNDI名称保持一致,否则会出现无法找到数据源的错误. 引用WAS的数据源时只需要与JNDI名称保持一致即可. 引用Tomcat ...
- Spring Boot整合Druid配置多数据源
Druid是阿里开发的数据库连接池,功能强大,号称Java语言中最好的数据库连接池.本文主要介绍Srping Boot下用Druid配置多个数据源,demo环境为:Spring Boot 2.1.4. ...
- AndroidStudio -- AndroidStuido中找不到cache.properties文件
AndroidStuido中找不到cache.properties文件 报错信息: 16:32:10 Gradle sync failed: C:\Users\***\.gradle\caches\2 ...
- spring、spring boot中配置多数据源
在项目开发的过程中,有时我们有这样的需求,需要去调用别的系统中的数据,那么这个时候系统中就存在多个数据源了,那么我们如何来解决程序在运行的过程中到底是使用的那个数据源呢? 假设我们系统中存在2个数据源 ...
随机推荐
- NYOJ题目64鸡兔同笼
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAsAAAAIZCAIAAAAnfB5fAAAgAElEQVR4nO3dO1LjygIG4LsJchZC7I ...
- AR , VR, GVR ...
虚拟现实与增强现实(眼镜或头盔)的现状与未来-简介http://blog.csdn.net/yanzhanyi/article/details/41982033 Google VR | Googl ...
- 构造方法 static 块 {}块 执行顺序
package com.test.innerclass; public class HelloB extends HelloA { public HelloB() { System.out.print ...
- 攻城狮在路上(伍)How tomcat works(四)Tomcat的默认连接器
在第4章中将通过剖析Tomcat4的默认连接器的代码,讨论需要什么来创建一个真实的Tomcat连接器. 注意:本章中提及的“默认连接器”是指Tomcat4的默认连接器.即使默认的连机器已经被 ...
- CentOS下用yum配置php+mysql+apache(LAMP)
#安装需要的包,有依赖关系,自动帮你解决 yum install httpd mysql mysql-server php php-gd php-mbstring php-mysql #启动httpd ...
- IBM Rational AppScan 无法记录登录序列 分类: 数据安全 2015-03-18 16:46 158人阅读 评论(0) 收藏
为了测试漏洞,我在本地部署了一个站点,为http://localhost/app,并且有登录页面. 但是尝试多次,都无法记录登录页面.此时尝试了在hosts文件中,自定义了一个域名 127.0.0.1 ...
- JS常用语句
JavaScript常用语句 1.document.write(""); 输出语句 2.JS中的注释为 // 3.传统的HTML文档顺序是: document-& ...
- 跟着鸟哥学Linux系列笔记3-第11章BASH学习
跟着鸟哥学Linux系列笔记0-扫盲之概念 跟着鸟哥学Linux系列笔记0-如何解决问题 跟着鸟哥学Linux系列笔记1 跟着鸟哥学Linux系列笔记2-第10章VIM学习 认识与学习bash 1. ...
- 10g ASM下修改control file的位置
1.查看位置以及name是否正确 SQL> sho parameter name NAME TYPE VALUE ------------------------------------ --- ...
- POJ 2114 Boatherds 树分治
Boatherds Description Boatherds Inc. is a sailing company operating in the country of Trabantust ...