一、拼接字符串并用逗号分隔

  1. public class StuTest {
  2. public static void main(String[] args) {
  3. Stu stu1 = new Stu(1, "zhangsan");
  4. Stu stu2 = new Stu(2, "lisi");
  5. List<Stu> list = new ArrayList<>();
  6. list.add(stu1);
  7. list.add(stu2);
  8. String result = list.stream().map(Stu::getName).collect(Collectors.joining(","));
  9. System.out.println(result.toString());
  10. }
  11. }

二、在sql中进行判断逻辑

  1. <sql id="sql-count-where">
  2. <choose>
  3. <when test="param.basicIdList == null">
  4. and 1 = 1
  5. </when>
  6. <when test="param.basicIdList != null and param.basicIdList.size > 0">
  7. and aci.basic_id in
  8. <foreach collection="param.basicIdList" index="index" item="item" open="(" close=")" separator=",">
  9. #{item}
  10. </foreach>
  11. </when>
  12. <otherwise>
  13. and 1 = -1
  14. </otherwise>
  15. </choose>
  16. </sql>

三、使用正则切割字符串

image.png

  1. public static final String ORDER_NO_SPLIT_REGEX = ",|,|;|;| |\t|\n|\r";
  1. private List<String> getorderOrWaybillNoList(String orderNoOrWaybillNo) {
  2. if (StringUtils.isBlank(orderNoOrWaybillNo)) {
  3. return Lists.newArrayList();
  4. }
  5. String[] split = orderNoOrWaybillNo.split(ORDER_NO_SPLIT_REGEX);
  6. List<String> orderNoOrWaybillNos = com.google.common.collect.Lists.newArrayList(split);
  7. List<String> orderNoOrWaybillNoList = orderNoOrWaybillNos.stream().filter(o -> StringUtils.isNotBlank(o)).distinct().collect(Collectors.toList());
  8. if (CollectionUtils.isEmpty(orderNoOrWaybillNoList)) {
  9. logger.info("切出的单号集合为空", JSON.toJSONString(split));
  10. }
  11. return orderNoOrWaybillNoList;
  12. }

四、条件语句中的循环模糊查询

  1. <if test="param.businessType == 2 and param.waybillNoList != null and param.waybillNoList.size>0">
  2. and
  3. (
  4. <foreach collection="param.waybillNoList" separator="or" item="waybillNo" index="index">
  5. aci.business_no like concat('%', #{waybillNo},'%')
  6. </foreach>
  7. )
  8. </if>