mybatis之 SqlSessionFactory单例创建

新建MybatisSqlSessionFactory单例类,并实例化,使其有个SqlSessionFactory单例对象:

package com.mybatis3.util; 
import java.io.*; 
import org.apache.ibatis.io.Resources; 
import org.apache.ibatis.session.*; 
public class MyBatisSqlSessionFactory{ 
   private static  SqlSessionFactory sqlSessionFactory; 
   public static SqlSessionFactory getSqlSessionFactory(){ 
        if(sqlSessionFactory == null){ 
            InputStream inputStream; 
            try{ 
                inputStream = Resources.getResourceAsStream("mybatis-config.xml"); 
                sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 
            }catch (IOException e){ 
                throw newRuntimeException(e.getCause()); 
      	  } 
        } 
        return sqlSessionFactory; 
    } 
   public static SqlSession openSession(){ 
       return getSqlSessionFactory().openSession(); 
    } 
}




你可能感兴趣的:(mybatis,单例)