zoukankan      html  css  js  c++  java
  • SpringMVC+mybatis配置多数据源

    正常的逻辑高设多个dataSource,并注入相应的dao,dao注入相应的mapping.

    jdbc.properties

    mypt.url=jdbc:mysql://192.168.1.42:3306/epi_ningxia_utf8_test?useUnicode=true&allowMultiQueries=true&characterEncoding=utf8&characterSetResults=utf8&useOldAliasMetadataBehavior=true
    mypt.username=sa
    mypt.password=123456
    
    qzj.url=jdbc:mysql://192.168.1.42:3306/epi_ningxia_qzj?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&useOldAliasMetadataBehavior=true
    qzj.username=sa
    qzj.password=123456
    
    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.initialSize=10
    jdbc.maxActive=30
    jdbc.maxIdle=5
    jdbc.minIdle=1
    jdbc.maxWait=2000

    spring-mybatis.xml

     <bean id="propertyConfigurer"  
                class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
                <property name="location" value="classpath:jdbc.properties" />  
            </bean>  
          
    <!-- ------------免疫平台 start-------------------------- -->
            <bean id="qzjdataSource" class="org.apache.commons.dbcp.BasicDataSource"  
                destroy-method="close">  
                <property name="driverClassName" value="${jdbc.driver}" />  
                <property name="url" value="${mypt.url}" />  
                <property name="username" value="${mypt.username}" />  
                <property name="password" value="${mypt.password}" />  
                <!-- 初始化连接大小 -->  
                <property name="initialSize" value="${jdbc.initialSize}"></property>  
                <!-- 连接池最大数量 -->  
                <property name="maxActive" value="${jdbc.maxActive}"></property>  
                <!-- 连接池最大空闲 -->  
                <property name="maxIdle" value="${jdbc.maxIdle}"></property>  
                <!-- 连接池最小空闲 -->  
                <property name="minIdle" value="${jdbc.minIdle}"></property>  
                <!-- 获取连接最大等待时间 -->  
                <property name="maxWait" value="${jdbc.maxWait}"></property>  
            </bean>  
            <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->  
            <bean id="qzj_sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
                <property name="dataSource" ref="qzjdataSource" />  
                <!-- 自动扫描mapping.xml文件 -->  
                <property name="mapperLocations" value="classpath*:com/shensu/mapping/qzj/*.xml"></property>  
            </bean>  
            <!-- DAO接口所在包名,Spring会自动查找其下的类 -->  
            <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" id="qzj_sqlSessionFactory1">  
                <property name="basePackage" value="com.shensu.dao.qzj" />  
                <property name="sqlSessionFactoryBeanName" value="qzj_sqlSessionFactory"></property>  
            </bean>  
    <!-- --------------免疫平台 end------------------- -->
    <!-- ------前置机start------ -->
               <bean id="myptdataSource" class="org.apache.commons.dbcp.BasicDataSource"  
                destroy-method="close">  
                <property name="driverClassName" value="${jdbc.driver}" />  
                <property name="url" value="${qzj.url}" />  
                <property name="username" value="${qzj.username}" />  
                <property name="password" value="${qzj.password}" />  
                <!-- 初始化连接大小 -->  
                <property name="initialSize" value="${jdbc.initialSize}"></property>  
                <!-- 连接池最大数量 -->  
                <property name="maxActive" value="${jdbc.maxActive}"></property>  
                <!-- 连接池最大空闲 -->  
                <property name="maxIdle" value="${jdbc.maxIdle}"></property>  
                <!-- 连接池最小空闲 -->  
                <property name="minIdle" value="${jdbc.minIdle}"></property>  
                <!-- 获取连接最大等待时间 -->  
                <property name="maxWait" value="${jdbc.maxWait}"></property>        
            </bean>  
            <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->  
            <bean id="mypt_sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
                <property name="dataSource" ref="myptdataSource" />  
                <!-- 自动扫描mapping.xml文件 -->  
                <property name="mapperLocations" value="classpath*:com/shensu/mapping/mypt/*.xml"></property>  
            </bean>  
            <!-- DAO接口所在包名,Spring会自动查找其下的类 -->  
            <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" id="mypt_sqlSessionFactory1">  
                <property name="basePackage" value="com.shensu.dao.mypt" />  
                <property name="sqlSessionFactoryBeanName" value="mypt_sqlSessionFactory"></property>  
            </bean>  
    <!-- ----前置机 end---- -->
          
            <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->  
            <bean id="transactionManager"  
                class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
                <property name="dataSource" ref="myptdataSource" />  
            </bean> 

    我觉得这样已经差不多,网上大部分的资料也是这么配置的,但这边会有一直报一个错,

    严重: StandardWrapper.Throwable
    org.springframework.beans.factory.BeanCreationException: 
    nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.shensu.common.connect.sql.SqlMapSupport.setSqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory);
    nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.apache.ibatis.session.SqlSessionFactory] is defined: expected single matching bean but found 2: [qzj_sqlSessionFactory, mypt_sqlSessionFactory]

    意思是

    SqlSessionFactory应该只有一个,但有两个所以报错。

    后来查看了资料,我们的dao会继承DaoSupport,但DaoSupport只能配一个SqlSessionFactory,所以这边我们要分重写,各自的数据源拥有各自的DaoSupport。

    重写如下:

    package com.shensu.common.connect.sql;
    
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.mybatis.spring.SqlSessionTemplate;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.dao.support.DaoSupport;
    
    public class QZJSqlSessionDaoSupport extends DaoSupport{
    
        private SqlSession sqlSession;
         
        private boolean externalSqlSession;
     
        @Autowired(required = false)
        public final void setSqlSessionFactory(@Qualifier("qzj_sqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
            if (!this.externalSqlSession) {
                this.sqlSession = new SqlSessionTemplate(sqlSessionFactory);
            }
        }
     
        @Autowired(required = false)
        public final void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
            this.sqlSession = sqlSessionTemplate;
            this.externalSqlSession = true;
        }
     
        /**
         * Users should use this method to get a SqlSession to call its statement methods
         * This is SqlSession is managed by spring. Users should not commit/rollback/close it
         * because it will be automatically done.
         *
         * @return Spring managed thread safe SqlSession
         */
        public final SqlSession getSqlSession() {
            return this.sqlSession;
        }
     
        @Override
        protected void checkDaoConfig() throws IllegalArgumentException {
            System.out.println("Property ‘qzj_sqlSessionFactory’ or ‘qzj_sqlSessionFactory’ are required");
            //notNull(this.sqlSession, "Property ‘sqlSessionFactory’ or ‘sqlSessionTemplate’ are required");
        }
    
    }

    然后dao继承这个QZJSqlSessionDaoSupport,其它的数据源类似。

    然后就不会报错了。。。。。

  • 相关阅读:
    js对象
    _proto_和prototype区别
    手写自己的Vuex
    limitPNG压缩图片
    swiper兼容性ie浏览器出现的问题
    postcss-pxtorem
    【Other】Win10防火墙放行Docker(WSL2)端口
    docker容器内使用apt-get报错
    docker+mysql
    docker部署+验证码错误
  • 原文地址:https://www.cnblogs.com/wuxiaojuan/p/11308644.html
Copyright © 2011-2022 走看看