如何用两个参数编写Mybatis XML Mapper(1st是List <string>,2nd是Long)?

时间:2018-03-22 03:31:45

标签: java spring-boot mybatis

我当前的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]

我怎样才能正确写出来?

2 个答案:

答案 0 :(得分:2)

您可以使用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>

答案 1 :(得分:0)

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

这是我的最终选择