spring+springMVC+mybatis 多数据源配置

虾米姐 阅读:618 2021-03-31 21:44:45 评论:0

本文参考借鉴:http://zhuchengzzcc.iteye.com/blog/1827633

一、项目目录结构:



spring-context.xml 配置文件内容如下:

<?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:p="http://www.springframework.org/schema/p" 
	xmlns:context="http://www.springframework.org/schema/context" 
	xmlns:mvc="http://www.springframework.org/schema/mvc" 
	xmlns:tx="http://www.springframework.org/schema/tx" 
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xsi:schemaLocation="http://www.springframework.org/schema/beans   
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd   
                        http://www.springframework.org/schema/context   
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd   
                        http://www.springframework.org/schema/tx 
                        http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
                        http://www.springframework.org/schema/mvc   
                        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
                        http://www.springframework.org/schema/aop  
                        http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">    
	<!-- 引入配置文件 --> 
	<bean id="propertyConfigurer" 
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
		<property name="locations"> 
			<list>   
                <value>classpath:jdbc.properties</value>   
                <value>classpath:memcached.properties</value>   
            </list>  
		</property> 
	</bean>	 
	<!--wlsq_oauth 数据源--> 
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" 
		init-method="init" destroy-method="close"> 
		<!-- 基本属性 url、user、password --> 
		<property name="url" value="${url}" /> 
		<property name="username" value="${username}" /> 
		<property name="password" value="${password}" /> 
 
		<!-- 配置初始化大小、最小、最大 --> 
		<property name="initialSize" value="1" /> 
		<property name="minIdle" value="1" /> 
		<property name="maxActive" value="20" /> 
 
		<!-- 配置获取连接等待超时的时间 --> 
		<property name="maxWait" value="60000" /> 
 
		<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> 
		<property name="timeBetweenEvictionRunsMillis" value="60000" /> 
 
		<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> 
		<property name="minEvictableIdleTimeMillis" value="300000" /> 
 
		<property name="validationQuery" value="SELECT 'x'" /> 
		<property name="testWhileIdle" value="true" /> 
		<property name="testOnBorrow" value="false" /> 
		<property name="testOnReturn" value="false" /> 
 
		<!-- 打开PSCache,并且指定每个连接上PSCache的大小 --> 
		<property name="poolPreparedStatements" value="true" /> 
		<property name="maxPoolPreparedStatementPerConnectionSize" 
			value="20" /> 
 
		<!-- 配置监控统计拦截的filters --> 
		<property name="filters" value="stat" /> 
	</bean> 
 
	<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> 
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
		<property name="dataSource" ref="dataSource" /> 
		<!-- 自动扫描mapping.xml文件 --> 
		<property name="mapperLocations" value="classpath:com/wlsq/kso/mapper/*.xml"></property> 
	</bean> 
 
	<!-- DAO接口所在包名,Spring会自动查找其下的类 --> 
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 
		<property name="basePackage" value="com.wlsq.kso.dao" /> 
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> 
	</bean> 
 
	<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --> 
	<bean id="transactionManager" 
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
		<property name="dataSource" ref="dataSource" /> 
		<qualifier value="isap" />   
	</bean> 
	<!-- 全注解方式   需加上@Transactional -->   
    <tx:annotation-driven transaction-manager="transactionManager" />   
	<!--   
    <tx:advice id="tx" 
        transaction-manager="transactionManager"> 
        <tx:attributes> 
            <tx:method name="delete*" propagation="REQUIRED" /> 
            <tx:method name="insert*" propagation="REQUIRED" /> 
            <tx:method name="update*" propagation="REQUIRED" /> 
            <tx:method name="find*" read-only="true" /> 
            <tx:method name="select*" read-only="true" /> 
        </tx:attributes> 
    </tx:advice> 
 
    <aop:config> 
        <aop:pointcut id="pc" expression="execution(* com.wlsq.kso.service.*.*(..))" />        
        <aop:advisor pointcut-ref="pc" advice-ref="tx" /> 
    </aop:config> 
    --> 
    <!--xclode 数据源  --> 
    <bean id="dataSource2" class="com.alibaba.druid.pool.DruidDataSource" 
		init-method="init" destroy-method="close"> 
		<!-- 基本属性 url、user、password --> 
		<property name="url" value="${url2}" /> 
		<property name="username" value="${username2}" /> 
		<property name="password" value="${password2}" /> 
 
		<!-- 配置初始化大小、最小、最大 --> 
		<property name="initialSize" value="1" /> 
		<property name="minIdle" value="1" /> 
		<property name="maxActive" value="20" /> 
 
		<!-- 配置获取连接等待超时的时间 --> 
		<property name="maxWait" value="60000" /> 
 
		<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> 
		<property name="timeBetweenEvictionRunsMillis" value="60000" /> 
 
		<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> 
		<property name="minEvictableIdleTimeMillis" value="300000" /> 
 
		<property name="validationQuery" value="SELECT 'x'" /> 
		<property name="testWhileIdle" value="true" /> 
		<property name="testOnBorrow" value="false" /> 
		<property name="testOnReturn" value="false" /> 
 
		<!-- 打开PSCache,并且指定每个连接上PSCache的大小 --> 
		<property name="poolPreparedStatements" value="true" /> 
		<property name="maxPoolPreparedStatementPerConnectionSize" 
			value="20" /> 
 
		<!-- 配置监控统计拦截的filters --> 
		<property name="filters" value="stat" /> 
	</bean> 
 
	<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> 
	<bean id="sqlSessionFactory2" class="org.mybatis.spring.SqlSessionFactoryBean"> 
		<property name="dataSource" ref="dataSource2" /> 
		<!-- 自动扫描mapping.xml文件 --> 
		<property name="mapperLocations" value="classpath:com/wlsq/kso/mapper/*.xml"></property> 
	</bean> 
 
	<!-- DAO接口所在包名,Spring会自动查找其下的类 --> 
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 
		<property name="basePackage" value="com.wlsq.kso.dao" /> 
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory2"></property> 
	</bean> 
 
	<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --> 
	<bean id="transactionManager2" 
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
		<property name="dataSource" ref="dataSource2" /> 
		<qualifier value="insurance" />   
	</bean> 
	 
	 
	 
	 
	 
	 
	 
    <!--spring 集成缓存服务器(memcached) --> 
	 <bean id="memcachedPool" class="com.danga.MemCached.SockIOPool" 
        factory-method="getInstance" init-method="initialize" 
        destroy-method="shutDown"> 
 
        <constructor-arg> 
            <value>memCachedPool</value> 
        </constructor-arg> 
         
        <property name="servers"> 
            <list> 
                <value>${memcache.server}</value> 
            </list> 
        </property> 
         
        <property name="initConn"> 
            <value>${memcache.initConn}</value> 
        </property> 
         
        <property name="minConn"> 
            <value>${memcache.minConn}</value> 
        </property> 
 
        <property name="maxConn"> 
            <value>${memcache.maxConn}</value> 
        </property> 
 
        <property name="maintSleep"> 
            <value>${memcache.maintSleep}</value> 
        </property> 
 
        <property name="nagle"> 
            <value>${memcache.nagle}</value> 
        </property> 
 
        <property name="socketTO"> 
            <value>${memcache.socketTO}</value> 
        </property> 
    </bean> 
 
    <bean id="memCachedClient" class="com.danga.MemCached.MemCachedClient"> 
        <constructor-arg> 
            <value>memCachedPool</value> 
        </constructor-arg> 
    </bean> 
     
    <bean id="memcachedService" class="com.wlsq.kso.service.impl.MemcachedServiceImpl"/>  
     
    <!-- 2、安全管理器 --> 
	<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> 
		<property name="realm" ref="userRealm" /> 
	</bean> 
 
	<!--3、realm --> 
	<bean id="userRealm" class="com.wlsq.kso.realm.WlsqLoginRealm" />  
       <!-- Shiro 的Web过滤器 --> 
	<!--1、与web.xml对应的bean --> 
	<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> 
		<property name="securityManager" ref="securityManager" /> 
		<!-- 如果没有认证将要跳转的登陆地址,http可访问的url,如果不在表单认证过虑器FormAuthenticationFilter中指定此地址就为身份认证地址 --> 
		<property name="loginUrl" value="/login/enterDeveloperLogin.html" /> 
		<!-- 没有权限跳转的地址 --> 
		<property name="unauthorizedUrl" value="/" /> 
		 
		<!--过滤定义,从上而下,蒋匿名的anon放最下面 --> 
		<property name="filterChainDefinitions"> 
			<value> 
			    <!--访问页面样式资源文件,不需要任何权限即可访问  --> 
				/static/** = anon 
				/login/** = anon 
				/picCode/** = anon 
				/sendMobileCode/** = anon 
				/account/** =anon 
				/oauthAccessToken!authorize = anon 
				<!--perms[user:search]表示访问此连接需要权限为user:search的用户  --> 
				<!--/developer=anon,roles[admin] perms[add] --> 
				<!--roles[admin]表示访问此连接需要用户的角色为admin --> 
				/developer/** = authc 
				/oauthAccessToken/** = authc 
				/application/**=authc	 
				/productinfo/** = authc 
				/productdetailinfo/** = authc 
				/productcommondinfo/** = authc 
				/application/** = authc 
			</value> 
		</property> 
	</bean>  
       
     
    <!--ssm 集成 mongodb  --> 
    <import resource="classpath:spring/spring-mongodb.xml"/> 
    
   
 
</beans>

二、Service 类指定数据库(默认数据库:isap)

package com.wlsq.kso.basic; 
 
import org.springframework.transaction.annotation.Transactional; 
 
@Transactional(value = "isap", rollbackFor = Exception.class)   
public abstract class BaseMySqlService { 
 
} 


package com.wlsq.kso.service.impl; 
 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Service; 
 
import com.wlsq.kso.basic.BaseMySqlService; 
import com.wlsq.kso.dao.AccountMapper; 
import com.wlsq.kso.entity.Account; 
import com.wlsq.kso.service.IAccountService; 
 
@Service("accountService")  
public class AccountServiceImpl  extends BaseMySqlService implements IAccountService{ 
	@Autowired 
	private AccountMapper accountMapper; 
	@Override 
	public int deleteByPrimaryKey(Integer id) { 
		// TODO Auto-generated method stub 
		return accountMapper.deleteByPrimaryKey(id); 
	} 
 
	@Override 
	public int insert(Account record) { 
		// TODO Auto-generated method stub 
		return accountMapper.insert(record); 
	} 
 
	@Override 
	public int insertSelective(Account record) { 
		// TODO Auto-generated method stub 
		return accountMapper.insertSelective(record); 
	} 
 
	@Override 
	public Account selectByPrimaryKey(Integer id) { 
		// TODO Auto-generated method stub 
		return accountMapper.selectByPrimaryKey(id); 
	} 
 
	@Override 
	public int updateByPrimaryKeySelective(Account record) { 
		// TODO Auto-generated method stub 
		return accountMapper.updateByPrimaryKeySelective(record); 
	} 
 
	@Override 
	public int updateByPrimaryKey(Account record) { 
		// TODO Auto-generated method stub 
		return accountMapper.updateByPrimaryKey(record); 
	} 
 
 
	@Override 
	public Account selectByUserPass(String username, String password) { 
		// TODO Auto-generated method stub 
		return accountMapper.selectByUserPass(username, password); 
	} 
 
	@Override 
	public Account selectByOpenId(String openId) { 
		// TODO Auto-generated method stub 
		return accountMapper.selectByOpenId(openId); 
	} 
 
	@Override 
	public int deleteByUserPassOpenId(String openId) {			 
		// TODO Auto-generated method stub 
		return accountMapper.deleteByUserPassOpenId(openId); 
	} 
 
	@Override 
	public int updateByUserOpenId(Account account) { 
		// TODO Auto-generated method stub 
		return accountMapper.updateByUserOpenId(account); 
	} 
 
 
	@Override 
	public Account checkAccount(String username, String password) { 
		// TODO Auto-generated method stub 
		return accountMapper.checkAccount(username, password); 
	} 
 
	@Override 
	public Account selectByAccount(Account account) { 
		// TODO Auto-generated method stub 
		return accountMapper.selectByAccount(account); 
	} 
 
 
} 


三、Service 指定其他数据实现(XcloudDataServiceImpl 没有实现BaseMysqlService,我们需要通过事务标签transactional 为该service 指定数据库(insurance))

package com.wlsq.kso.service.impl; 
 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Service; 
import org.springframework.transaction.annotation.Transactional; 
 
import com.wlsq.kso.dao.XcloudDataMapper; 
import com.wlsq.kso.entity.XcloudData; 
import com.wlsq.kso.service.XcloudDataService; 
 
@Service("xcloudDataService")   
@Transactional(value = "insurance", rollbackFor = Exception.class)   
public class XcloudDataServiceImpl implements XcloudDataService { 
	@Autowired 
	private XcloudDataMapper xcloudData; 
	@Override 
	public int deleteByPrimaryKey(Integer cloudid) { 
		// TODO Auto-generated method stub 
		return xcloudData.deleteByPrimaryKey(cloudid); 
	} 
 
	@Override 
	public int insert(XcloudData record) { 
		// TODO Auto-generated method stub 
		return xcloudData.insert(record); 
	} 
 
	@Override 
	public int insertSelective(XcloudData record) { 
		// TODO Auto-generated method stub 
		return xcloudData.insertSelective(record); 
	} 
 
	@Override 
	public XcloudData selectByPrimaryKey(Integer cloudid) { 
		// TODO Auto-generated method stub 
		return xcloudData.selectByPrimaryKey(cloudid); 
	} 
 
	@Override 
	public int updateByPrimaryKeySelective(XcloudData record) { 
		// TODO Auto-generated method stub 
		return xcloudData.updateByPrimaryKeySelective(record); 
	} 
 
	@Override 
	public int updateByPrimaryKey(XcloudData record) { 
		// TODO Auto-generated method stub 
		return xcloudData.updateByPrimaryKey(record); 
	} 
 
} 





标签:springMVC
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号