网上大部分是ssm的多数据源都是采用spring AOP实现,我用的时候遇到不少问题;所以现找到一种更好更简单的方式:刚开始采用不知道会不会有什么问题:大家欢迎留言你的观点哦。
最简单的mybatis多数据源:接口绑定mapper实现
1spring配置文件:
<bean name="dsPay" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="url" value="${pay.jdbc.url}" />
<property name="username" value="${pay.jdbc.username}" />
<property name="password" value="${pay.jdbc.password}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="${pool.initialSize}" />
<!-- 连接池最大使用连接数量 -->
<property name="maxActive" value="${pool.maxActive}" />
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${pool.minIdle}" />
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${pool.maxWait}" />
<property name="poolPreparedStatements" value="${pool.poolPreparedStatements}" />
<property name="maxPoolPreparedStatementPerConnectionSize"
value="${pool.maxPoolPreparedStatementPerConnectionSize}" />
<!-- 用来检测有效sql -->
<property name="validationQuery" value="${pool.validationQuery}" />
<property name="testOnBorrow" value="${pool.testOnBorrow}" />
<property name="testOnReturn" value="${pool.testOnReturn}" />
<property name="testWhileIdle" value="${pool.testWhileIdle}" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="${pool.timeBetweenEvictionRunsMillis}" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="${pool.minEvictableIdleTimeMillis}" />
<!-- 打开removeAbandoned功能 -->
<property name="removeAbandoned" value="${pool.removeAbandoned}" />
<!-- 1800秒,也就是30分钟 -->
<property name="removeAbandonedTimeout" value="${pool.removeAbandonedTimeout}" />
<!-- 关闭abanded连接时输出错误日志 -->
<property name="logAbandoned" value="${pool.logAbandoned}" />
<!-- 监控数据库 -->
<property name="filters" value="${pool.filters}" />
</bean>
<bean name="dsModel" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="url" value="${model.jdbc.url}" />
<property name="username" value="${model.jdbc.username}" />
<property name="password" value="${model.jdbc.password}" />
</bean>
<!-- myBatis文件 -->
<bean id="ssf_pay" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dsPay" />
<!-- <property name="configLocation" value="classpath:applicationContext-mybatis.xml"/> -->
<property name="mapperLocations" value="classpath:lzs/pay/order/dao/*Mapper.xml" />
</bean>
<bean id="ssf_model" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dsModel" />
<property name="mapperLocations" value="classpath:lzs/model/*/dao/*Mapper.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="lzs.pay.order.dao" />
<property name="sqlSessionFactoryBeanName" value="ssf_pay" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="lzs.model.*.dao" />
<property name="sqlSessionFactoryBeanName" value="ssf_model" />
</bean>
<!-- 配置事务管理器 -->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dsPay" />
</bean>
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dsModel" />
</bean>
2.service层:
@Override
public PayOrderSubmit createOrder(String clieid,String itemid,String note,String lang,int paytype) {
PayOrderSubmit order = new PayOrderSubmit();
order.setPayid(getPaySerialNO());
// order.setOrdertype(PayConst.ORDERTYPE_ICU);
ModExpense exp = modExpenseService.getById(Integer.valueOf(itemid));
order.setClieid(clieid);
order.setExpeid(Integer.valueOf(itemid));
int add = add(order);
}
这样,当在service层调用其他数据源操作时,比如:ModExpense exp = modExpenseService.getById(Integer.valueOf(itemid));
mybaits就会自动使用dsModel数据源操作了 ,是不是很简单。具体的底层原理我不清楚,此博只为学习方便工作。