我使用foreach for SELECT IN子句。
映射器未找到项目。
这里是例外的一部分。
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'id' not found. Available parameters are [ids, param1]] with root cause
org.apache.ibatis.binding.BindingException: Parameter 'id' not found. Available parameters are [ids, param1]
at org.apache.ibatis.binding.MapperMethod$ParamMap.get(MapperMethod.java:212) ~[mybatis-3.5.9.jar:3.5.9]
at org.apache.ibatis.reflection.wrapper.MapWrapper.get(MapWrapper.java:45) ~[mybatis-3.5.9.jar:3.5.9]
at org.apache.ibatis.reflection.MetaObject.getValue(MetaObject.java:122) ~[mybatis-3.5.9.jar:3.5.9]
at org.apache.ibatis.executor.BaseExecutor.createCacheKey(BaseExecutor.java:219) ~[mybatis-3.5.9.jar:3.5.9]
at org.apache.ibatis.executor.CachingExecutor.createCacheKey(CachingExecutor.java:146) ~[mybatis-3.5.9.jar:3.5.9]
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:88) ~[mybatis-3.5.9.jar:3.5.9]
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:151) ~[mybatis-3.5.9.jar:3.5.9]
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:145) ~[mybatis-3.5.9.jar:3.5.9]
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140) ~[mybatis-3.5.9.jar:3.5.9]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]可以找到Param is,但也可以找到不存在的param1。
下面是@Select注释中使用的sql。
select a.* from ACTOR
<if test="ids != null and ids.size > 0">
where a.ID in
<foreach collection="ids" item="id" separator="," close=")" open="(">
#{id}
</foreach>
</if>接口方法定义为
public List<Actor> listByIds(@Param("ids")List<Long> ids);我已经在其他地方和其他sql查询中使用了foreach,这是唯一的问题。
发布于 2022-05-13 15:19:34
如果要使用script标记,则Select注释必须包括foreach标记,如下例所示。
@Select("""
<script>
select a.* from ACTOR
<if test="ids != null and ids.size > 0">
where a.ID in
<foreach collection="ids" item="id" separator="," close=")" open="(">
#{id}
</foreach>
</if>
</script>
""")
public List<Actor> listByIds(@Param("ids")List<Long> ids);否则,参数映射程序将尝试在函数签名中找到id参数,而不是foreach,因为它不知道它是脚本。
https://stackoverflow.com/questions/72227386
复制相似问题