国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

Spring+Mybatis之多數據源配置

2024-07-21 02:52:23
字體:
來源:轉載
供稿:網友

同一applicationContext.xml中:

1,配置多個數據源(采用連接池c3p0,直接jdbc連也行)

數據源1:dataSource1

<bean id = "dataSource1" class= "com.mchange.v2.c3p0.ComboPooledDataSource" lazy-init = "default">

<PRoperty name="driverClass" value = ""/>

...

...

</bean>

數據源2:dataSource2

<bean id = "dataSource2" class= "com.mchange.v2.c3p0.ComboPooledDataSource" lazy-init = "default">

<property name="driverClass" value = ""/>

...

...

</bean>

2, 配置mybatis數據會話管理(sqlsessionFactoryBean)

會話factory1:sqlSessionFactoryBean1

<bean id = "sqlSessionFactoryBean1" class = "mybatis.spring.SqlSessionFactoryBean">

<property name = "mapperLocations">

<list>

//不同factory,盡量保持其mapper的路徑不同

<value>classpath:mapper/*Mapper.xml</value> //classpath指的是:src/main/resources(maven項目文件包結構哦)

</list>

</property>

<property name = "dataSource" ref = "dataSource1"></property>

</bean>

會話factory2:sqlSessionFactoryBean2

<bean id = "sqlSessionFactoryBean2" class = "mybatis.spring.SqlSessionFactoryBean">

<property name = "mapperLocations">

<list>

<value>classpath:mapper/*Mapper.xml</value> 

</list>

</property>

<property name = "dataSource" ref = "dataSource2"></property>

</bean>

3, 配置Dao接口層(重點注意)

mybatis用mapperScannerConfigurer掃描相關的dao(或mapper)的interface的包,將其注入到spring進行管理;

dao層需要注入第2步配置的sessionFactory來獲得session(session中加入事物等操作),跟sessionFactory中配置的*Mapper.xml進行一一匹配;

mapperScanner1:

<bean id="mapperScanner1" class="org.mybatis.spring.mapper.MapperScannerConfigurer">

//不同數據源的掃描dao層不要放在一起,不然數據源1掃描了數據源2的dao,那 去數據源1的mappe.xml中匹配時,會找不到相應方法,而報錯!!!

<property name = "basePackage" value = "com.business.*.*dao1"/> //配置dao層需注入的sessionFactory<property name = "sqlSessionFactoryBeanName" value = "sqlSessionFactoryBean1"/>

</bean>

mapperScanner2:

<bean id="mapperScanner2" class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<property name = "basePackage" value = "com.business.*.*dao2"/> <property name = "sqlSessionFactoryBeanName" value = "sqlSessionFactoryBean2"/>

</bean>

其他配置:事物配置(dataSourceTransactionManager),持久化模板(SqlSessionTemplate)

常見錯誤1:

No unique bean of type [org.apache.ibatis.sessioin.SqlSessionFactory] is defined:expected single matching bean but found 2:[sqlSessionFactoryBean1,sqlSessionFactoryBean2]

解決問題:檢查步驟3配置,是否有配置sqlSessionFactoryBeanName

常見錯誤2:

...Dao conflicts with existing, non-compatible bean definition of same name and class[org.mybatis.spring.mapper.MapperFactoryBean]

解決問題:檢查步驟3配置,屬性basePackage,兩個數據源是否掃描了同一dao層包,若是,更改dao層包,不同數據源需掃描不同的dao層包。

參考:http://stackoverflow.com/questions/4746766/spring-and-mybatis-multiple-data-sources-setup


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新河县| 宣武区| 马关县| 宜君县| 大化| 萝北县| 兴山县| 宁强县| 溧阳市| 高阳县| 红河县| 万州区| 汨罗市| 苍山县| 高安市| 鸡东县| 桐梓县| 屏南县| 抚宁县| 澄江县| 徐汇区| 周至县| 大名县| 双辽市| 永城市| 永善县| 西藏| 霍林郭勒市| 紫云| 永新县| 公安县| 石城县| 东至县| 安福县| 博湖县| 凤凰县| 阳朔县| 巧家县| 驻马店市| 仙游县| 盱眙县|