2017-05-14 36 views
0

我有一個mybatis mapper如何在mybatis映射器代理中獲取sqlSession實例?

public interface FooMapper { 
    @Select("select now()") 
    String getTime(); 
} 

調試時,我得到了以下信息

enter image description here

我想SqlSession實例。所以我嘗試使用反射方式來獲取sqlSession。

Field hField = fooMapper.getClass().getDeclaredField("h"); 

    MapperProxy mapperProxy = (MapperProxy) hField.get(fooMapper); 

    Field sqlSessionField = mapperProxy.getClass().getDeclaredField("sqlSession"); 
    SqlSession sqlSession = (SqlSession) sqlSessionField.get(mapperProxy); 

,但實際上我得到了下面的錯誤

java.lang.NoSuch FieldException: h 
    at java.lang.Class.getDeclaredField(Class.java:2070) 

因此,如何通過反射的方式或其他方式(如反射是不可能的),以獲得SqlSession實例在fooMapper?

回答

0

這工作:

((MapperProxy)((Proxy)fooMapper).h).sqlSession 

MapperProxyorg.apache.ibatis.binding

ProxyJava.lang.reflect