核心提示:在练习MyBatis的Interceptor时,当执行sqlSession.selectOne总是抛以下异常,看到此异常信息一头蒙,想象不出来哪里会出错,debug调试发现Executor为null,...
在练习MyBatis的Interceptor时,当执行sqlSession.selectOne总是抛以下异常,看到此异常信息一头蒙,想象不出来哪里会出错,debug调试发现Executor为null,原因是在构建SqlSession时,在Configuration类中,
Executor executor1 = (Executor)this.interceptorChain.pluginAll(executor);得到为null,问题出在实现Inteceptor的插件里,plugin方法没有调用。
// 修改之前
@Override
public Object plugin(Object o) {
return null;
}
// 修改之后
@Override
public Object plugin(Object o) {
return Plugin.wrap(o,this);
}
/**
* 测试
*/
public class AAA {
public static void main(String[] args) {
try {
InputStream rs = Resources.getResourceAsStream("config/mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(rs);
SqlSession sqlSession = sqlSessionFactory.openSession();
Map map = new HashMap();
map.put("id","c0fe6693-6ff9-4845-8d0d-23a6dde205e5");
SysLogBean o = sqlSession.selectOne("com.company.exer.mapper.SysLogMapper.getById",map);
System.out.println(o.getOperatorName());
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* MyBatis插件
*/
@Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class})})
public class PagePlugin implements Interceptor {
private String sqlId = "";
public void setSqlId(String sqlId) {
this.sqlId = sqlId;
}
private static class ReflectHelper {
public static Object getValueByField(Class clasz,Object obj,String fieldStr) {
Object o = null;
try {
if (clasz != null && StringUtils.isNotEmpty(fieldStr)) {
Field field = clasz.getDeclaredField(fieldStr);
if(field.isAccessible()){
o = field.get(obj);
}else{
field.setAccessible(true);
o = field.get(obj);
field.setAccessible(false);
}
}
} catch (NoSuchFieldException e) {
if(clasz != Object.class){
o = getValueByField(clasz.getSuperclass(),obj,fieldStr);
}else{
e.printStackTrace();
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return o;
}
}
@Override
public Object intercept(Invocation invocation) throws Throwable {
Object target = invocation.getTarget();
if (target instanceof RoutingStatementHandler) {
PreparedStatementHandler delegate = (PreparedStatementHandler) ReflectHelper.getValueByField(target.getClass(),target, "delegate");
MappedStatement mappedStatement = (MappedStatement) ReflectHelper.getValueByField(delegate.getClass(), delegate, "mappedStatement");
if(mappedStatement.getId().matches(sqlId)){
BoundSql boundSql = delegate.getBoundSql();
String sql = boundSql.getSql();
}
}
return null;
}
@Override
public Object plugin(Object o) {
return null;
}
@Override
public void setProperties(Properties properties) {
this.sqlId = properties.getProperty("sqlId");
}
public static void main(String[] args) {
}
}
异常信息
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.lang.NullPointerException
### Cause: java.lang.NullPointerException
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:23)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:107)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:98)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:62)
at com.company.exer.AAA.main(AAA.java:26)
Caused by: java.lang.NullPointerException
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:104)
... 3 more


