아래 내용의 java 클래스 파일을 생성한다.
SqlConfig.java |
package com.util;
import java.io.Reader; import java.sql.Connection;
import com.ibatis.common.resources.Resources; import com.ibatis.sqlmap.client.SqlMapClient; import com.ibatis.sqlmap.client.SqlMapClientBuilder;
public class SqlConfig {
private static SqlMapClient sqlMapClient = null; private static SqlConfig sqlConfig = null; private SqlConfig() throws Exception { Reader reader = null; try { if ( sqlMapClient == null) { reader = Resources.getResourceAsReader("SqlMapConfig.xml"); sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader); } } catch (Exception e) { throw e; } finally { if ( reader != null) { try { reader.close(); } catch (Exception e) { e.printStackTrace(); } } } } private SqlConfig(String type) throws Exception { Reader reader = null; try { if ( sqlMapClient == null) { if(type.equals("batch")) { reader = Resources.getResourceAsReader("BatchSqlMapConfig.xml"); }else { reader = Resources.getResourceAsReader("SqlMapConfig.xml"); } sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader); } } catch (Exception e) { throw e; } finally { if ( reader != null) { try { reader.close(); } catch (Exception e) { e.printStackTrace(); } } } } public static SqlConfig getInstance() { if ( sqlConfig == null ) { synchronized(SqlConfig.class) { if ( sqlConfig == null) { try { sqlConfig = new SqlConfig(); } catch (Exception e) { e.printStackTrace(); } } } } return sqlConfig; } public static SqlConfig getInstance(String type) { if ( sqlConfig == null ) { synchronized(SqlConfig.class) { if ( sqlConfig == null) { try { sqlConfig = new SqlConfig(type); } catch (Exception e) { e.printStackTrace(); } } } } return sqlConfig; } public Connection getConnection() throws Exception { // return sqlMapClient.getDataSource().getConnection(); return sqlMapClient.getCurrentConnection(); } public SqlMapClient getSqlMapClient() { return sqlMapClient; } // public static void main(String[] args) throws Exception { // SqlConfig sc = SqlConfig.getInstance(); // Connection con = sc.getConnection(); // String query = "select * from tab"; // Statement stmt = con.createStatement(); // ResultSet rs = stmt.executeQuery(query); // ResultSetMetaData rsmd = rs.getMetaData(); // while ( rs.next() ) { // for ( int i=0 ; i<rsmd.getColumnCount() ; i++) { // System.out.println( rs.getString(i+1) ); // } // } // } }
|
SqlMapConfig.xml 의 내용을 아래와 같이 수정한다.
- 수정 전 -
|
- 수정 후 -
|
<transactionManager type="JDBC"> <dataSource type="SIMPLE"> <property name="JDBC.Driver" value="${driver}"/> <property name="JDBC.ConnectionURL" value="${url}"/> <property name="JDBC.Username" value="${username}"/> <property name="JDBC.Password" value="${password}"/> <property name="JDBC.DefaultAutoCommit" value="false"/> </dataSource> </transactionManager> |
<transactionManager type="JDBC" commitRequired="false"> <dataSource type="JNDI"> <property name="DataSource" value="TestDB"/> </dataSource> </transactionManager>
|