zoukankan      html  css  js  c++  java
  • MyBatisUtil的封装

    版本一:

    public class MyBatisUtil {
        private static SqlSessionFactory factory = null;
        static {
            InputStream is = null;
            try {
                is = Resources.getResourceAsStream("mybatis-config.xml");
                factory = new SqlSessionFactoryBuilder().build(is);
            } catch (IOException e) {
                throw new RuntimeException("mybatis配置文件加载异常",e);
            }finally {
                if(is != null) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    /** * 获得SqlSession * @throws Exception */ public static SqlSession getSqlSession() throws Exception { SqlSession session = factory.openSession(); return session; } }

    版本二:

    public class MyBatisUtil2 {
        private static SqlSessionFactory factory = null;
        private static ThreadLocal<SqlSession> td1 = new ThreadLocal<SqlSession>();
        static {
            InputStream is = null;
            try {
                is = Resources.getResourceAsStream("mybatis-config.xml");
                factory = new SqlSessionFactoryBuilder().build(is);
            } catch (IOException e) {
                throw new RuntimeException("mybatis配置文件加载异常",e);
            }finally {
                if(is != null) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        
        
        /**
         * 获得SqlSession
         * @throws Exception 
         */
        public static SqlSession getSqlSession(){
            //1.获得DAO对象 SqlSession
            SqlSession session = null;
            //2.从当前线程中获得session
            try {
                session = td1.get();
                if(session == null) {
                    //当前线程如果没有sqlsession,就赋值
                    session = factory.openSession();
                    //将session存入当前线程
                    td1.set(session);
                }
            } catch (Exception e) {
                throw new RuntimeException("获得sqlsession的异常",e);
            }
            return session;
        }
        
        /**
         * 根据接口的类对象,获得DAO对象
         */
        public static <T> T getMapper(Class<T> clazz) {
            //获得sqlsession
            SqlSession session = getSqlSession();
            //调用session.getMapper(clazz);
            T t = session.getMapper(clazz);
            return t;
        }
        
        /**
         * 提交事务+释放资源
         */
        public static void commit() {
            //sqlsession.commit();
            try {
                SqlSession session = getSqlSession();
                session.commit();
            } catch (RuntimeException e) {
                throw e;
            }finally {
                close();
            }
        }
        
        /**
         * 回滚事务+释放资源
         */
        public static void rollback() {
            try {
                SqlSession session = getSqlSession();
                session.rollback();
            } catch (RuntimeException e) {
                throw e;
            }finally {
                //释放资源
                close();
            }
        }
        
        /**
         * 释放session资源
         */
        public static void close() {
            SqlSession session = getSqlSession();
            if(session != null) {
                session.close();
                //从当前线程中移除该session
                td1.remove();
            }
        }
    }
  • 相关阅读:
    CentOS5.6下SVN的安装
    在servlet中的init方法中使用getInitParameter方法空指针错误
    Linux iostat监测IO状态【转】
    自己实现一个list比较器 实现Comparator()接口
    一些常用的随机实现
    java里null强转为某个类会报错吗?
    java游戏服务器简单工厂模式
    起个头!准备写一个 设计模式系列
    HashMap根据value值排序
    java游戏服务器 策略+简单工厂
  • 原文地址:https://www.cnblogs.com/lhl0131/p/13410292.html
Copyright © 2011-2022 走看看