Home » Uncategorized » MYBATIS Connection properties file out side resource bundle

MYBATIS Connection properties file out side resource bundle

Start here

Make mybatis properties file out of .jar,

common mybatis configuration 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”&gt;
<configuration>
<properties resource=’jdbc.properties’/>
<environments default=”development”>
<environment id=”development”>
<transactionManager type=”JDBC”/>
<dataSource type=”POOLED”>
<property name=’driver’ value=’${jdbc.driverClassName}’/>
<property name=’url’ value=’${jdbc.url}’/>
<property name=’username’ value=’${jdbc.username}’/>
<property name=’password’ value=’${jdbc.password}’/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper class=”com.opazz.mts.mappers.KelasMapper”/>
<mapper class=”com.opazz.mts.mappers.UsersMapper”/>
</mappers>
</configuration>

where ${jdbc.driverClassName},${jdbc.url}… get from properties file.

now remove <properties resource=’jdbc.properties’/> from mybatis xml configuration file then add properties file on SqlsessionFactory

sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream, prop);

 public static SqlSessionFactory getSqlSessionFactory() {
if (sqlSessionFactory == null) {
Properties prop = new Properties();
try {
prop.load(new FileInputStream(“config.properties”));
} catch (IOException ex) {
//eat all error
}
InputStream inputStream;

try {
inputStream = Resources.
getResourceAsStream(“com/opazz/mts/xml/mybatis-config.xml”);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream, prop);
} catch (IOException e) {
throw new RuntimeException(e.getCause());
}
}
return sqlSessionFactory;
}

now you can place mybatis properties file where ever you like.


Leave a comment