美文网首页
mybaits 学习3-多个数据源

mybaits 学习3-多个数据源

作者: Kean_L_C | 来源:发表于2019-05-04 22:14 被阅读0次

    数据源添加mysql和oracle
    代码见具体github:https://github.com/JNUpython/springboot2/tree/develop

    ojdbc依赖无法下载

    github上面下载该jar 并通过maven命令本地安装

    mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc7 -Dversion=12.1.0.2 -Dpackaging=jar -Dfile=./libs/ojdbc7-12.1.0.2.jar 
    

    mybatis 全局配置xml文件,以及数据库配置文件

    image.png

    mybatis-mysql.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
        <!-- 添加公共变量信息:方法1   -->
        <properties resource="application-dev.properties"></properties>
        <!--   自定义一些变量-->
        <!--    <properties>-->
        <!--        <property name="url" value="jdbc:mysql://127.0.0.1:3306/test?serverTimezone=CTT&amp;useUnicode=true&amp;characterEncoding=utf-8&amp;allowMultiQueries=true"/>-->
        <!--        <property name="driver" value="com.mysql.jdbc.Driver"/>-->
        <!--        <property name="username" value="root"/>-->
        <!--        <property name="password" value="123456"/>-->
        <!--    </properties>-->
    
        <settings>
            <!--   设置数库每个设置项     -->
            <!--   全局地开启或关闭配置文件中的所有映射器已经配置的任何缓存。     -->
            <setting name="cacheEnabled" value="true"/>
        </settings>
    
        <!--  default 选定不同环境  -->
        <environments default="oracle">
    
            <environment id="mysql">
                <transactionManager type="JDBC"/>
                <dataSource type="POOLED">
                    <property name="driver" value="${database.driverName2}"/>
                    <property name="url" value="${database.url1}"/>
                    <property name="username" value="${database.username1}"/>
                    <property name="password" value="${database.password1}"/>
                </dataSource>
            </environment>
    
            <environment id="oracle">
                <transactionManager type="JDBC"/>
                <dataSource type="POOLED">
                    <property name="driver" value="${database.driverName2}"/>
                    <property name="url" value="${database.url2}"/>
                    <property name="username" value="${database.username2}"/>
                    <property name="password" value="${database.password2}"/>
                </dataSource>
            </environment>
        </environments>
        <mappers>
            <mapper resource="mybatis/EmployeeMapper.xml"/>
            <mapper resource="mybatis/EmployeeMapperInterface"/>
        </mappers>
    </configuration>
    

    application-dev.properties

    # 开发环境配置文件
    
    # 程序中会用到的一些自定义变量
    com.didispace.blog.name=学习spring
    com.didispace.blog.title=进阶
    com.dispspace.blog.randomString=${random.value}
    com.dispspace.blog.root=${com.didispace.blog.title}-${com.didispace.blog.title}
    
    
    # mysql test 数据库
    database.url1=jdbc:mysql://127.0.0.1:3306/test?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
    database.username1=root
    database.password1=123456
    database.driverName1=com.mysql.cj.jdbc.Driver
    
    # oracle 数据库
    database.url2=jdbc:oracle:thin:@127.0.0.1:1521:orcl
    database.username2=c##kean
    database.password2=123456
    database.driverName2=oracle.jdbc.driver.OracleDriver
    

    执行结果

    22:11:16.601 [main] DEBUG org.apache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
    22:11:16.811 [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
    22:11:16.812 [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
    22:11:16.812 [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
    22:11:16.812 [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
    22:11:16.916 [main] INFO com.mybatis.MybatisTest - Mapper class: class com.sun.proxy.$Proxy6
    22:11:16.928 [main] DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Opening JDBC Connection
    22:11:19.052 [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - Created connection 927327686.
    22:11:19.052 [main] DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@3745e5c6]
    22:11:19.054 [main] DEBUG com.mybatis.dao.EmployeeMapper.getEmpById - ==>  Preparing: select * from tbl_employee where id = ? 
    22:11:19.184 [main] DEBUG com.mybatis.dao.EmployeeMapper.getEmpById - ==> Parameters: 101(Integer)
    22:11:19.277 [main] DEBUG com.mybatis.dao.EmployeeMapper.getEmpById - <==      Total: 1
    22:11:19.278 [main] INFO com.mybatis.MybatisTest - 101 jack man
    22:11:19.278 [main] DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [oracle.jdbc.driver.T4CConnection@3745e5c6]
    22:11:19.279 [main] DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Closing JDBC Connection [oracle.jdbc.driver.T4CConnection@3745e5c6]
    22:11:19.279 [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - Returned connection 927327686 to pool.
    22:11:19.279 [main] INFO com.mybatis.MybatisTest - 查询完成!
    

    相关文章

      网友评论

          本文标题:mybaits 学习3-多个数据源

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