美文网首页
SSM配置双数据源

SSM配置双数据源

作者: 你笑时很美丶 | 来源:发表于2019-07-12 13:26 被阅读0次

    config配置

    project.name=cas-hnist
    logback.baseRoot=../logs
    #jdbc
    jdbc.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
    jdbc.url=jdbc:sqlserver://192.168.0.1;databaseName=td
    jdbc.username=sa
    jdbc.password=sa
    jdbc.initialSize=10
    jdbc.maxActive=100
    jdbc.maxIdle=20
    jdbc.minIdle=1
    jdbc.maxWait=60000
    jdbc.timeBetweenEvictionRunsMillis=60000
    jdbc.minEvictableIdleTimeMillis=300000
    
    
    #jdbc
    jdbc.driverClassName2=com.microsoft.sqlserver.jdbc.SQLServerDriver
    jdbc.url2=jdbc:sqlserver://192.168.0.1;databaseName=drm
    jdbc.username2=sa
    jdbc.password2=sa
    

    spring-mybatis.ml配置:

    <?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:tx="http://www.springframework.org/schema/tx"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
        <context:component-scan base-package="com.meta.hnist.service.impl"/>
    
        <!-- 配置数据源 -->
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
            <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="initialSize" value="${jdbc.initialSize}"/>
            <!-- 连接池最大数量 -->
            <property name="maxActive" value="${jdbc.maxActive}"/>
            <!-- 连接池最小空闲 -->
            <property name="minIdle" value="${jdbc.minIdle}"/>
            <!-- 获取连接最大等待时间 -->
            <property name="maxWait" value="${jdbc.maxWait}"/>
            <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
            <property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}"/>
            <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
            <property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}"/>
            <!-- 用来检测连接是否有效的sql,要求是一个查询语句-->
            <property name="validationQuery" value="select 1"/>
            <!-- 申请连接时执行validationQuery检测连接是否有效,配置为true会降低性能 -->
            <property name="testWhileIdle" value="true"/>
            <property name="testOnBorrow" value="false"/>
        </bean>
        <!-- 配置数据源 -->
        <bean id="dataSource_first" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
            <property name="driverClassName" value="${jdbc.driverClassName2}"/>
            <property name="url" value="${jdbc.url2}"/>
            <property name="username" value="${jdbc.username2}"/>
            <property name="password" value="${jdbc.password2}"/>
            <!-- 初始化连接大小 -->
            <property name="initialSize" value="${jdbc.initialSize}"/>
            <!-- 连接池最大数量 -->
            <property name="maxActive" value="${jdbc.maxActive}"/>
            <!-- 连接池最小空闲 -->
            <property name="minIdle" value="${jdbc.minIdle}"/>
            <!-- 获取连接最大等待时间 -->
            <property name="maxWait" value="${jdbc.maxWait}"/>
            <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
            <property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}"/>
            <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
            <property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}"/>
            <!-- 用来检测连接是否有效的sql,要求是一个查询语句-->
            <property name="validationQuery" value="select 1"/>
            <!-- 申请连接时执行validationQuery检测连接是否有效,配置为true会降低性能 -->
            <property name="testWhileIdle" value="true"/>
            <property name="testOnBorrow" value="false"/>
        </bean>
       <!-- Spring整合mybatis -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
            <property name="mapperLocations" value="classpath:mybatis-mappers/*.xml"/>
        </bean>
      <!--Spring整合mybatis2-->
      <bean id="sqlSessionFactory2" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource_first"/>
            <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
            <property name="mapperLocations" value="classpath:student-mybatis-mappers/*.xml"/>
        </bean>
        <!-- MyBatis 动态扫描 -->
        <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.meta.hnist.mapper"/>
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        </bean>
        <!-- MyBatis 动态扫描2 -->
      <bean  id="mapperScannerConfigurer2" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.meta.student.mapper"/>
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory2"/>
        </bean>
        <!-- 配置事务管理器 -->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>
      <!-- 配置事务管理器 -->
        <bean id="transactionManager2" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource_first"/>
        </bean>
        <!-- 拦截器方式配置事物 -->
        <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="add*" propagation="REQUIRED"/>
                <tx:method name="create*" propagation="REQUIRED"/>
                <tx:method name="save*" propagation="REQUIRED"/>
                <tx:method name="update*" propagation="REQUIRED"/>
                <tx:method name="modify*" propagation="REQUIRED"/>
                <tx:method name="edit*" propagation="REQUIRED"/>
                <tx:method name="delete*" propagation="REQUIRED"/>
                <tx:method name="remove*" propagation="REQUIRED"/>
                <tx:method name="evaluate*" propagation="REQUIRED"/>
    
                <tx:method name="get*" propagation="SUPPORTS"/>
                <tx:method name="query*" propagation="SUPPORTS"/>
                <tx:method name="find*" propagation="SUPPORTS"/>
    
                <tx:method name="*" propagation="SUPPORTS"/>
            </tx:attributes>
        </tx:advice>
      <!-- 拦截器方式配置事物 -->
        <tx:advice id="transactionAdvice2" transaction-manager="transactionManager2">
            <tx:attributes>
                <tx:method name="add*" propagation="REQUIRED"/>
                <tx:method name="create*" propagation="REQUIRED"/>
                <tx:method name="save*" propagation="REQUIRED"/>
                <tx:method name="update*" propagation="REQUIRED"/>
                <tx:method name="modify*" propagation="REQUIRED"/>
                <tx:method name="edit*" propagation="REQUIRED"/>
                <tx:method name="delete*" propagation="REQUIRED"/>
                <tx:method name="remove*" propagation="REQUIRED"/>
                <tx:method name="evaluate*" propagation="REQUIRED"/>
    
                <tx:method name="get*" propagation="SUPPORTS"/>
                <tx:method name="query*" propagation="SUPPORTS"/>
                <tx:method name="find*" propagation="SUPPORTS"/>
    
                <tx:method name="*" propagation="SUPPORTS"/>
            </tx:attributes>
        </tx:advice>
        <!-- 配置切面 -->
        <aop:config>
            <aop:pointcut id="transactionPointcut" expression="execution(* com.meta.hnist.service.impl.*Impl.*(..))"/>
            <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice"/>
        </aop:config>
    </beans>
    <!-- 配置切面2 -->
        <aop:config>
            <aop:pointcut id="transactionPointcut2" expression="execution(* com.meta.student.service.impl.*Impl.*(..))"/>
            <aop:advisor pointcut-ref="transactionPointcut2" advice-ref="transactionAdvice2"/>
        </aop:config>
    
    代码分开写

    相关文章

      网友评论

          本文标题:SSM配置双数据源

          本文链接:https://www.haomeiwen.com/subject/pbyekctx.html