• spring mybatis 多数据源配置


    1、创建好数据库的配置文件

    mysql.url=jdbc:mysql://***/***?useUnicode=true&characterEncoding=UTF-8
    mysql.username=***
    mysql.password=***
    mysql.driverClassName=com.mysql.jdbc.Driver
    
    dws.url=jdbc:postgresql://***/***
    dws.username=***
    dws.password=***
    dws.driverClassName=org.postgresql.Driver

    2、applicationContext.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:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/mvc
           http://www.springframework.org/schema/mvc/spring-mvc.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx.xsd">
    
    
        <!--开启自动扫描-->
        <context:component-scan base-package="com.huawei.test"/>
    
        <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                    <value>classpath:config/jdbc.properties</value>
                </list>
            </property>
        </bean>
        <!--现在可以替换为如下方式-->
        <!--使用逗号分隔多个值-->
        <!--<context:property-placeholder location="classpath:config/jdbc.properties"/>-->
    
        <!--配置数据源-->
        <bean id="dataSource_mysql" class="org.apache.commons.dbcp2.BasicDataSource">
            <property name="url" value="${mysql.url}"/>
            <property name="username" value="${mysql.username}"/>
            <property name="password" value="${mysql.password}"/>
            <property name="driverClassName" value="${mysql.driverClassName}"/>
        </bean>
        <bean id="sqlSessionFactory_mysql" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource_mysql"/>
            <property name="mapperLocations" value="classpath:mybatis/mysql/*.xml"/>
        </bean>
        <bean id="mapperScannerConfigurer_mysql" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.huawei.test.dao.mysql"/>
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory_mysql"/>
        </bean>
        <!--数据库事务控制-->
        <bean id="transactionManager_mysql" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource_mysql"/>
        </bean>
    
        <bean id="dataSource_dws" class="org.apache.commons.dbcp2.BasicDataSource">
            <property name="url" value="${dws.url}"/>
            <property name="username" value="${dws.username}"/>
            <property name="password" value="${dws.password}"/>
            <property name="driverClassName" value="${dws.driverClassName}" />
        </bean>
        <bean id="sqlSessionFactory_dws" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource_dws"/>
            <property name="mapperLocations" value="classpath:mybatis/dws/*.xml"/>
        </bean>
        <bean id="mapperScannerConfigurer_dws" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.huawei.test.dao.dws"/>
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory_dws"/>
        </bean>
        <bean id="transactionManager_dws" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource_dws"/>
        </bean>
    
        <!--使用annotation定义事务,比如使用注解-->
        <tx:annotation-driven/>
        <mvc:annotation-driven/>
    </beans>

    3、创建好对应的mappers文件

  • 相关阅读:
    主流的浏览器分别是什么内核?
    css的基本语句构成是什么?
    如何居中一个浮动元素?
    你如何管理CSS文件、JS与图片?
    、在ie6,ie7中元素高度超出自己设置高度。原因是IE8以前的浏览器中会给元素设置默认的行高的高度导致的。
    前端页面有哪三层构成,分别是什么?作用是什么?
    两个对象的 hashCode() 相同,则 equals() 也一定为 true,对吗?
    == 和 equals 的区别是什么?
    JDK 和 JRE 有什么区别?
    2020.10.19
  • 原文地址:https://www.cnblogs.com/langren1992/p/10151908.html
Copyright © 2020-2023  润新知