您的位置:首页 > 编程语言 > Java开发

spring多数据源配置

2016-08-29 11:32 447 查看
项目中我们经常会遇到多数据源的问题,尤其是数据同步或定时任务等项目更是如此。多数据源让人最头痛的,不是配置多个数据源,而是如何能灵活动态的切换数据源。绑定给sessionFactory,在dao层代码中再指定sessionFactory来进行数据库操作。



正如上图所示,每一块都是指定绑死的,如果是多个数据源,也只能是下图中那种方式。



可看出在Dao层代码中写死了两个SessionFactory,这样日后如果再多一个数据源,还要改代码添加一个SessionFactory,显然这并不符合开闭原则。
那么正确的做法应该是下载



代码:下载
1. applicationContext.xml
[html] view plain copy

xml version="1.0" encoding="UTF-8"?>

<< span="">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:cache="http://www.springframework.org/schema/cache"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"

xmlns:jms="http://www.springframework.org/schema/jms" xmlns:lang="http://www.springframework.org/schema/lang"

xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:oxm="http://www.springframework.org/schema/oxm"

xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task"

xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.1.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">

<< span="">context:annotation-config />

<< span="">context:component-scan base-package="com">context:component-scan>

<< span="">bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<< span="">property name="locations">

<< span="">list>

<< span="">value>classpath:com/resource/config.propertiesvalue>

list>

property>

bean>

<< span="">bean id="dataSourceOne" class="com.mchange.v2.c3p0.ComboPooledDataSource"

destroy-method="close">

<< span="">property name="driverClass" value="${dbOne.jdbc.driverClass}" />

<< span="">property name="jdbcUrl" value="${dbOne.jdbc.url}" />

2. DynamicDataSource.class下载
[java] view plain copy

package com.core;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

public class DynamicDataSource extends AbstractRoutingDataSource{

@Override

protected Object determineCurrentLookupKey() {

return DatabaseContextHolder.getCustomerType();

}

}

3. DatabaseContextHolder.class下载
[java] view plain copy

package com.core;

public class DatabaseContextHolder {

private static final ThreadLocal contextHolder = new ThreadLocal();

public static void setCustomerType(String customerType) {

contextHolder.set(customerType);

}

public static String getCustomerType() {

return contextHolder.get();

}

public static void clearCustomerType() {

contextHolder.remove();

}

}

4. DataSourceInterceptor.class
[java] view plain copy

package com.core;

import org.aspectj.lang.JoinPoint;

import org.springframework.stereotype.Component;

@Component

public class DataSourceInterceptor {

public void setdataSourceOne(JoinPoint jp) {

DatabaseContextHolder.setCustomerType("dataSourceOne");

}

public void setdataSourceTwo(JoinPoint jp) {

DatabaseContextHolder.setCustomerType("dataSourceTwo");

}

}

5. po实体类下载
[java] view plain copy

package com.po;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.Id;

import javax.persistence.Table;

@Entity

@Table(name = "BTSF_BRAND", schema = "hotel")

public class Brand {

private String id;

private String names;

private String url;

@Id

@Column(name = "ID", unique = true, nullable = false, length = 10)

public String getId() {

return this.id;

}

public void setId(String id) {

this.id = id;

}

@Column(name = "NAMES", nullable = false, length = 50)

public String getNames() {

return this.names;

}

public void setNames(String names) {

this.names = names;

}

@Column(name = "URL", length = 200)

public String getUrl() {

return this.url;

}

public void setUrl(String url) {

this.url = url;

}

}

[java] view plain copy

package com.po;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.Id;

import javax.persistence.Table;

@Entity

@Table(name = "CITY", schema = "car")

public class City {

private Integer id;

private String name;

@Id

@Column(name = "ID", unique = true, nullable = false)

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

@Column(name = "NAMES", nullable = false, length = 50)

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

6. BrandDaoImpl.class下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息