MyBatis基础:使用java提供的ThreadLocal类优化代码

Posted caiJava

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MyBatis基础:使用java提供的ThreadLocal类优化代码相关的知识,希望对你有一定的参考价值。

public class MyBaitsView {
    //使用java提供的ThreadLocal类来存储SqlSession对象,方便同一线程获得sqlSession
    public static ThreadLocal<SqlSession> threadLocal=new ThreadLocal();
    public static SqlSessionFactory factory;
    //初使化SqlSessionFactory工厂
    static {
        try {
            InputStream is = Resources.getResourceAsStream("myBatis.xml");
            factory=new SqlSessionFactoryBuilder().build(is);
        }catch(Exception e) {
            e.printStackTrace();
        }
    }
    //获取SqlSession的方法,如果session 为空,则生成一个新的SqlSession
    public static SqlSession getSession() {
        SqlSession session=threadLocal.get();
        if(session==null) {
            threadLocal.set(factory.openSession());
        }
        return threadLocal.get();
    }
    //关闭SqlSession
    public static void closeSession() {
        SqlSession session=threadLocal.get();
        if(session!=null) {
            session.close();
        }
    }
}
@WebFilter("/*")
public class InsertFilter implements Filter{
    @Override
    public void destroy() { }
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filter)
            throws IOException, ServletException {
        //获得当前线程的SqlSession对象
            SqlSession session=MyBaitsView.getSession();
            try {
                //对请求进行处理
                filter.doFilter(request, response);
            }catch(Exception e) {
                //如果出异常,则回滚
                session.rollback();
                e.printStackTrace();
            }finally {
                //提交事务,关闭SqlSession
                session.commit();
                MyBaitsView.closeSession();
            }
    }
    @Override
    public void init(FilterConfig arg0) throws ServletException { }
}
public class InsertServlet extends HttpServlet{
    InsertService insertService=new InsertServiceImpl();
    @Override
    public void service(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException  {
        request.setCharacterEncoding("utf-8");
        Log log=new Log();
        //获得表单数据
        log.setAccountIn(request.getParameter("accountIn"));
        log.setAccountOut(request.getParameter("accountOut"));
        log.setMoney(Integer.parseInt(request.getParameter("money")));
        //调用service层方法插入数据
        int index = insertService.insert(log);
        System.out.println(index);
    }
}
public class InsertServiceImpl implements InsertService{
    @Override
    public int insert(Log log) {
        //获得SqlSession对象 
        SqlSession session=MyBaitsView.getSession();
        LogMappery logMapper = session.getMapper(LogMappery.class);
        int index=logMapper.insert(log);
        return index;
    }
}

 

以上是关于MyBatis基础:使用java提供的ThreadLocal类优化代码的主要内容,如果未能解决你的问题,请参考以下文章

[ 转载 ] Java基础14--创建线程的两个方法

Java线程基础

java mybatisplus

MyBatis源码阅读: MyBatis基础模块-反射模块

Java 基础 - 多线程基础

Java并发基础--Thread类