SqlDataAccessUtil2 package org.onepiece;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import ja
package org.onepiece; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.InputStream; import java.io.Reader; public class SqlDataAccessUtil2 extends SqlDataAccessUtil { //封装SqlSessionFactory********************************************************************************************* private final static Classlock2 = SqlDataAccessUtil2.class; private static SqlSessionFactory sqlSessionFactory2 = null; private final static String xmlResource2 = "mybatis-config.xml"; private static SqlSessionFactory getSqlSessionFactoryImpl2() throws IOException { //加锁,防止在多线程中多次实例化sqlSessionFactory对象 //为了保证sqlSessionFactory的唯一性 synchronized (lock2) { if (sqlSessionFactory2 != null) { return sqlSessionFactory2; } else { /* InputStream inputStream = null; try { inputStream = Resources.getResourceAsStream(xmlResource2); sqlSessionFactory2 = new SqlSessionFactoryBuilder().build(inputStream); } finally { if (inputStream != null) { inputStream.close(); } } */ Reader reader = null; try{ reader = org.apache.ibatis.io.Resources.getResourceAsReader(xmlResource2); sqlSessionFactory2 = new SqlSessionFactoryBuilder().build(reader); } finally { if(reader != null){ reader.close(); } } return sqlSessionFactory2; } } } //****************************************************************************************************************** @Override protected SqlSessionFactory getSqlSessionFactory() throws IOException { //return super.getSqlSessionFactory(); if (sqlSessionFactory2==null){ sqlSessionFactory2=getSqlSessionFactoryImpl2(); } return sqlSessionFactory2; } @Override public SqlSession getSqlSession() throws IOException { return getSqlSession();//super.getSqlSession();//未测试 } }