【Java】自定义mybatis

处理 sql in 多条件搜索时 单引号双引号问题

StringBuilder pidNoZeroIds = new StringBuilder();
Integer numTmp = 0;
for (ShequLsDatingType pidNoZero : pidNoZeroList) {
    pidNoZeroIds.append("'" + pidNoZero.getId() + "'"); //拼接单引号,到数据库后台用in查询.
    if ( numTmp < pidNoZeroList.size() - 1) { //前面的元素后面全拼上",",最后一个元素后不拼
        pidNoZeroIds.append(",");
    }
    numTmp++;
}

Date date = new Date();
String simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd").format(date);

Map<Integer, Integer> usedNumByDatingTypeId = sysService.getUsedNumByDatingTypeId(pidNoZeroIds, simpleDateFormat);
<select id="getUsedNumByDatingTypeId" resultType="map">
  SELECT dating_type_id AS datingTypeId,SUM(used_num) AS usedNum FROM shequ_ls_dating_times where dating_type_id IN (#{paramMap.dating_type_ids_str}) AND date = #{paramMap.now_date} GROUP BY dating_type_id;
</select>
posted @ 2023-02-01 15:32  小魁jking  阅读(31)  评论(0编辑  收藏  举报