形如("'1','2','3'"),SQL in('1','2','3')拼接
阿新 • • 發佈:2021-11-25
形如("'1','2','3'"),SQL in('1','2','3')拼接
service.java
String str1 = "1,2,3"; String str2 = "1,2,3,1,2,3,3,2,1"; String str3 = "1"; public static String getInStr(String str){ String inStr = ""; if(StringUtils.isNotBlank(str)) { String[] strAgg = str.split(","); if(strAgg != null && strAgg.length >0){ for(String st: strAgg){ inStr += "'"+st+"',"; } inStr = inStr.substring(0,inStr.length()-1); } } return inStr; } //過濾重複的元素 public static String getInStr(String str){ String inStr = ""; Set<String> set = new HashSet<>(); if(StringUtils.isNotBlank(str)) { String[] strAgg = str.split(","); if(strAgg != null && strAgg.length >0){ for(String st: strAgg){ if (set.add(st)){ inStr += "'"+st+"',"; } } inStr = inStr.substring(0,inStr.length()-1); } } return inStr; }
mapper.xml
<if test="str != null and str != ''">
and temp.str in (${str})
</if>