1、线程容器,给线程绑定一个Object 内容,后只要线程不变,可以随时取出.
1.1 改变线程,无法取出内容.
 final ThreadLocal threadLocal = new ThreadLocal<>();
threadLocal.set("测试");
new Thread(){
public void run() {
String result = threadLocal.get();
System.out.println("结果:"+result);
};
}.start();
2、使用Threadlocal简化开发
2.1定义工具类
    将SqlSession创建出来,存放到Threadlocal中,需要时再从中取出
 package com.test.util;

 import java.io.IOException;
import java.io.InputStream; 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 factory;
//线程容器
private static ThreadLocal<SqlSession> tl = new ThreadLocal<>(); static{
try {
InputStream is = Resources.getResourceAsStream("mybatis.xml");
factory = new SqlSessionFactoryBuilder().build(is);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取session
* 从线程容器中获取,没有则创建session在存放到线程容器中
* @return
*/
public static SqlSession getSession(){
SqlSession sqlSession = tl.get();
if(sqlSession == null){
sqlSession = factory.openSession();
tl.set(sqlSession);
}
return tl.get();
} /**
* 关闭session
* 从容器线程中获取session,有则关闭,且将线程容器的session清空
*/
public static void closeSession(){
SqlSession sqlSession = tl.get();
if(sqlSession != null){
sqlSession.close();
}
tl.set(null);
}
}
将工厂的产生改成单例模式
 package com.text.util;

 import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger; 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 factory;
//类线程锁
private static final Class CLASS_LOCK = MybatisUtil.class;
//线程容器
private static ThreadLocal<SqlSession> tl = new ThreadLocal<>();
//构造器私有化
private MybatisUtil(){} public static SqlSessionFactory initSqlSessionFactory(){
String resource = "mybatis.xml";
InputStream is = null;
try {
is = Resources.getResourceAsStream(resource);
} catch (IOException e) {
Logger.getLogger(MybatisUtil.class.getName()).log(Level.SEVERE, null, e);
}
synchronized (CLASS_LOCK) {
if(null == factory){
factory = new SqlSessionFactoryBuilder().build(is);
}
}
return factory;
} /**
* 获取session
* 从线程容器中获取,没有则创建session在存放到线程容器中
* @return
*/
public static SqlSession getSession(){
SqlSession sqlSession = tl.get();
if(sqlSession == null){
if(factory == null){
initSqlSessionFactory();
}
sqlSession = factory.openSession();
tl.set(sqlSession);
}
return tl.get();
} /**
* 关闭session
* 从容器线程中获取session,有则关闭,且将线程容器的session清空
*/
public static void closeSession(){
SqlSession sqlSession = tl.get();
if(sqlSession != null){
sqlSession.close();
}
tl.set(null);
}
}
 
2.2 定义过滤器
    过滤器可以在调用方法前及方法后添加代码
 package com.test.filter;

 import java.io.IOException;

 import java.io.InputStream;

 import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter; 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 com.test.util.MyBatisUtil; /**
* 最开始是由Spring框架提出的.整合Hibernate框架是使用的是OpenSessionInView
*
*
* @author Administrator
*
*/
@WebFilter("/*")
public class OpenSessionInView implements Filter{ @Override
public void init(FilterConfig filterconfig) throws ServletException {
// TODO Auto-generated method stub
} @Override
public void doFilter(ServletRequest servletrequest, ServletResponse servletresponse, FilterChain filterchain)
throws IOException, ServletException {
// InputStream is = Resources.getResourceAsStream("mybatis.xml");
// SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
// SqlSession session = factory.openSession();
SqlSession session = MyBatisUtil.getSession();
try {
filterchain.doFilter(servletrequest, servletresponse);
session.commit();
} catch (Exception e) {
session.rollback();
e.printStackTrace();
}finally{
MyBatisUtil.closeSession();
}
// session.commit();
// session.close();
} @Override
public void destroy() {
// TODO Auto-generated method stub
}
}
 
05-11 15:39