importjava.util.ArrayList;importjava.util.List;importorg.apache.commons.lang.StringUtils;public classSqlUtils {/***
*@paramparams List,sql执行的?参数List
*@paramsqlBuffer StringBuffer
*@paramcolumn String,表字段,当column=user_name,生成如下:user_name in (?,?)
*@paramvalues Object[],问号(?)的数组值,*/
public static void setInSql(Listparams, StringBuffer sqlBuffer, String column, Object[] values) {
sqlBuffer.append(" ").append("and").append(" ").append(column).append(" ").append("in (");for(Object obj : values) {if(obj != null){
sqlBuffer.append(" ").append("?,");
params.add(obj);
}
}
sqlBuffer.delete(sqlBuffer.length()- 1, sqlBuffer.length());
sqlBuffer.append(" ").append(")");
}/***
*@paramparams sql执行的?参数List List
*@paramsqlBuffer StringBuffer
*@paramcolumn 表字段,当column=user_name,生成如下:user_name in (?,?)
*@paramvaluesString String,以英文逗号(,)分隔的字符串*/
public static void setInSql(Listparams, StringBuffer sqlBuffer, String column, String valuesString) {
String[] values= valuesString.split(",");
setInSql(params, sqlBuffer, column, values);
}/*** 字符串参数转成整型数组
*@paramvaluesString
*@return
*/
public staticInteger[] getIntegerValue(String valuesString){
List integerValues = new ArrayList();if(!StringUtils.isBlank(valuesString)){if(valuesString.indexOf(",") > -1){
String[] values= valuesString.split(",");for(String value : values) {if(!StringUtils.isBlank(value)){
integerValues.add(Integer.valueOf(value.trim()));
}
}
}else{
integerValues.add(Integer.valueOf(valuesString.trim()));
}return integerValues.toArray(newInteger[integerValues.size()]);
}return null;
}/***
*@paramsqlBuffer StringBuffer
*@paramcolumn String,表字段,当column=user_name,生成如下:user_id in (1, 2)
*@paramvalues Integer[],数组值*/
public static voidgetInSql(StringBuffer sqlBuffer, String column, Integer[] values) {if(values == null || values.length < 1){throw new RuntimeException("参数值不能为空");
}
sqlBuffer.append(" ").append("and").append(" ").append(column).append(" ").append("in (");for(Integer value : values) {if(value != null){
sqlBuffer.append(" ").append(value).append(",");
}
}
sqlBuffer.delete(sqlBuffer.length()- 1, sqlBuffer.length());
sqlBuffer.append(" ").append(")");
}/***
*@paramsqlBuffer StringBuffer
*@paramcolumn String,表字段,当column=user_name,生成如下:user_name in ('a','b')
*@paramvalues String[],数组值*/
public static voidgetInSql(StringBuffer sqlBuffer, String column, String[] values) {if(values == null || values.length < 1){throw new RuntimeException("参数值不能为空");
}
sqlBuffer.append(" ").append("and").append(" ").append(column).append(" ").append("in (");for(String value : values) {if(value != null){
sqlBuffer.append(" '").append(value).append("',");
}
}
sqlBuffer.delete(sqlBuffer.length()- 1, sqlBuffer.length());
sqlBuffer.append(" ").append(")");
}public static voidmain(String[] args) {/*String[] values = "a".split(",");
System.out.println(JsonUtils.toJson(values));
String[] values2 = "a,b,c".split(",");
System.out.println(JsonUtils.toJson(values2));*/
/*StringBuffer sqlBuffer = new StringBuffer("");
getInSql(sqlBuffer, "id", new Integer[]{1,2,3});
System.out.println(sqlBuffer.toString());
getInSql(sqlBuffer, "name", new String[]{"a", "b", "cccc"});
System.out.println(sqlBuffer.toString());*/Integer[] values= getIntegerValue("1 ,2,4 ,5,,7");for(Integer integer : values) {
System.out.println(integer);
}
}
}