场景:mybatis @Select注解 使用IN,后接参数
注:
若sql中比较符号,如小于等于:
<![CDATA[<=]]>
有效方式:
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
@Mapper
@Component
public interface TestMapper {
/**
* 查询
*/
@Select({
"<script>",
"SELECT * FROM test t where t.tem in",
"<foreach collection='cameraIds' item='item' open='(' separator=',' close=')'>",
"#{item}",
"</foreach>",
"</script>"
})
List<Map<String, Object>> searchData(@Param("cameraIds") List<String> cameraIds);
}
无效方式:
test1:
cameraIds为数据拼接字符串(‘1’,‘2’)或(1,2),但是查询内容数据大小为0。
@Select({"SELECT * FROM test t WHERE t.tem IN (#{cameraIds})"})
解决方式: 将#{}改为${}即可。
test2:
@Select({"SELECT * FROM pdt p WHERE p.sbbh IN <foreach collection = \'cameraIds\' index=\'index\' item=\'item\'" +
" open=\'(\' separator=\',\' close=\')\'> #{item}</foreach>"})
or
@Select({"SELECT * FROM pdt p WHERE p.sbbh IN <foreach collection = \"cameraIds\" index=\"index\" item=\"param1\"" +
" open=\"(\" separator=\",\" close=\")\"> #{param1}</foreach>"})
报错:
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'item' not found. Available parameters are [param1, param2, cameraIds]
转自: https://blog.csdn.net/LGHunter/article/details/93711211
网友评论