Spring mvc动态多数据源

news/2024/5/18 22:43:23 标签: spring, mvc, jdbc, date, class, string
class="baidu_pl">
class="article_content clearfix">
class="htmledit_views">

        本文基于Spring MVC,拦截器实现Session控制。

        本文通过拦截器取得当前使用的Locale,然后通过Locale找到不同的数据源。

        首先,新建类DynamicDataSource,使其继承org.class="tags" href="/tags/SPRING.html" title=spring>springframework.class="tags" href="/tags/JDBC.html" title=jdbc>jdbc.datasource.lookup.AbstractRoutingDataSource并实现其determineCurrentLookupKey方法,并实现获取currentLookupKey的方法,代码如下所示:

class="language-java">/**
 *
 * @author geloin
 * @class="tags" href="/tags/DATE.html" title=date>date 2012-5-18 下午3:20:51
 */
package com.embest.ruisystem.datasource;

import org.class="tags" href="/tags/SPRING.html" title=spring>springframework.class="tags" href="/tags/JDBC.html" title=jdbc>jdbc.datasource.lookup.AbstractRoutingDataSource;

/**
 * 动态数据源
 * 
 * @author geloin
 * @class="tags" href="/tags/DATE.html" title=date>date 2012-5-18 下午3:20:51
 */
public class DynamicDataSource extends AbstractRoutingDataSource {

	private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();

	/**
	 * 
	 * @author geloin
	 * @class="tags" href="/tags/DATE.html" title=date>date 2012-5-18 下午4:06:44
	 * @return the currentLookupKey
	 */
	public static String getCurrentLookupKey() {
		return (String) contextHolder.get();
	}

	/**
	 * 
	 * @author geloin
	 * @class="tags" href="/tags/DATE.html" title=date>date 2012-5-18 下午4:06:44
	 * @param currentLookupKey
	 *            the currentLookupKey to set
	 */
	public static void setCurrentLookupKey(String currentLookupKey) {
		contextHolder.set(currentLookupKey);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.class="tags" href="/tags/SPRING.html" title=spring>springframework.class="tags" href="/tags/JDBC.html" title=jdbc>jdbc.datasource.lookup.AbstractRoutingDataSource#
	 * determineCurrentLookupKey()
	 */
	@Override
	protected Object determineCurrentLookupKey() {
		return getCurrentLookupKey();
	}

}

        上述代码中的determineCurrentLookupKey方法取得一个字符串,该字符串将与配置文件中的相应字符串进行匹配以定位数据源,配置文件,即applicationContext.xml文件中需要要如下代码:

class="language-java">        <!-- 动态数据源 -->
	<bean id="dataSource" class="com.embest.ruisystem.datasource.DynamicDataSource">
		<property name="targetDataSources">
			<map key-type="java.lang.String">
				<entry key="zh" value-ref="chinaDataSource" />
				<entry key="en" value-ref="englishDataSource" />
			</map>
		</property>
		<property name="defaultTargetDataSource" ref="chinaDataSource" />
	</bean>

        determineCurrentLookupKey方法取得字符串后,将会与上述配置中的<map...></map>中的值对应,即当determineCurrentLookupKey方法取得值为en时,则数据源指向englishDataSource,当然,map中的value-ref对应的是你在applicationContext.xml文件中配置的数据源,如下所述:

class="language-java">        <!--创建中国class="tags" href="/tags/JDBC.html" title=jdbc>jdbc数据源 -->
	<bean id="chinaDataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${driver_zh}" />
		<property name="url" value="${url_zh}" />
		<property name="username" value="${username_zh}" />
		<property name="password" value="${password_zh}" />
	</bean>
	<!--创建英国class="tags" href="/tags/JDBC.html" title=jdbc>jdbc数据源 -->
	<bean id="englishDataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${driver_en}" />
		<property name="url" value="${url_en}" />
		<property name="username" value="${username_en}" />
		<property name="password" value="${password_en}" />
	</bean>

        若determineCurrentLookupKey方法未取得任何值时,则指向defaultTargetDataSource所代表的数据源。

        需要注明的是,上述配置中的{url_en}等值来自于class="tags" href="/tags/JDBC.html" title=jdbc>jdbc.properties:

class="language-java">driver_zh=com.mysql.class="tags" href="/tags/JDBC.html" title=jdbc>jdbc.Driver
url_zh=class="tags" href="/tags/JDBC.html" title=jdbc>jdbc:mysql://localhost:3306/ruisystem
username_zh=root
password_zh=root

driver_en=com.mysql.class="tags" href="/tags/JDBC.html" title=jdbc>jdbc.Driver
url_en=class="tags" href="/tags/JDBC.html" title=jdbc>jdbc:mysql://localhost:3306/ruisystem_en
username_en=root
password_en=root
        以上工作做好后,还差最后一步,即何时给determineCurrentLookupKey()方法传值。通过DynamicDataSource类可知,该方法的值可通过外办调用setCurrentLookupKey方法设置,作者在拦截器中添加如下代码进行设置:

class="language-java">Locale locale = RequestContextUtils.getLocaleResolver(request) .resolveLocale(request);
DynamicDataSource.setCurrentLookupKey(locale.getLanguage());

        进行上述所有操作后,可以实现国际化数据库,即各个国家使用不同的数据库。


http://www.niftyadmin.cn/n/1262803.html

相关文章

(转)C\C++关于string.h头文件和string类

1.C的字符串头文件是<string.h>&#xff0c;在C里这个文件变成了<cstring>&#xff0c;string前面的c表示这个是c语言的&#xff1b;而C的字符串头文件是<string>&#xff0c;是利用的模板实现的&#xff0c;跟原先C的实现方法不一样&#xff0c;但要安全得多…

10万以上数据查询-存储过程实现

需求&#xff1a;有如下两张表&#xff0c;其中tb_web_app表中数据有十万甚至百万&#xff0c;另&#xff0c;tb_web_app表中的c_category_code关联表tb_system_category中的c_code字段。 CREATE TABLE tb_system_category (id int(11) NOT NULL AUTO_INCREMENT,c_parent_id in…

Spring MVC欢迎页

本文基于Spring MVC 注解-让Spring跑起来。 在做web项目时&#xff0c;我们通常会被要求输入域名即跳转到页面首页&#xff0c;或者输入域名background即跳转到后台登录页面&#xff0c;若使用非Spring MVC当然好说&#xff0c;在web.xml中添加如下代码即可&#xff1a; <we…

GUID类型

1.一个GUID为一个128位的整数(16字节)&#xff0c;在使用唯一标识符的情况下&#xff0c;你可以在所有计算机和网络之间使用这一整数。 2.GUID 的格式为“xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx”&#xff0c;其中每个 x 是 0-9 或 a-f 范围内的一个十六进制的数字。例如&…

进军企业服务器 Ubuntu准备好了吗?

时间&#xff1a;2010-03-08作者&#xff1a; | 博客园本文关键词&#xff1a;服务器 Ubuntu 从各方面来看&#xff0c;Red Hat都是企业Linux领域无可争议的领导者&#xff0c;但Ubuntu正在证明它也能够应付这一挑战。凭借着桌面端的良好声誉和强大影响力&#xff0c;Ubuntu正在…

适应IE、FireFox、Google浏览器的回车提交事件

function submitEvent(event) {if (event.keyCode 13 || event.which 13) {toSubmit();}}

浏览器兼容记录

1. 居中&#xff1a; 在div外面再添加一层div并设置其宽度&#xff0c;同时设置margin:0 auto可居中。 2. 设置div为inline-block: #query div {display: -moz-inline-stack;display: inline-block;zoom: 1;*display: inline; }3. 为IE和其它浏览器设置不同的属性&#xff1a;…

MySQL存储过程递归调用

有分类表tb_system_category&#xff0c;结构如下&#xff1a; CREATE TABLE tb_system_category (id int(11) NOT NULL AUTO_INCREMENT,c_parent_id int(11) NOT NULL,c_name varchar(50) NOT NULL,c_full_name varchar(200) DEFAULT NULL,c_code varchar(50) NOT NULL,c_desc…