../common/src/main/java/com/xxxxxxx/drvpp/fw/common/mybatis/MyBatisSessionFactory.java
| 1 | |
package com.xxxxxxx.drvpp.fw.common.mybatis; |
| 2 | |
| 3 | |
import java.io.IOException; |
| 4 | |
import java.io.Reader; |
| 5 | |
| 6 | |
import org.apache.ibatis.io.Resources; |
| 7 | |
import org.apache.ibatis.session.SqlSession; |
| 8 | |
import org.apache.ibatis.session.SqlSessionFactory; |
| 9 | |
import org.apache.ibatis.session.SqlSessionFactoryBuilder; |
| 10 | |
| 11 | |
import com.xxxxxxx.drvpp.fw.common.exception.FwRuntimeException; |
| 12 | |
| 13 | |
public class MyBatisSessionFactory { |
| 14 | 0 |
private static String resource = "mybatis.xml"; |
| 15 | |
private SqlSessionFactory sqlMapper; |
| 16 | |
| 17 | 0 |
private static class MyBatisSessionFactoryHolder { |
| 18 | 0 |
private static final MyBatisSessionFactory instance = new MyBatisSessionFactory(); |
| 19 | |
} |
| 20 | |
| 21 | |
public static MyBatisSessionFactory getInstance() { |
| 22 | 0 |
MyBatisSessionFactoryHolder.instance.init(); |
| 23 | 0 |
return MyBatisSessionFactoryHolder.instance; |
| 24 | |
} |
| 25 | |
| 26 | |
public static void setResourceName(String name){ |
| 27 | 0 |
resource = name; |
| 28 | 0 |
} |
| 29 | |
| 30 | 0 |
private MyBatisSessionFactory() { |
| 31 | |
| 32 | 0 |
} |
| 33 | |
| 34 | |
private void init() { |
| 35 | |
Reader reader; |
| 36 | |
try { |
| 37 | 0 |
reader = Resources.getResourceAsReader(resource); |
| 38 | 0 |
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); |
| 39 | 0 |
sqlMapper = builder.build(reader); |
| 40 | 0 |
} catch (IOException e) { |
| 41 | 0 |
throw new FwRuntimeException(e); |
| 42 | |
} |
| 43 | 0 |
} |
| 44 | |
| 45 | |
public SqlSession openSession() { |
| 46 | 0 |
return sqlMapper.openSession(); |
| 47 | |
} |
| 48 | |
} |