欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JdbcTemplate进行in查询随机数量参数的SQL简单拼写 博客分类: java;  

程序员文章站 2024-03-15 10:05:23
...
使用JdbcTemplate做随机数量的in查询时,拼sql有一种较简单的写法。
利用google的CharMatcher和Strings工具类。

    public List<TypePriceInfo> listTypePriceByProductIds(List<Long> productIds) {
        String sqlInParam = CharMatcher.is(',').trimFrom(Strings.repeat("?,", productIds.size()));
        String sql = BASE_TYPE_PRICE_INFO_SQL + "where p.product_id in(" + sqlInParam + ")";
        List<TypePriceInfo> priceInfos = jdbcTemplate.query(sql, productIds.toArray(), typePriceInfoRowMapper);
        if (priceInfos == null) {
            return Collections.emptyList();
        } else {
            return priceInfos;
        }
    }



画面多条件选择查询,做了个condition类,生成条件和参数
package com.qunar.hotel.qhotel.sight.dto.input;

import com.google.common.base.CharMatcher;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * User: zhen.ma
 * Date: 14-3-6
 * Time: 下午5:56
 */
public class TicketTypesCondition {
    private Long sightId;
    private String sightName;
    private Integer ticketTypeId;
    private String ticketTypeName;
    private Integer pageNo;
    private Integer pageSize;
    private String errMessage;
    private List<Long> sightIdsSelectedBySightName;
    public static final Integer maxPageSize = 100;

    public Long getSightId() {
        return sightId;
    }

    public void setSightId(Long sightId) {
        this.sightId = sightId;
    }

    public String getSightName() {
        return sightName;
    }

    public void setSightName(String sightName) {
        this.sightName = sightName;
    }

    public Integer getTicketTypeId() {
        return ticketTypeId;
    }

    public void setTicketTypeId(Integer ticketTypeId) {
        this.ticketTypeId = ticketTypeId;
    }

    public String getTicketTypeName() {
        return ticketTypeName;
    }

    public void setTicketTypeName(String ticketTypeName) {
        this.ticketTypeName = ticketTypeName;
    }

    public Integer getOffset() {
        if (pageNo.compareTo(0) > 0) {
            return (pageNo - 1) * pageSize;
        }
        return 0;
    }

    public void setPageNo(Integer pageNo) {
        this.pageNo = pageNo;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public String getErrMessage() {
        return errMessage;
    }

    public void addSightIdsSelectedBySightName(Long sightId) {
        if (sightIdsSelectedBySightName == null) {
            sightIdsSelectedBySightName = Lists.newArrayList();
        }
        sightIdsSelectedBySightName.add(sightId);
    }

    public boolean checkCondition() {

        if (sightId == null && StringUtils.isEmpty(sightName)
                && ticketTypeId == null && StringUtils.isEmpty(ticketTypeName)) {
            errMessage = "未指定查询条件";
            return false;
        }

        if (pageNo == null || pageSize == null
                || pageNo.compareTo(0) < 1 || pageSize.compareTo(0) < 1) {
            errMessage = "页码或记录条数错误";
            return false;
        }
        if (pageSize != null && pageSize.compareTo(maxPageSize) > 0) {
            errMessage = String.format("单次查询限制%s条", maxPageSize);
            return false;
        }
        return true;
    }

    public String packageSql() {

        StringBuilder ticketTypeSql = new StringBuilder(16);
        ticketTypeSql.append(" WHERE origin_id > 0");

        List<Long> sightIds = Lists.newArrayList();
        if (sightId != null) {
            sightIds.add(sightId);
        }
        if (!CollectionUtils.isEmpty(sightIdsSelectedBySightName)) {
            sightIds.addAll(sightIdsSelectedBySightName);
        }
        if (!CollectionUtils.isEmpty(sightIds)) {
            String sightParam = CharMatcher.is(',').trimFrom(Strings.repeat("?,", sightIds.size()));
            ticketTypeSql.append(" AND sight_id IN (");
            ticketTypeSql.append(sightParam);
            ticketTypeSql.append(")");
        }

        if (ticketTypeId != null) {
            ticketTypeSql.append(" AND origin_id=?");
        }
        if (!StringUtils.isEmpty(ticketTypeName)) {
            ticketTypeSql.append(" AND type_name like ?");
        }

        return ticketTypeSql.toString();
    }

    public Object[] packageParams() {

        List<Object> params = Lists.newArrayList();
        if (sightId != null) {
            params.add(sightId);
        }
        if (!CollectionUtils.isEmpty(sightIdsSelectedBySightName)) {
            params.addAll(sightIdsSelectedBySightName);
        }
        if (ticketTypeId != null) {
            params.add(ticketTypeId);
        }
        if (!StringUtils.isEmpty(ticketTypeName)) {
            params.add("%" + ticketTypeName + "%");
        }
        return params.toArray();
    }
}