@@ -20,8 +20,10 @@ import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MybatisUtils
{
private static HashMap < String , SqlSession > SessionMap = new HashMap < > ( 5 ) ;
private static HashMap < String , SqlSession > SessionMapBatch = new HashMap < > ( 5 ) ;
private static HashMap < String , SqlSession > SessionMap = new HashMap < > ( 5 ) ;
private static HashMap < String , SqlSession > SessionMapBatch = new HashMap < > ( 5 ) ;
private static HashMap < String , SqlSessionFactory > SessionFactoryMap = new HashMap < > ( 5 ) ;
private static HashMap < String , SqlSessionFactory > SessionFactoryMapBatch = new HashMap < > ( 5 ) ;
private static String DEFAULT_CONFIGFILE = " mybatis/mybatis-config.xml " ;
/**
@@ -33,43 +35,45 @@ public class MybatisUtils
public static SqlSession getSqlSession ( String configFile ) throws IOException
{
// 根据配置文件的路径, 查找是否已经创建了对应的session
SqlSession session = SessionMap . get ( configFile ) ;
SqlSessionFactory factory = SessionFactory Map . get ( configFile ) ;
// 找到就直接返回
if ( session ! = null )
if ( factory ! = null )
{
return session ;
return factory . openSession ( ) ;
}
// 没找到就创建一个
InputStream configIS = Resources . getResourceAsStream ( configFile ) ;
SqlSessionFactory factory = new SqlSessionFactoryBuilder ( ) . build ( configIS ) ;
session = factory . openSession ( ) ;
factory = new SqlSessionFactoryBuilder ( ) . build ( configIS ) ;
SqlSession session = factory . openSession ( ) ;
// 保存
SessionMap . put ( configFile , session ) ;
SessionFactory Map . put ( configFile , factory ) ;
return session ;
}
public static SqlSession getSqlSessionBatch ( String configFile ) throws IOException
{
SqlSession sessionBatch = SessionMapBatch . get ( configFile ) ;
// 根据配置文件的路径, 查找是否已经创建了对应的session
SqlSessionFactory factory = SessionFactoryMap . get ( configFile ) ;
if ( sessionBatch ! = null )
// 找到就直接返回
if ( factory ! = null )
{
return sessionBatch ;
return factory . openSession ( ) ;
}
// 没有缓存的 就创建一个新的
// 没找到 就创建一个
InputStream configIS = Resources . getResourceAsStream ( configFile ) ;
SqlSessionFactory factory = new SqlSessionFactoryBuilder ( ) . build ( configIS ) ;
sessionBatch = factory . openSession ( ExecutorType . BATCH , false ) ;
factory = new SqlSessionFactoryBuilder ( ) . build ( configIS ) ;
SqlSession session = factory . openSession ( ExecutorType . BATCH , false ) ;
//保存
SessionMapBatch . put ( configFile , sessionBatch ) ;
// 保存
SessionFactoryMap . put ( configFile , factory ) ;
return sessionBatch ;
return session ;
}
/**
@@ -80,21 +84,21 @@ public class MybatisUtils
public static SqlSession getSqlSession ( ) throws IOException
{
// 根据配置文件的路径, 查找是否已经创建了对应的session
SqlSession session = SessionMap . get ( DEFAULT_CONFIGFILE ) ;
SqlSessionFactory factory = SessionFactory Map . get ( DEFAULT_CONFIGFILE ) ;
// 找到就直接返回
if ( session ! = null )
if ( factory ! = null )
{
return session ;
return factory . openSession ( ) ;
}
// 没找到就创建一个
InputStream configIS = Resources . getResourceAsStream ( DEFAULT_CONFIGFILE ) ;
SqlSessionFactory factory = new SqlSessionFactoryBuilder ( ) . build ( configIS ) ;
session = factory . openSession ( ) ;
factory = new SqlSessionFactoryBuilder ( ) . build ( configIS ) ;
SqlSession session = factory . openSession ( ) ;
// 保存
SessionMap . put ( DEFAULT_CONFIGFILE , session ) ;
SessionFactory Map . put ( DEFAULT_CONFIGFILE , factory ) ;
return session ;
}
@@ -108,21 +112,23 @@ public class MybatisUtils
*/
public static SqlSession getSqlSessionBatch ( ) throws IOException
{
SqlSession sessionBatch = SessionMapBatch . get ( DEFAULT_CONFIGFILE ) ;
// 根据配置文件的路径, 查找是否已经创建了对应的session
SqlSessionFactory factory = SessionFactoryMap . get ( DEFAULT_CONFIGFILE ) ;
if ( sessionBatch ! = null )
// 找到就直接返回
if ( factory ! = null )
{
return sessionBatch ;
return factory . openSession ( ) ;
}
// 没有缓存的 就创建一个新的
// 没找到 就创建一个
InputStream configIS = Resources . getResourceAsStream ( DEFAULT_CONFIGFILE ) ;
SqlSessionFactory factory = new SqlSessionFactoryBuilder ( ) . build ( configIS ) ;
sessionBatch = factory . openSession ( ExecutorType . BATCH , false ) ;
factory = new SqlSessionFactoryBuilder ( ) . build ( configIS ) ;
SqlSession session = factory . openSession ( ExecutorType . BATCH , false ) ;
//保存
SessionMapBatch . put ( DEFAULT_CONFIGFILE , sessionBatch ) ;
// 保存
SessionFactoryMap . put ( DEFAULT_CONFIGFILE , factory ) ;
return sessionBatch ;
return session ;
}
}