首页 » 技术分享 » Mybatis获取连接工厂工具类

Mybatis获取连接工厂工具类

 

package com.guigu.util;

import java.io.IOException;
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;
import org.apache.log4j.Logger;

public class MyBatisUtil {
    private static  final Logger LOG = Logger.getLogger(MyBatisUtil.class);
    private static  SqlSessionFactory factory;

    static{
        SqlSessionFactoryBuilder  builder = new SqlSessionFactoryBuilder();
        try {
            InputStream  inputStream = Resources.getResourceAsStream("mybatis.xml");
            factory  = builder.build(inputStream);
        } catch (IOException e) {
            LOG.info("com.guigu.util.MyBatisUtil static{},msg 加载配置文件出错了"+e.getMessage());
        }
    }

    public  static   SqlSession  getSqlSession(){
        if(factory == null){
            throw new RuntimeException("factory is  null");
        }
        return factory.openSession();
    }

}

转载自原文链接, 如需删除请联系管理员。

原文链接:Mybatis获取连接工厂工具类,转载请注明来源!

0