一.ThreadLocal基础知识(示例中结果:null-----因为在主线程中新建了一个线程,不在同一个线程中取不到threadLocal对象的值)
二.将ThreadLocal知识应用到mybatis中,可以避免重复生成SqlSessionFactory对象,造成代码冗余
1.配置映射配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<typeAliases>
<package name="com.bjsxt.pojo"/>
</typeAliases>
<environments default="default">
<environment id="default">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/ssm"/>
<property name="username" value="root"/>
<property name="password" value="smallming"/>
</dataSource>
</environment>
</environments>
<mappers>
<package name="com.bjsxt.mapper"/>
</mappers>
</configuration>
2.添加MyBatisUtil工具类-----调用ThreadLocal线程容器
package com.bjsxt.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 {
//factory实例化的过程是一个比较耗费性能的过程.
//保证有且只有一个factory
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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 获取SqlSession的方法
*/
public static SqlSession getSession(){
SqlSession session = tl.get();
if(session==null){
tl.set(factory.openSession());
}
return tl.get();
}
public static void closeSession(){
SqlSession session = tl.get();
if(session!=null){
session.close();
}
tl.set(null);
}
}
3.新建过滤器,在过滤器中调用MyBatisUtil.getSession()的方法获得同一个线程中的SqlSession对象
package com.bjsxt.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.bjsxt.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
}
}
4.最后在service业务层调用调用MyBatisUtil.getSession()的方法获得同一个线程中的SqlSession对象
package com.bjsxt.service.impl;
import org.apache.ibatis.session.SqlSession;
import com.bjsxt.mapper.LogMapper;
import com.bjsxt.pojo.Log;
import com.bjsxt.service.LogService;
import com.bjsxt.util.MyBatisUtil;
public class LogServiceImpl implements LogService{
@Override
public int ins(Log log) {
SqlSession session = MyBatisUtil.getSession();
LogMapper mapper = session.getMapper(LogMapper.class);
return mapper.ins(log);
}
}