Search code examples
javaibatismybatis

Keep sqlMapConfig out of the jar/classpath


I have kept my SqlMapConfig.xml inside the src folder as a packge. When I create the jar for my project, the SqlMapCofig along with the mappers and the properties file are copied inside the jar. This is a problem when I have to change the database connection username, passwords and stuff. Is there a way to keep the SqlMapConfig.xml or the connection.properties file outside the jar?

I tried putting it outside but then it gives a file not found error.

Here's my SqlMapConfig file;

<?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>

<properties resource="config/connection.properties" />

<typeAliases>
    <typeAlias alias="Employee" type="com.Employee" />

</typeAliases>

<environments default="development">
    <environment id="development">
        <transactionManager type="JDBC" />
        <dataSource type="POOLED">
            <property name="driver" value="${database.driver.class.name}" />
            <property name="url" value="${database.connection.url}" />
            <property name="username" value="${database.username}" />
            <property name="password" value="${database.password}" />
        </dataSource>
    </environment>
</environments>

<mappers>
    <mapper resource="config/mappers/Employee.xml" />
</mappers>


Solution

  • After some searching I came across a solution by using,

    SqlSession session = sqlMapper.openSession(conn);
    

    instead of,

    SqlSession session = sqlMapper.openSession();
    

    'conn' is a normal SQlConnection you pass to the openSession(). The connectio properties for conn can be stored any where you like.