一、配置读写数据源datasource
<bean id="dataSourceWrite"class="org.apache.commons.dbcp.BasicDataSource"scope="singleton">
<property name="driverClassName"value="${jdbc.driverClassName}" />
<property name="url"value="${jdbc.url}" />
<property name="username"value="${jdbc.username}" />
<property name="password"value="${jdbc.password}" />
<property name="maxActive"value="${jdbc.maxActive}" />
<property name="initialSize"value="${jdbc.initialSize}" />
<property name="validationQuery"value="select 1" />
<property name="testWhileIdle"value="true" />
<property name="minEvictableIdleTimeMillis"value="1800000" />
<property name="minIdle"value="${jdbc.minIdle}" />
<property name="maxIdle"value="${jdbc.maxIdle}" />
<property name="removeAbandoned"value="${jdbc.removeAbandoned}" />
<property name="removeAbandonedTimeout"value="${jdbc.removeAbandonedTimeout}" />
<property name="logAbandoned"value="${jdbc.logAbandoned}" />
<property name="maxWait"value="${jdbc.maxWait}" />
</bean>
<bean id="dataSourceReadOnly"class="org.apache.commons.dbcp.BasicDataSource"scope="singleton">
<property name="driverClassName"value="${jdbc.driverClassName}" />
<property name="url"value="${jdbc.url2}" />
<property name="username"value="${jdbc.username}" />
<property name="password"value="${jdbc.password}" />
<property name="maxActive"value="${jdbc.maxActive}" />
<property name="initialSize"value="${jdbc.initialSize}" />
<property name="validationQuery"value="select 1" />
<property name="testWhileIdle"value="true" />
<property name="minEvictableIdleTimeMillis"value="1800000" />
<property name="minIdle"value="${jdbc.minIdle}" />
<property name="maxIdle"value="${jdbc.maxIdle}" />
<property name="removeAbandoned"value="${jdbc.removeAbandoned}" />
<property name="removeAbandonedTimeout"value="${jdbc.removeAbandonedTimeout}" />
<property name="logAbandoned"value="${jdbc.logAbandoned}" />
<property name="maxWait"value="${jdbc.maxWait}" />
</bean>
二、配置读写sqlsessionfactory
<bean id="writeSqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource"ref="dataSourceWrite"/>
<property name="configLocation"value="classpath:mybatis/sqlmap-config.xml"/>
<property name="mapperLocations"value="classpath:mybatis/write/**/*.xml"/>
<property name="plugins">
<array>
<bean class="com.lifang.wkeyes.DbAccessInterceptor">
</bean>
</array>
</property>
</bean>
<bean id="readSqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource"ref="dataSourceReadOnly"/>
<property name="configLocation"value="classpath:mybatis/sqlmap-config.xml"/>
<property name="mapperLocations"value="classpath:mybatis/read/**/*.xml"/>
<property name="plugins">
<array>
<bean class="com.lifang.wkeyes.DbAccessInterceptor">
</bean>
</array>
</property>
</bean>
三、指定读写的相应的mapper类,MapperScannerConfigurer
<bean id="writeMapperScannerConfigurer"class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage"value="com.lifang.crm.dao.write"/>
<property name="sqlSessionFactoryBeanName"value="writeSqlSessionFactory"/>
</bean>
<bean id="readMapperScannerConfigurer"class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage"value="com.lifang.crm.dao.read"/>
<property name="sqlSessionFactoryBeanName"value="readSqlSessionFactory"/>
</bean>