MyBatis优化技巧

时间:2023-03-08 17:09:45
MyBatis优化技巧

☬配置日志文件

MyBatis优化技巧

封装工具类

MyBatis优化技巧

代码贴一下:

 package com.shxt.utils;

 import java.io.InputStream;

 import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class MyBatisUtils {
private MyBatisUtils(){};
private static SqlSessionFactory sqlSessionFactory=null;
private static final String PATH="config.xml";
private static InputStream inputStream = null;
static{
try{
inputStream=Resources.getResourceAsStream(PATH);
sqlSessionFactory =new SqlSessionFactoryBuilder().build(inputStream);
}catch(Exception e){
System.out.println("加载MyBatis核心文件错误。异常信息为:"+e.getMessage());
}
}
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
public static void closeSqlSession(SqlSession sqlSession){
if(sqlSession!=null)sqlSession.close();
}
}