================================
©Copyright 蕃薯耀 2020-01-07
https://www.cnblogs.com/fanshuyao/
import java.util.ArrayList; import java.util.List; import org.apache.commons.lang.StringUtils; public class SqlUtils { /** * * @param params List<Object>,sql执行的?参数List * @param sqlBuffer StringBuffer * @param column String,表字段,当column=user_name,生成如下:user_name in (?,?) * @param values Object[],问号(?)的数组值, */ public static void setInSql(List<Object> params, 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(")"); } /** * * @param params sql执行的?参数List List<Object> * @param sqlBuffer StringBuffer * @param column 表字段,当column=user_name,生成如下:user_name in (?,?) * @param valuesString String,以英文逗号(,)分隔的字符串 */ public static void setInSql(List<Object> params, StringBuffer sqlBuffer, String column, String valuesString) { String[] values = valuesString.split(","); setInSql(params, sqlBuffer, column, values); } /** * 字符串参数转成整型数组 * @param valuesString * @return */ public static Integer[] getIntegerValue(String valuesString){ List<Integer> integerValues = new ArrayList<Integer>(); 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(new Integer[integerValues.size()]); } return null; } /** * * @param sqlBuffer StringBuffer * @param column String,表字段,当column=user_name,生成如下:user_id in (1, 2) * @param values Integer[],数组值 */ public static void getInSql(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(")"); } /** * * @param sqlBuffer StringBuffer * @param column String,表字段,当column=user_name,生成如下:user_name in ('a','b') * @param values String[],数组值 */ public static void getInSql(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 void main(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); } } }
(如果你觉得文章对你有帮助,欢迎捐赠,^_^,谢谢!)
================================
©Copyright 蕃薯耀 2020-01-07