package com.js.ai.modules.pointwall.interfac; import java.io.IOException; import java.io.InputStream; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; /*工具类MyBatisUtils,用双检锁简单的实现了一个线程安全的工具类。核心代码在最内层的if判断中。 * 由于配置文件在类路径上,所以我们只需要指定文件名即可。这里用到了MyBatis提供的Resources工具类, * 创建一个输入流,然后交给SqlSessionFactoryBuilder来创建一个SqlSessionFactory。*/ public abstract class MyBatisUtils { private static volatile SqlSessionFactory sqlSessionFactory; public static final String MyBatisConfigLocation = "configuration.xml"; public static SqlSessionFactory getSqlSessionFactory() throws IOException{ if(sqlSessionFactory==null){ synchronized (MyBatisUtils.class) { if(sqlSessionFactory==null){ InputStream inputStream=Resources.getResourceAsStream(MyBatisConfigLocation); sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream); inputStream.close(); } } } return sqlSessionFactory; } }
时间: 2024-12-28 00:48:35