zoukankan      html  css  js  c++  java
  • MyBatisUtil

    package com.baizhi.util;
    
    import java.io.IOException;
    import java.io.Reader;
    
    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 MyBatisUtil {
        private static SqlSessionFactory ssf;
        static {
            try {
                //读取主配置文件
                Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
                //创建sqlsessionfactory(sqlsession工厂)
                ssf = new SqlSessionFactoryBuilder().build(reader);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        public static SqlSession getSqlSession() {
            //通过ssf获取sqlsession
            SqlSession sqlSession = null;
            sqlSession = ssf.openSession();
            return sqlSession;
        }
        //先判断sqlsession不是空再关
        public static void close(SqlSession sqlSession) {
            if(sqlSession!=null) {
                sqlSession.close();
            }
        }
    }
    以粮为纲全面发展
  • 相关阅读:
    shell文件包含
    shell输入/输出重定向
    shell流程控制
    shell echo命令(六)
    shell基本运算符(五)
    shell数组(四)
    shell传递参数-$的用法(三)
    SQL 注入 处理
    WPF 还未开始我就打算结束
    Java SDK 2.0
  • 原文地址:https://www.cnblogs.com/alexliuf/p/13717233.html
Copyright © 2011-2022 走看看