Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iBatis - select environment using XML

I have this configuration in ibatis-config.xml

<configuration>
    <properties resource="collector.properties"/>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="${dev.jdbc.driver}" />
                <property name="url" value="${dev.jdbc.url}" />
            </dataSource>
        </environment>
        <environment id="test">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="${test.jdbc.driver}" />
                <property name="url" value="${test.jdbc.url}" />
            </dataSource>
        </environment>
    </environments>
    <mappers>
    </mappers>
</configuration>

As shown it will load datasource from <environment id="development">

QUESTION: Is it possible at run time switch to use <environment id="test"> without modifying XML? For example - I have a test file where I'm using SqlSessionFactory and want to set it programmatically to use test environment?

like image 995
Bostone Avatar asked Oct 26 '25 14:10

Bostone


1 Answers

SqlSessionFactoryBuilder.build() method can select a specific environment in XML.

For example,

private Reader reader;
private SqlSessionFactory sqlSessionFactorys;
private SqlSession session;

reader = Resources.getResourceAsReader("ibatis-config.xml");

sqlSessionFactorys = new SqlSessionFactoryBuilder().build(reader, "test");
testSession = sqlSessionFactorys.openSession(); // test env

sqlSessionFactorys = new SqlSessionFactoryBuilder().build(reader, "development");
devSession = sqlSessionFactorys.openSession(); // dev env
like image 87
Alex Park Avatar answered Oct 29 '25 17:10

Alex Park



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!