码迷,mamicode.com
首页 > 其他好文 > 详细

MyBatis工厂工具类 MyBatisUtils

时间:2017-12-17 20:53:25      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:new   sources   nfa   session   post   on()   bsp   ali   tis   

 

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 static SqlSessionFactory factory;

        

         static{

                   try{

                            factory =

                                     new SqlSessionFactoryBuilder()

                                               .build(

                                                                 Resources.getResourceAsStream("mybatis-cfg.xml")

                                                                 );

                   }catch(Exception e){

                            e.printStackTrace();

                            // 静态初始化代码块异常错误

                            throw new ExceptionInInitializerError(e);

                   }

         }

        

         /**

          * @Description 工具方法,创建会话对象使用.

          * @return 创建的会话对象.

          */

         public static SqlSession openSession(){

                   return factory.openSession();

         }

 

}

MyBatis工厂工具类 MyBatisUtils

标签:new   sources   nfa   session   post   on()   bsp   ali   tis   

原文地址:http://www.cnblogs.com/7758521gorden/p/8052980.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!