求大神帮忙 spring + mybatis 动态切换数据源?
请教个问题.谁有做过 spring + mybatis 动态切换数据源?
需求如下:
1. 有同样的表结构,和实体类数据Dao,就是数据库不一样,能够同时操作(最好只配置一个数据源配置,通过代码来切换不同的jdbcurl,user,password)
2. 不同的表结构,不同的数据库,(这个可以通过配置XML实现)
3. 要实现 事务
求帮忙 !!!! Spring MyBatis --------------------编程问答-------------------- 学习。 --------------------编程问答-------------------- 很实用的一个需求....
希望大神能够帮帮忙!!!!!
我的结贴率,都是因为大家回答不了问题....才没有结....只要有回答,我肯定结.... --------------------编程问答-------------------- 学习一下 --------------------编程问答-------------------- 求大神帮忙 .... 有没有相应的解决方案...
我把另一个链接也发上来. 分虽然不够. 帮上忙的.我另一个贴 的.100分也一起给了.
--------------------编程问答-------------------- 有高手.帮忙 解决吗? --------------------编程问答-------------------- 这个是公司需求,?还是你自己想的,就是改下数据库地址 在配置文件中,一帮的分页了什么关键字 因为用的mybatis 都会默认通用的 --------------------编程问答-------------------- 现在有这样的业务需求 --------------------编程问答-------------------- 在spring中配置两个数据源,自己写个类继承MethodBeforeAdvice类, 在方法before中判断选择数据源,最后在spring中配置AOP监控。 --------------------编程问答-------------------- 这是spring的配置文件 你只做个参考
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
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.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:property-placeholder location="file:${catalina.home}/webapps/ROOT/bas.properties" ignore-unresolvable="true"/>
<!--<context:property-placeholder location="file:${catalina.home}/webapps/bas/bas.properties" ignore-unresolvable="true"/>-->
<!-- <context:property-placeholder location="file:${catalina.home}/ebas/conf/bas.properties" ignore-unresolvable="true"/> -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!--
<context:property-placeholder location="classpath*:bas.properties" ignore-unresolvable="true"/>
<context:property-placeholder location="file:${user.dir}/bas.properties" ignore-unresolvable="true"/>
<context:property-placeholder location="file:${catalina.home}/conf/bas.properties" ignore-unresolvable="true"/>
-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<!-- <property name="jdbcUrl" value="jdbc:sqlserver://10.36.65.5;databaseName=LocalDataAnalyticalSystem" /> -->
<property name="user" value="${user.name}" />
<property name="password" value="${user.password}" />
<property name="autoCommitOnClose" value="true" />
<property name="checkoutTimeout" value="5000" />
<property name="initialPoolSize" value="10" />
<property name="minPoolSize" value="10" />
<property name="maxPoolSize" value="100" />
<property name="maxIdleTime" value="25000" />
<property name="acquireIncrement" value="5" />
<property name="maxIdleTimeExcessConnections" value="4800" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:/mybatis/mybatis.xml"></property>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!-- Single realm app (realm configured next, below). If you have multiple realms, use the 'realms'
property instead. -->
<property name="realm" ref="sampleRealm"/>
</bean>
<bean id="sampleRealm" class="com.belter.bas.system.manage.service.impl.SampleRealm"/>
<!-- Post processor that automatically invokes init() and destroy() methods -->
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<property name="loginUrl" value="/tlogin.jsp"/>
<property name="successUrl" value="/page/home.jsp"/>
<!--<property name="unauthorizedUrl" value="/index.jsp" />-->
<property name="filters">
<util:map>
<entry key="authc">
<bean class="org.apache.shiro.web.filter.authc.PassThruAuthenticationFilter"/>
</entry>
</util:map>
</property>
<property name="filterChainDefinitions">
<value>
/resources/** = anon
/public/** = anon
/service/** = anon
/services/** = anon
/luccshc/** = anon
/page/** = authc
/sys/** = authc
</value>
</property>
</bean>
</beans> --------------------编程问答-------------------- 到我的博客看如何用spring来切换数据源.这个跟底层查询没多大关系,切换了数据源,然后你再调用的方法查询是第二个数据源的内容就可以了...重点要注意的是事务问题
补充:Java , Web 开发