1. 程式人生 > >JdbcTemplate進行in查詢隨機數量引數的SQL簡單拼寫

JdbcTemplate進行in查詢隨機數量引數的SQL簡單拼寫

使用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();
}
}