具体实现可以参考mybatis-spring.jar中的SqlSessionTemplate
SqlSessionTemplate 继承sqlSession接口
并通过动态代理,在SqlSessionInterceptor类代理方法中实现session的自动close
注意 继承的SqlSession的getMapper方法不能使用
public Object getMapper(Class type)
{
return sqlSessionFactory.getMapper(type);
}
而要用:
public Object getMapper(Class type)
{
return getConfiguration().getMapper(type, this);
}
另外:
java动态代理链接
本文介绍如何利用MyBatis-Spring中的SqlSessionTemplate实现SqlSession的自动关闭,通过动态代理机制确保资源得到妥善管理。文章还提供了一个正确的getMapper方法实现示例。
388

被折叠的 条评论
为什么被折叠?



