问题描述
我的项目要使用两个数据源这样的话,自然就出来的两套sessionfactory了.<!--以下是mysql的DAO--><beanid="quartsPlanDao"class="com.hpb.quarts.mysql.dao.impl.QuartsPlanDaoImpl"><propertyname="sqlSessionFactory"ref="sqlSessionFactoryMysql"></property></bean><beanid="hpbMoneyInfoDao"class="com.hpb.quarts.mysql.dao.impl.HpbMoneyInfoDaoImpl"><propertyname="sqlSessionFactory"ref="sqlSessionFactoryMysql"></property></bean>------------------------<!--以下是mysql的DAO--><beanid="memAccessIssueRuleDao"class="com.hpb.quarts.oracle.dao.impl.MemAccessIssueRuleDaoImpl"><propertyname="sqlSessionFactory"ref="sqlSessionFactoryOracle"></property></bean><beanid="sysIssuePlanDao"class="com.hpb.quarts.oracle.dao.impl.SysIssuePlanDaoImpl"><propertyname="sqlSessionFactory"ref="sqlSessionFactoryOracle"></property></bean>我怎么用component或扫描的方式.注入这个sqlsessionFactory这样的话,就不用我每写一个DAO,要到这里来写的BEAN了.他们的包是不同的..很久没登这论坛了.没想到我有这么多的分.
解决方案
解决方案二:
想要不重复这么写,可以搞两个baseDao继承来用
解决方案三:
AbstractRoutingDataSource配合aop是实现多数据源切换.可以最大程度降低代码的耦合,不管是service,dao本身都无需关注数据源问题.不同的数据源仅仅是切点特征不同而以,比如:service方法名.mysqlXXX查询mysql数据源,oracleXXX查询oracle数据源
解决方案四:
@Repository("quartsPlanDao")publicclassQuartsPlanDaoImplimplementsQuartsPlanDao{@ResourceSessionFactorysqlSessionFactoryMysql;}@Repository("sysIssuePlanDao")publicclassSysIssuePlanDaoImplimplementsSysIssuePlanDao{@ResourceSessionFactorysqlSessionFactoryOracle;}
Springconfigxml:<context:component-scanbase-package="com.hpb.quarts"/>
不会这么简单吧,我理解错了?
解决方案五:
貌似我配过多个数据源,我看分来的~
解决方案六:
<?xmlversion="1.0"encoding="utf-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsd"><beanclass="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><propertyname="locations"><list><value>classpath:applicationContext.properties</value></list></property></bean><!--使用annotation自动注册bean,并保证@Required、@Autowired的属性被注入--><context:component-scanbase-package="com.jfpal"></context:component-scan><!--数据源--><beanid="dataSourceOne"class="com.mchange.v2.c3p0.ComboPooledDataSource"><propertyname="driverClass"value="${jdbc.driver}"></property><propertyname="jdbcUrl"value="${jdbc.url}"></property><propertyname="user"value="${jdbc.username}"/><propertyname="password"value="${jdbc.password}"/><propertyname="minPoolSize"value="1"/><propertyname="maxPoolSize"value="12"/><propertyname="maxIdleTime"value="1800"/><propertyname="acquireIncrement"value="2"/><propertyname="maxStatements"value="10"/><propertyname="initialPoolSize"value="2"/><propertyname="idleConnectionTestPeriod"value="1800"/><propertyname="acquireRetryAttempts"value="30"/><propertyname="acquireRetryDelay"value="100"/><propertyname="breakAfterAcquireFailure"value="false"/><propertyname="testConnectionOnCheckout"value="false"/></bean><!--数据源2--><beanid="dataSourceTwo"class="com.mchange.v2.c3p0.ComboPooledDataSource"><propertyname="driverClass"value="${jdbc.driver2}"></property><propertyname="jdbcUrl"value="${jdbc.url2}"></property><propertyname="user"value="${jdbc.username2}"/><propertyname="password"value="${jdbc.password2}"/><propertyname="minPoolSize"value="1"/><propertyname="maxPoolSize"value="12"/><propertyname="maxIdleTime"value="1800"/><propertyname="acquireIncrement"value="2"/><propertyname="maxStatements"value="10"/><propertyname="initialPoolSize"value="2"/><propertyname="idleConnectionTestPeriod"value="1800"/><propertyname="acquireRetryAttempts"value="30"/><propertyname="acquireRetryDelay"value="100"/><propertyname="breakAfterAcquireFailure"value="false"/><propertyname="testConnectionOnCheckout"value="false"/></bean><beanid="dynamicDataSource"class="com.jfpal.framework.dao.DynamicDataSource"><!--通过key-value的形式来关联数据源--><propertyname="targetDataSources"><map><entryvalue-ref="dataSourceOne"key="dataSourceOne"></entry><entryvalue-ref="dataSourceTwo"key="dataSourceTwo"></entry></map></property><propertyname="defaultTargetDataSource"ref="dataSourceOne"/></bean><beanid="dataSourceInterceptor"class="com.jfpal.framework.interceptor.DataSourceInterceptor"></bean><aop:config><aop:aspectid="dataSourceAspect"ref="dataSourceInterceptor"><aop:pointcutid="switch"expression="execution(*com.jfpal.pmout.payterminal.service.*.*.*(..))"/><!--进入方法前设置为数据源2--><aop:beforepointcut-ref="switch"method="setdataSourceTwo"/><!--退出方法,设置回数据源1--><aop:afterpointcut-ref="switch"method="setdataSourceOne"/></aop:aspect></aop:config><!--SqlSessionFactory配置--><beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"><propertyname="dataSource"ref="dynamicDataSource"/><!--自动扫描entity目录,省掉Configuration.xml里的手工配置--><propertyname="typeAliasesPackage"value="com.jfpal"/><!--显式指定Mapper文件位置--><propertyname="mapperLocations"value="classpath*:mybatis/**/*Mapper.xml"/><propertyname="configurationProperties"><props><propkey="dialect">oracle</prop></props></property></bean><beanid="sqlSession"class="org.mybatis.spring.SqlSessionTemplate"><constructor-argindex="0"ref="sqlSessionFactory"/></bean><!--================================事务相关控制=================================================--><beanname="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><propertyname="dataSource"ref="dynamicDataSource"></property></bean><tx:adviceid="userTxAdvice"transaction-manager="transactionManager"><tx:attributes><tx:methodname="insert*"propagation="REQUIRED"read-only="false"rollback-for="java.lang.Exception"no-rollback-for="java.lang.RuntimeException"/><tx:methodname="select*"propagation="REQUIRED"read-only="false"rollback-for="java.lang.Exception"no-rollback-for="java.lang.RuntimeException"/><tx:methodname="find*"propagation="REQUIRED"read-only="false"rollback-for="java.lang.Exception"no-rollback-for="java.lang.RuntimeException"/><tx:methodname="get*"propagation="REQUIRED"read-only="false"rollback-for="java.lang.Exception"no-rollback-for="java.lang.RuntimeException"/><tx:methodname="save*"propagation="REQUIRED"read-only="false"rollback-for="java.lang.Exception"no-rollback-for="java.lang.RuntimeException"/><tx:methodname="delete*"propagation="REQUIRED"read-only="false"rollback-for="java.lang.Exception"no-rollback-for="java.lang.RuntimeException"/></tx:attributes></tx:advice></beans>
全部配置在这里了,具体数据库配置就不用了吧?
解决方案七:
多套SessionFactory必须是可以的;你用Spring3.x来做的话直接用注解把SessionFactory注入到各种Dao中即可。
解决方案八:
引用1楼wobuxiangnila的回复:
想要不重复这么写,可以搞两个baseDao继承来用
现在用的就是继承.但是,父类是注入进去的.子类,出来的时候,非spring对象.
解决方案九:
引用2楼whos2002110的回复:
AbstractRoutingDataSource配合aop是实现多数据源切换.可以最大程度降低代码的耦合,不管是service,dao本身都无需关注数据源问题.不同的数据源仅仅是切点特征不同而以,比如:service方法名.mysqlXXX查询mysql数据源,oracleXXX查询oracle数据源
是的.事务的部分是这样配置的.<aop:pointcutid="appService"expression="execution(*com.hpb..*.*MService*(..))"/>因为他支持表达式.
解决方案十:
引用3楼zhangjihao的回复:
@Repository("quartsPlanDao")publicclassQuartsPlanDaoImplimplementsQuartsPlanDao{@ResourceSessionFactorysqlSessionFactoryMysql;}@Repository("sysIssuePlanDao")publicclassSysIssuePlanDaoImplimplementsSysIssuePlanDao{@ResourceSessionFactorysqlSessionFactoryOracle;}Springconfigxml:<context:component-scanbase-package="com.hpb.quarts"/>
不会这么简单吧,我理解错了?
你的思路应该是对的..开始的时候,我也是这样配置..但是,不知道为什么注入不进去.而且,sqlsession不是一个属性.它只是那个support的一个SET方法.我不理解,为什么就注入不进去了.就是想用这样的扫描方式但是我没试成功