其他分享
首页 > 其他分享> > mybatis工具包

mybatis工具包

作者:互联网

package org.cs.util;

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 java.io.IOException;
import java.io.InputStream;

public class MybatisUtil {
    //加载mybatis配置文件
    //创建sqlsessionfactory对象
    //创建sqlsession连接对象
    //创建关闭sqlsession连接对象

    private static SqlSessionFactory sessionFactory;




    static {
        String resource = "mybatis.xml";
        try {
            InputStream is = Resources.getResourceAsStream(resource);
            sessionFactory = new SqlSessionFactoryBuilder().build(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static SqlSession getSession(){
        return sessionFactory.openSession();
    }
    public static void closeSession(SqlSession session){
        if (session != null) {
            session.close();
        }
    }
}

标签:工具包,ibatis,apache,session,static,org,mybatis,import
来源: https://blog.csdn.net/zhb_888888/article/details/123074923