如何使用两个参数编写Mybatis XML映射器(第一个是List<String>,第二个是Long)?



我目前的 mybatis mapper.xml

  <select id="batchSelect" resultMap="ResultMap">
    select id, user_id, mall_id, log, log_type
    from user_log
    where user_id in (
    <foreach collection="userList" index="index" item="item" separator=",">
      #{item,jdbcType=VARCHAR}
    </foreach>
    ) and mall_id = #{1}
  </select>

Java Mapper.java

List<UserLog> batchSelect(List<String> userList, Long mallId);

当我启动 spring-boot 服务时,异常是:

exception: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'userList' not found. Available parameters are [0, 1, param1, param2]

我怎样才能正确地写这个?

您可以使用annonation @Param

List<UserLog> batchSelect(@Param("userList")List<String> userList, @Param("mailId")Long mallId);
<select id="batchSelect" resultMap="ResultMap">
    select id, user_id, mall_id, log, log_type
    from user_log
    where user_id in (
    <foreach collection="userList" index="index" item="item" separator=",">
      #{item,jdbcType=VARCHAR}
    </foreach>
    ) and mall_id = #{mailId}
  </select>

实际上,mybatis-generator supprot selectByExampleupdateByExample,它们都支持where in子句。

这是我的最终选择

相关内容

最新更新