Mybatis的封装
a) SqlSession对象要使用同一个factory对象创建
b) 一次请求内共享同一个SqlSession对象
c) 需要注意:用到ThreadLocal对象来存储SqlSession对象
i. ThreadLocal对象中存储的数据只能在同一个线程中获取
ii. 在某个线程中存储的,必须在该线程中获取,就算另一个线程中获取到了同一个ThreadLocal对象,也无法获取到其中存储的其他线程的数据
d) private static ThreadLocal threadLocal = new ThreadLocal<>();
public class BaseServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置编码格式
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
//获取请求中的方法
String methodName = req.getParameter("methodName");
Class clazz = this.getClass();
//判断
try {
Method method = clazz.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
method.invoke(this,req,resp);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
}