Created
June 29, 2016 06:36
-
-
Save lifedever/5d3181502d7cd4f085424f777ef848ee to your computer and use it in GitHub Desktop.
JDBC查询参数链式结构拼接工具类
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ArrayParams { | |
private static Logger logger = Logger.getLogger(MapParams.class); | |
private List<Object> params; | |
private String pattern = "yyyy-MM-dd"; | |
private ArrayParams() { | |
} | |
public static ArrayParams begin() { | |
ArrayParams arrayParams = new ArrayParams(); | |
arrayParams.setParams(new ArrayList<Object>()); | |
return arrayParams; | |
} | |
/** | |
* 指定日期格式 | |
* | |
*/ | |
public static ArrayParams begin(String pattern) { | |
ArrayParams arrayParams = begin(); | |
arrayParams.setPattern(pattern); | |
return arrayParams; | |
} | |
public ArrayParams add(Object value) { | |
// if (value == null) { | |
// logger.debug("param value cannot be null!"); | |
// throw new RuntimeException("param value cannot be null!"); | |
// } | |
Object strVal = null; | |
if (value instanceof Date) { // 处理日期类型 | |
SimpleDateFormat jdf = new SimpleDateFormat(getPattern()); | |
strVal = jdf.format((Date) value); | |
} else { | |
strVal = value; | |
} | |
params.add(strVal); | |
return this; | |
} | |
/** | |
* 返回查询参数集 | |
* @return | |
*/ | |
public Object[] end() { | |
return this.params.toArray(); | |
} | |
public List<Object> getParams() { | |
return params; | |
} | |
public String getPattern() { | |
return pattern; | |
} | |
public void setParams(List<Object> params) { | |
this.params = params; | |
} | |
public void setPattern(String pattern) { | |
this.pattern = pattern; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment