spring mvc中使用动态数据源
来源:互联网 发布:返利网淘宝返利比例 编辑:程序博客网 时间:2024/05/22 00:07
实际应用中有这么个需求:根据用户所属的租户,来切换到租户对应的数据库(一个租户一套数据库,数据表都一样,存放的schema不同而已)
下面是配置的代码
1.动态数据源的切换,使用spring提供的AbstractRoutingDataSource接口,实现determineCurrentLookupKey()方法
public class DynamicDataSource extends AbstractRoutingDataSource {private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();public static String getCurrentLookupKey() {return (String) contextHolder.get();}public static void setCurrentLookupKey(String currentLookupKey) {contextHolder.set(currentLookupKey);}@Overrideprotected Object determineCurrentLookupKey() {return getCurrentLookupKey();}}
2.在applicationContext.xml中为每个租户写一个对应的数据源
<bean id="baseDataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="${jdbc.driver}" /><property name="url" value="${jdbc.urla}" /><property name="username" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /></bean><bean id="aDataSource" parent="baseDataSource"><property name="url" value="${jdbc.urla}" /></bean><bean id="bDataSource" parent="baseDataSource"><property name="url" value="${jdbc.urlb}" /></bean><bean id="dataSource" class="com.baosight.iframework.common.DynamicDataSource"><property name="targetDataSources"><map key-type="java.lang.String"><entry key="a" value-ref="aDataSource" /><entry key="b" value-ref="bDataSource" /></map></property><property name="defaultTargetDataSource" ref="aDataSource" /> </bean>配置中使用了属性继承,因为唯一变动的只有url连接,dataSource具体使用哪个是根据传来的key来决定,下面我们看下如何传key
3.调用数据访问层前,都先传dataSource的key
public class DynamicDataSourceInterceptor {public void setdataSource(JoinPoint jp) {HttpSession s = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession();String t = (String) s.getAttribute("tenant");if (t.equals("tenant1")) {DynamicDataSource.setCurrentLookupKey("a");}if (t.equals("tenant2")) {DynamicDataSource.setCurrentLookupKey("b");}}}
上述的代码就是指定key的过程,因为用户的信息保存在session中,所以使用了session,下面的工作就是把上述的代码织入到service层的所有方法
<aop:config> <aop:aspect id="dsAspect" ref="dsInterceptor"> <aop:pointcut id="businessService" expression="execution(* com.jacky.service.*.*(..))" /> <aop:before pointcut-ref="businessService" method="setdataSource"/> </aop:aspect> </aop:config> <bean id="dsInterceptor" class="com.jacky.DynamicDataSourceInterceptor" />aop的作用就是每次调用service的方法前都先切换数据库
这样就完成了。我刚开始把aop切面放到controller层没有效果,要放到service层才行。
0 0
- spring mvc中使用动态数据源
- spring mvc 动态数据源
- spring mvc项目,部署tomcat 中,使用jndi数据源
- spring mvc 配置DataSource以及动态数据源
- Spring MVC+分布式事务+动态数据源配置
- Spring mvc 动态数据源的配置
- Spring中使用数据源
- Spring代码中动态切换数据源
- Spring动态切换数据源
- spring动态数据源1
- SPRING动态数据源使用方法
- spring 动态数据源切换
- Spring动态数据源
- spring 动态注册数据源
- spring 动态数据源不起作用
- spring配置动态数据源
- Spring 动态数据源配置
- spring动态加载数据源
- C++常见用法规范整理
- 用iptables -ADC 来指定链的规则
- java的程序教程!
- 对用用户密码的保留方式
- hdu 1054 Strategic Game
- spring mvc中使用动态数据源
- android 设置Spinner文字标题颜色 字体大小样式
- ava程序员认证
- java jar 图片 打包 引用 路径
- 同源建模总结(一) automodel类的使用
- IOS屏幕旋转详解(自动旋转、手动旋转、兼容IOS6之前系统)
- Cocos2d场景切换效果汇总
- 使用循环数组高效的实现队列类
- jQuery 图片轮播插件