Mybatis用指定的环境Id配置()

如果在mybatis的config文件里面配置了多个JDBC环境该如何使用呢?

例如有如下mybatis的配置文件

<?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>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://192.168.3.12:3306/orangedb"/>
                <property name="username" value="root"/>
                <property name="password" value="abcd2022"/>
            </dataSource>
        </environment>

        <environment id="test">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://192.168.3.12:3306/orangedb"/>
                <property name="username" value="root"/>
                <property name="password" value="abcd2022"/>
            </dataSource>
        </environment>

    </environments>
    <mappers>
        <mapper resource="mapper/Cars.xml"/>
        <mapper resource="mapper/User.xml"/>
    </mappers>
</configuration>

以上xml文件里面配置了两个JDBC的环境,它们的environment id分别是”development”和”test”.

default表示默认的环境。

但如果要使用指定的环境,则SqlSessionFactory应该这样写:

 @Test
    public void testEnvironment() throws Exception{
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder=new SqlSessionFactoryBuilder();
        InputStream in= Resources.getResourceAsStream("mybatis-config.xml");
        //使用默认的Mybatis环境
        SqlSessionFactory sqlSessionFactory=sqlSessionFactoryBuilder.build(in);

        //使用指定的Mybatis环境,用mybatis主配置文件里面的 environment id来指定
        SqlSessionFactory sqlSessionFactory1=sqlSessionFactoryBuilder.build(in,"test");
    }
————————

如果在mybatis的config文件里面配置了多个JDBC环境该如何使用呢?

例如有如下mybatis的配置文件

<?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>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://192.168.3.12:3306/orangedb"/>
                <property name="username" value="root"/>
                <property name="password" value="abcd2022"/>
            </dataSource>
        </environment>

        <environment id="test">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://192.168.3.12:3306/orangedb"/>
                <property name="username" value="root"/>
                <property name="password" value="abcd2022"/>
            </dataSource>
        </environment>

    </environments>
    <mappers>
        <mapper resource="mapper/Cars.xml"/>
        <mapper resource="mapper/User.xml"/>
    </mappers>
</configuration>

以上xml文件里面配置了两个JDBC的环境,它们的environment id分别是”development”和”test”.

default表示默认的环境。

但如果要使用指定的环境,则SqlSessionFactory应该这样写:

 @Test
    public void testEnvironment() throws Exception{
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder=new SqlSessionFactoryBuilder();
        InputStream in= Resources.getResourceAsStream("mybatis-config.xml");
        //使用默认的Mybatis环境
        SqlSessionFactory sqlSessionFactory=sqlSessionFactoryBuilder.build(in);

        //使用指定的Mybatis环境,用mybatis主配置文件里面的 environment id来指定
        SqlSessionFactory sqlSessionFactory1=sqlSessionFactoryBuilder.build(in,"test");
    }