sqlUtils
2021/6/15 19:27:17
本文主要是介绍sqlUtils,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
动态拼接sql
import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map.Entry; import org.apache.commons.lang3.StringUtils; import com.alibaba.fastjson.JSONObject; /** * ************************************************************************* * <PRE> * @ClassName: : SqlUtils * * @Description: : dynamic sql * * @Creation Date : Jun 15, 2021 1:41:48 PM * * @Author : Sea * * * </PRE> ************************************************************************** */ public class SqlUtils { // @Test // public void testSql() throws Exception { // JSONObject IncriteriaMap = new JSONObject(); // IncriteriaMap.put("sea","aa,bb,cc,dd"); // JSONObject mycriteriaMap = new JSONObject(); // mycriteriaMap.put("in", IncriteriaMap); // String inlude="name,age,total"; // String sql="select " +inlude+ " from user where "+ mapToWhere(mycriteriaMap) +" order by sold desc"; // System.err.println(sql); // } /** * JSONObject andcriteriaMap = new JSONObject(); andcriteriaMap.put("sea", "sea"); andcriteriaMap.put("number", 123); andcriteriaMap.put("double", 12.31); JSONObject orcriteriaMap = new JSONObject(); orcriteriaMap.put("sea", "sea"); orcriteriaMap.put("double", 12.31); JSONObject IncriteriaMap = new JSONObject(); IncriteriaMap.put("sea","aa,bb,cc,dd"); JSONObject mycriteriaMap = new JSONObject(); mycriteriaMap.put("and", andcriteriaMap); mycriteriaMap.put("or", andcriteriaMap); mycriteriaMap.put("in", IncriteriaMap); * @param mycriteriaMap * @return * @throws Exception */ public static String mapToWhere(JSONObject mycriteriaMap) throws Exception { String criteria=""; int criteriaMapsize = mycriteriaMap.size(); for (Entry<String, Object> okv : mycriteriaMap.entrySet()) { String option = okv.getKey(); HashMap<String, Object> criteriaMap=(HashMap<String, Object>) okv.getValue();; int msize=criteriaMap.size(); for (Entry<String, Object> kv : criteriaMap.entrySet()) { String key = kv.getKey(); Object value = kv.getValue(); if(StringUtils.isBlank(key)||StringUtils.isBlank(value+"")) { continue; } //if option is in if("in".equalsIgnoreCase(option)) { criteria+=" " +key+" in( " +convert2SqlIn(Arrays.asList((value+"").split(","))) +" )"; }else //option is and | or { if(String.class.isInstance(value)) { criteria+=" " +key+"='" +value +"' "; }else { criteria+=" " +key+"=" +value +" "; } msize--; if(msize!=0) { criteria+=" "+option+" "; } } } criteriaMapsize--; if(criteriaMapsize!=0) { criteria+=" and "; } }; return criteria; } /** * @Desc list<String> to sql in * @param list<String> * @return */ public static String convert2SqlIn(List<String> list){ StringBuilder sb = new StringBuilder(); if(list != null && list.size()>0){ for(int i=0,len=list.size();i<len;i++){ sb.append("'"+ list.get(i) + "'"); if(i < len-1){ sb.append(","); } } } return sb.toString(); } }
这篇关于sqlUtils的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23Springboot应用的多环境打包入门
- 2024-11-23Springboot应用的生产发布入门教程
- 2024-11-23Python编程入门指南
- 2024-11-23Java创业入门:从零开始的编程之旅
- 2024-11-23Java创业入门:新手必读的Java编程与创业指南
- 2024-11-23Java对接阿里云智能语音服务入门详解
- 2024-11-23Java对接阿里云智能语音服务入门教程
- 2024-11-23JAVA对接阿里云智能语音服务入门教程
- 2024-11-23Java副业入门:初学者的简单教程
- 2024-11-23JAVA副业入门:初学者的实战指南