首先:声明一下,公司使用的项目是xml的配置bean,所以只能继续做,没办法注解使用。所以同行别鄙视啊。具体配置如下,错误提示如下。
(动态多个数据源虽然存在,但是每次都会有默认的某一个数据源创建sqlSessionFactory那么就不存在多个数据连接冲突的问题啊。)
具体service层对象创建的xml
<?xml version="1.0" encoding="UTF-8"?>
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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zst.cloudzst.mapper.user" />
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<bean id="conferencecallService" class="com.zst.cloudzst.service.user.ConferencecallServiceImpl">
<property name="conferencecallMapper" ref="conferencecallMapper" />
</bean>
</beans>
applicationContext的配置文件
<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<aop:aspectj-autoproxy proxy-target-class="true" />
<!-- 读取配置文件 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"/>
</bean>
<!-- <bean id="dataSourceLocalHost" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>${jdbc_portal_driver}</value>
</property>
<property name="url">
<value>${jdbc_portal_url}</value>
</property>
<property name="username">
<value>${jdbc_portal_user}</value>
</property>
<property name="password">
<value>${jdbc_portal_password}</value>
</property>
<property name="maxActive" value="100"></property>
<property name="maxIdle" value="30"></property>
<property name="maxWait" value="500"></property>
<property name="defaultAutoCommit" value="true"></property>
</bean> -->
<!-- ccm数据库连接池 -->
<bean id="dataSource_ccm" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>${jdbc_driver}</value>
</property>
<property name="url">
<value>${jdbc_ccm_url}</value>
</property>
<property name="username">
<value>${jdbc_ccm_user}</value>
</property>
<property name="password">
<value>${jdbc_ccm_password}</value>
</property>
<property name="maxActive" value="100"></property>
<property name="maxIdle" value="30"></property>
<property name="maxWait" value="500"></property>
<property name="defaultAutoCommit" value="true"></property>
</bean>
<!-- cp数据库连接池 -->
<bean id="dataSource_cp" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>${jdbc_driver}</value>
</property>
<property name="url">
<value>${jdbc_cp_url}</value>
</property>
<property name="username">
<value>${jdbc_cp_user}</value>
</property>
<property name="password">
<value>${jdbc_cp_password}</value>
</property>
<property name="maxActive" value="100"></property>
<property name="maxIdle" value="30"></property>
<property name="maxWait" value="500"></property>
<property name="defaultAutoCommit" value="true"></property>
</bean>
<!-- scm数据库连接池 -->
<bean id="dataSource_scm" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>${jdbc_driver}</value>
</property>
<property name="url">
<value>${jdbc_scm_url}</value>
</property>
<property name="username">
<value>${jdbc_scm_user}</value>
</property>
<property name="password">
<value>${jdbc_scm_password}</value>
</property>
<property name="maxActive" value="100"></property>
<property name="maxIdle" value="30"></property>
<property name="maxWait" value="500"></property>
<property name="defaultAutoCommit" value="true"></property>
</bean>
<!-- portal数据库连接池 -->
<bean id="dataSource_portal" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>${jdbc_portal_driver}</value>
</property>
<property name="url">
<value>${jdbc_portal_url}</value>
</property>
<property name="username">
<value>${jdbc_portal_user}</value>
</property>
<property name="password">
<value>${jdbc_portal_password}</value>
</property>
<property name="maxActive" value="100"></property>
<property name="maxIdle" value="30"></property>
<property name="maxWait" value="500"></property>
<property name="defaultAutoCommit" value="true"></property>
</bean>
<bean id="dynamicDataSource" class="com.zst.cloudzst.util.DynamicDataSource" >
<!-- 通过key-value的形式来关联数据源 -->
<property name="targetDataSources">
<map>
<entry value-ref="dataSource_ccm" key="dataSource_ccm"></entry>
<entry value-ref="dataSource_cp" key="dataSource_cp"></entry>
<entry value-ref="dataSource_scm" key="dataSource_scm"></entry>
<!-- <entry value-ref="dataSourceLocalHost" key="dataSourceLocalHost"></entry> -->
<!-- <entry value-ref="dataSource_portal" key="dataSource_portal"></entry> -->
</map>
</property>
<property name="defaultTargetDataSource" ref="dataSource_ccm" />
</bean>
<!-- 事务配置 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dynamicDataSource" />
</bean>
<!-- 实体映射类-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dynamicDataSource" />
<property name="typeAliasesPackage" value="com.zst.cloudzst.vo,com.zst.cloudzst.model" />
</bean>
<bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" />
</beans>
接口对应的XML文件,在对应的com.zst.cloudzst.mapper.user包下
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
异常提示信息:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'conferencecallService' defined in class path resource [springConfig/applicationContext-conferencecall.xml]: Cannot resolve reference to bean 'conferencecallMapper' while setting bean property 'conferencecallMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'conferencecallMapper' is defined