MyBatis优化技巧

☬配置日志文件

 

 

封装工具类

 

 

 代码贴一下:

 1 package com.shxt.utils;
 2 
 3 import java.io.InputStream;
 4 
 5 import org.apache.ibatis.io.Resources;
 6 import org.apache.ibatis.session.SqlSession;
 7 import org.apache.ibatis.session.SqlSessionFactory;
 8 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 9 
10 public class MyBatisUtils {
11     private MyBatisUtils(){};
12     private static SqlSessionFactory sqlSessionFactory=null;
13     private static final String PATH="config.xml";
14     private static InputStream inputStream = null;
15     static{
16         try{
17             inputStream=Resources.getResourceAsStream(PATH);
18             sqlSessionFactory =new SqlSessionFactoryBuilder().build(inputStream);
19         }catch(Exception e){
20             System.out.println("加载MyBatis核心文件错误。异常信息为:"+e.getMessage());
21         }
22     }
23     public static SqlSession getSqlSession(){
24         return sqlSessionFactory.openSession();
25     }
26     public static void closeSqlSession(SqlSession sqlSession){
27         if(sqlSession!=null)sqlSession.close();
28     }
29 }

 

 

 

posted @ 2016-09-21 20:28 菜鸟宝宝 阅读(...) 评论(...) 编辑 收藏