在Spring的配置文件中,如下:
<?xml version="1.0" encoding="utf-8"?> <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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" default-autowire="byName" default-lazy-init="false"> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>/WEB-INF/database.properties</value> </list> </property> </bean> <bean id="ds1" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${cc.driver}"/> <property name="url" value="${cc.url}"/> <property name="username" value="${cc.username}"/> <property name="password" value="${cc.password}"/> </bean> <bean id="ds2" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${tt.driver}"/> <property name="url" value="${tt.url}"/> <property name="username" value="${tt.username}"/> <property name="password" value="${tt.password}"/> </bean> <bean id="dataSource" class="cn.ac.iscas.pebble.ufe.ds.DynamicDataSource"> <property name="targetDataSources"> <map key-type="java.lang.String"> <entry value-ref="ds1" key="ds1"></entry> <entry value-ref="ds2" key="ds2"></entry> </map> </property> <property name="defaultTargetDataSource" ref="ds1"></property> <!-- 默认使用ds1的数据源 --> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="ysSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mapperLocations" value="classpath*:cn/ac/iscas/pebble/ufe/mapper/*Mapper.xml"/> </bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="ysSqlSessionFactory" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="ysSqlSessionFactory"/> <property name="basePackage" value="cn.ac.iscas.pebble.ufe.inter"/> </bean> </beans>
注意上述黄色背景的部分,注意事项:
1. 其中必须是 sqlSessionFactoryBeanName 而不能是 sqlSessionFactoryBean
2. 如果是sqlSessionFactoryBeanName 则其后应该用value,而不是ref(ref对应于sqlSessionFactoryBean)
3. org.mybatis.spring.SqlSessionFactoryBean 的id 不能是 SqlSessionFactory,也不能类似SqlSessionFactory2,sqlSessionFactory或者MySqlSessionFactory,可以是ySqlSessionFactory等。
主要原因是mybatis的问题,在有mybatis的时候,Spring会先加载MapperScannerConfigurer,而使用sqlSessionFactoryBean就会报错找不到DatabaseID,因为此时还没有读取database.properties文件,sqlSessionFactoryBean在实例化的时候报错,可以使用sqlSessionFactoryBeanName,此参数不会实例化,只是声明。