728x90
728x90
SMALL
package com.test.common;
import java.io.InputStream;
import java.sql.Connection;
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.apache.ibatis.type.JdbcType;
public class MySqlSessionFactory {
// 서버정보
private static String driver = "driver";
private static String url = "url";
private static String username = "username";
private static String password = "password!";
private static String id = "development";
private static String[] mapperResources = { "com/test/mapper/TestMapper.xml"};
private static SqlSessionFactory sqlSessionFactory = null;
static {
try {
// DataSource 생성
PooledDataSource dataSource = new PooledDataSource();
dataSource.setDriver(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
// Transaction Isolation Level 설정
dataSource.setDefaultTransactionIsolationLevel(Connection.TRANSACTION_READ_COMMITTED);
// Auto Commit 설정
dataSource.setDefaultAutoCommit(false);
// TransactionFactory 생성
JdbcTransactionFactory transactionFactory = new JdbcTransactionFactory();
// Environment 생성
Environment environment = new Environment(id, transactionFactory, dataSource);
// Config 생성
Configuration config = new Configuration(environment);
// parameter 가 null 일 경우 처리
config.setJdbcTypeForNull(JdbcType.VARCHAR);
// null 을 "" 로 return
config.setCallSettersOnNulls(true);
// Mapper 생성
for(String mapper : mapperResources) {
InputStream inputStream = Resources.getResourceAsStream(mapper);
XMLMapperBuilder mapperBuilder = new XMLMapperBuilder(inputStream, config, mapper, config.getSqlFragments());
mapperBuilder.parse();
}
// SqlSessionFactory 생성
sqlSessionFactory = new SqlSessionFactoryBuilder().build(config);
} catch(Exception e) {
e.printStackTrace();
}
}
public static SqlSessionFactory getSqlSessionFactory() {
return sqlSessionFactory;
}
}
728x90
728x90
LIST
'MyBatis' 카테고리의 다른 글
MyBatis 동적 SQL 사용법 (0) | 2021.07.23 |
---|