逗号分隔字符串转String集合,再转Long集合,然后用foreach in查询

String str = "123,222,333",
 String[] strs=str .split(",");
 //将字符串数组转换成集合list
List<String> list=Arrays.asList(strs);
List<Long> paramList = new ArrayList<>();
for(String l : list) {
     paramList.add(Long.valueOf(l));
}
System.out.printIn("paramList");

BigDonationListPO po = new BigDonationListPO();
po.setIdss(paramList);

输出结果 [123,222,333]

<select id="selectBigDonationList" parameterType="com.zgshfpw.grd_api.entity.po.BigDonationListPO" resultMap="ExtendsResultMap">
    SELECT
    fb.PRI_KEY, fb.CROWDFUNDING_ID, fb.CREATOR_ID, fb.CREATE_TIME, fb.DONATENUM,fb.create_user,
    fc01.acr036,
    fc01.acr002
    FROM
    FC_BIGDONATION fb
    LEFT JOIN fc01 ON fc01.acr001 = fb.CROWDFUNDING_ID
    where 1=1
      <if test="idss != null and idss.size > 0">//注意集合非空判断方式
        and fb.PRI_KEY in
        <foreach collection="idss" open="(" close=")" separator="," item="item">//注意collection定义 要为在parameterType中定义的属性
          #{item}
        </foreach>
      </if>
    order by fb.CREATE_TIME desc
  </select>

idss为集合 用if test 判断时, 不要使用 “ idss !=’ ’ ”, mybatis会将集合类型idss 与字符串类型进行 != 比较,报类型比较错误。

补充paramterType定义

@Data
public class BigDonationListPO {
    private List idss; //实体类定义idss为集合
}

你可能感兴趣的:(Mybatis)