欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

工具类:mybatis中使用Threadlocal开启session及关闭session

程序员文章站 2024-01-26 15:51:22
1、线程容器,给线程绑定一个Object 内容,后只要线程不变,可以随时取出. 1.1 改变线程,无法取出内容. 1 final ThreadLocal threadLocal = new ThreadLocal<>(); 2 threadLocal.set("测试"); 3 new Thread( ......
1、线程容器,给线程绑定一个object 内容,后只要线程不变,可以随时取出.
1.1 改变线程,无法取出内容.
1 final threadlocal threadlocal = new threadlocal<>();
2 threadlocal.set("测试");
3 new thread(){
4     public void run() {
5         string result = threadlocal.get();
6         system.out.println("结果:"+result);
7     };
8 }.start();
2、使用threadlocal简化开发
2.1定义工具类
    将sqlsession创建出来,存放到threadlocal中,需要时再从中取出
 1 package com.test.util;
 2 
 3 import java.io.ioexception;
 4 import java.io.inputstream;
 5 
 6 import org.apache.ibatis.io.resources;
 7 import org.apache.ibatis.session.sqlsession;
 8 import org.apache.ibatis.session.sqlsessionfactory;
 9 import org.apache.ibatis.session.sqlsessionfactorybuilder;
10 
11 public class mybatisutil {
12 
13     //工厂
14     private static sqlsessionfactory factory;
15     //线程容器
16     private static threadlocal<sqlsession> tl = new threadlocal<>();
17     
18     
19     static{
20         try {
21             inputstream is = resources.getresourceasstream("mybatis.xml");
22             factory = new sqlsessionfactorybuilder().build(is);
23         } catch (ioexception e) {
24             e.printstacktrace();
25         }
26     }
27     /**
28      * 获取session
29      * 从线程容器中获取,没有则创建session在存放到线程容器中
30      * @return
31      */
32     public static sqlsession getsession(){
33         sqlsession sqlsession = tl.get();
34         if(sqlsession == null){
35             sqlsession = factory.opensession();
36             tl.set(sqlsession);
37         }
38         return tl.get();
39     }
40     
41     /**
42      * 关闭session
43      * 从容器线程中获取session,有则关闭,且将线程容器的session清空
44      */
45     public static void closesession(){
46         sqlsession sqlsession = tl.get();
47         if(sqlsession != null){
48             sqlsession.close();
49         }
50         tl.set(null);
51     }
52 }
2.2 定义过滤器
    过滤器可以在调用方法前及方法后添加代码
 1 package com.test.filter;
 2 
 3 import java.io.ioexception;
 4 
 5 import java.io.inputstream;
 6 
 7 import javax.servlet.filter;
 8 import javax.servlet.filterchain;
 9 import javax.servlet.filterconfig;
10 import javax.servlet.servletexception;
11 import javax.servlet.servletrequest;
12 import javax.servlet.servletresponse;
13 import javax.servlet.annotation.webfilter;
14 
15 import org.apache.ibatis.io.resources;
16 import org.apache.ibatis.session.sqlsession;
17 import org.apache.ibatis.session.sqlsessionfactory;
18 import org.apache.ibatis.session.sqlsessionfactorybuilder;
19 
20 import com.test.util.mybatisutil;
21 
22 
23 /**
24  * 最开始是由spring框架提出的.整合hibernate框架是使用的是opensessioninview
25  * 
26  * 
27  * @author administrator
28  *
29  */
30 @webfilter("/*")
31 public class opensessioninview implements filter{
32 
33     @override
34     public void init(filterconfig filterconfig) throws servletexception {
35         // todo auto-generated method stub
36     }
37 
38     @override
39     public void dofilter(servletrequest servletrequest, servletresponse servletresponse, filterchain filterchain)
40             throws ioexception, servletexception {
41 //        inputstream is = resources.getresourceasstream("mybatis.xml");
42 //        sqlsessionfactory factory = new sqlsessionfactorybuilder().build(is);
43 //        sqlsession session = factory.opensession();        
44         sqlsession session = mybatisutil.getsession();
45         try {
46             filterchain.dofilter(servletrequest, servletresponse);
47             session.commit();
48         } catch (exception e) {
49             session.rollback();
50             e.printstacktrace();
51         }finally{
52             mybatisutil.closesession();
53         }
54 //        session.commit();
55 //        session.close();
56     }
57 
58     @override
59     public void destroy() {
60         // todo auto-generated method stub    
61     }
62 }