使用的mapper接口以及对应的mapper.xml
public interface BooksSlaveMapper {
List<Book> getBookList(Book object);
List<Book> getBookListByIdList(List<Integer> list, Integer status);
}
<select id="getBookListByIdList" resultMap="BaseResultMap">
select
<include refid="Column_List" />
from books
where id in
<foreach collection="list" item="id" open="(" separator="," close=")">
#{id}
</foreach>
and `status` = #{status,jdbcType=INTEGER}
</select>
运行执行后报错:
Caused by: org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.apache.ibatis.mapping.SqlMapperException: The expression 'list' evaluated to a null value.
### The error may exist in com/lwby/mapper/slave/BooksSlaveMapper.xml
### The error may involve com.lwby.dao.slave.BooksSlaveMapper.getBookListByIdList
### The error occurred while executing a query
### Cause: org.apache.ibatis.mapping.SqlMapperException: The expression 'list' evaluated to a null value.
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:8)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:61)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:53)
at org.apache.ibatis.binding.MapperMethod.executeForList(MapperMethod.java:82)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:63)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:35)
at com.sun.proxy.$Proxy157.getAdPosListByIdListAndPlatformId(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.mybatis.spring.SqlSessionTemplate$7$1.doInSqlSession(SqlSessionTemplate.java:342)
... 49 more
Caused by: org.apache.ibatis.mapping.SqlMapperException: The expression 'list' evaluated to a null value.
at org.apache.ibatis.builder.xml.dynamic.ExpressionEvaluator.evaluateIterable(ExpressionEvaluator.java:27)
at org.apache.ibatis.builder.xml.dynamic.ForEachSqlNode.apply(ForEachSqlNode.java:36)
at org.apache.ibatis.builder.xml.dynamic.MixedSqlNode.apply(MixedSqlNode.java:14)
at org.apache.ibatis.builder.xml.dynamic.DynamicSqlSource.getBoundSql(DynamicSqlSource.java:22)
at org.apache.ibatis.mapping.MappedStatement.getBoundSql(MappedStatement.java:198)
at org.apache.ibatis.executor.BaseExecutor.createCacheKey(BaseExecutor.java:119)
at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:88)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:72)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:59)
... 59 more
这个问题的原因是在于传递的参数,在mapper接口中传递的参数是有两个,在这种情况下list是找不到对应传的值的,所以才会报上面的问题
处理方法:
1.将这些参数处理成一个对象进行传参;
2.在mapper对应接口中添加@Param注解,如下:
public interface BooksSlaveMapper {
List<Book> getBookList(Book object);
List<Book> getBookListByIdList(@Param(value = "list") List<Integer> list, @Param(value = "status") Integer status);
}