个人小工具

Posted 原野里的回忆

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了个人小工具相关的知识,希望对你有一定的参考价值。

该方法是sql中where部分的拼写方法。如果感觉有不对的地方可以在评论中指出,并写出正确的代码。

  1     /**
  2      * 
  3      * 方法描述:拼接Where条件。
  4      * 2016-4-1 下午2:12:14
  5      * @param columns("column [|judge][|matching]")judge不写默认为 and,如果val里的值为Object[]则自动为in,matching不写默认为 = 
  6      * @param value如果是要用
  7      * @param param   参数值,
  8      * @return
  9      */
 10     @SuppressWarnings("unchecked")
 11     public static String getSqlWhere(String[] columns, Object[] values, List<Object> param){
 12         StringBuilder where = new StringBuilder();
 13         if(columns != null && columns.length > 0){
 14             if(columns.length == values.length){
 15                 for(int i=0;i<columns.length;i++){
 16                     int prevIndex = i-1;
 17                     String col = columns[i];
 18                     Object val = values[i];
 19                     String column = ""; //字段名
 20                     String judge = prevIndex < 0 ?"and":columns[prevIndex].contains("(")?"":"and";  //条件标示
 21                     String matching = "=";     //判断标示
 22                     String brackets = "";
 23                     if(!StringUtil.stringIsNull(col)){
 24                         if(col.split("\\|").length == 1){
 25                             column = col;
 26                         }else if(col.split("\\|").length == 2){
 27                             column = col.split("\\|")[0];
 28                             judge = StringUtil.stringIsNull(col.split("\\|")[1])?"and":col.split("\\|")[1];
 29                         }else if(col.split("\\|").length == 3){
 30                             column = col.split("\\|")[0];
 31                             judge = StringUtil.stringIsNull(col.split("\\|")[1])?"and":col.split("\\|")[1];
 32                             matching = StringUtil.stringIsNull(col.split("\\|")[2])?"=":col.split("\\|")[2];
 33                         }
 34                     }
 35                     if(val == null && !StringUtil.stringIsNull(matching) && (matching.equalsIgnoreCase("is") || matching.equalsIgnoreCase("not"))){
 36                         where.append(" ").append(judge).append(" ").append(column);
 37                         if(judge.equalsIgnoreCase("and") || judge.equalsIgnoreCase("or")){
 38                             if(matching.equalsIgnoreCase("is")){
 39                                 where.append(" is ").append(val);
 40                             }else if(matching.equalsIgnoreCase("not")){
 41                                 where.append(" is not ").append(val);
 42                             }
 43                         }
 44                     }
 45                     if(judge.equalsIgnoreCase("(")){
 46                         //此时的column为判断词
 47                         where.append(" ").append(column).append(" (");
 48                     }else if(column.equalsIgnoreCase("(")){
 49                         where.append(" ").append(judge).append(" (");
 50                     }else if(column.equalsIgnoreCase(")")){
 51                         where.append(" ) ");
 52                     }else{
 53                         if(column.contains(")")){
 54                             brackets = column.substring(column.length()-1, column.length());
 55                             column = column.substring(0, column.length()-1);
 56                         }
 57                     }
 58                     if(val instanceof String && !StringUtil.stringIsNull(val.toString())){
 59                         if(judge.equalsIgnoreCase("and") || judge.equalsIgnoreCase("or") || judge == ""){
 60                             if(matching.equalsIgnoreCase("like")){
 61                                 where.append(" ").append(judge).append(" ");
 62                                 where.append(" instr( ").append(column).append(" ,?) ");
 63                                 param.add(val);
 64                             }else if(matching.equalsIgnoreCase("in")){
 65                                 where.append(" ").append(judge).append(" ");
 66                                 where.append(" ").append(column).append(" in("+val+") ");
 67                             }else if(matching.equalsIgnoreCase(">")){
 68                                 where.append(" ").append(judge).append(" ");
 69                                 where.append(" ").append(column).append(" > ? ");
 70                                 param.add(val);
 71                             }else if(matching.equalsIgnoreCase("<")){
 72                                 where.append(" ").append(judge).append(" ");
 73                                 where.append(" ").append(column).append(" > ? ");
 74                                 param.add(val);
 75                             }else if(matching.equalsIgnoreCase(">=")){
 76                                 where.append(" ").append(judge).append(" ");
 77                                 where.append(" ").append(column).append(" >= ? ");
 78                                 param.add(val);
 79                             }else if(matching.equalsIgnoreCase("<=")){
 80                                 where.append(" ").append(judge).append(" ");
 81                                 where.append(" ").append(column).append(" <= ? ");
 82                                 param.add(val);
 83                             }else if(matching.equalsIgnoreCase("!=")){
 84                                 where.append(" ").append(judge).append(" ");
 85                                 where.append(" ").append(column).append(" != ? ");
 86                                 param.add(val);
 87                             }else{
 88                                 where.append(" ").append(judge).append(" ").append(column);
 89                                 where.append(" = ? ");
 90                                 param.add(val);
 91                             }
 92                         }
 93                     }
 94                     if(val instanceof Integer && val != null){
 95                         if(judge.equalsIgnoreCase("and") || judge.equalsIgnoreCase("or") || judge == ""){
 96                             if(matching.equalsIgnoreCase("like")){
 97                                 where.append(" ").append(judge).append(" ");
 98                                 where.append(" instr( ").append(column).append(" ,?) ");
 99                                 param.add(val);
100                             }else if(matching.equalsIgnoreCase(">")){
101                                 where.append(" ").append(judge).append(" ");
102                                 where.append(" ").append(column).append(" > ? ");
103                                 param.add(val);
104                             }else if(matching.equalsIgnoreCase("<")){
105                                 where.append(" ").append(judge).append(" ");
106                                 where.append(" ").append(column).append(" > ? ");
107                                 param.add(val);
108                             }else if(matching.equalsIgnoreCase(">=")){
109                                 where.append(" ").append(judge).append(" ");
110                                 where.append(" ").append(column).append(" >= ? ");
111                                 param.add(val);
112                             }else if(matching.equalsIgnoreCase("<=")){
113                                 where.append(" ").append(judge).append(" ");
114                                 where.append(" ").append(column).append(" <= ? ");
115                                 param.add(val);
116                             }else if(matching.equalsIgnoreCase("!=")){
117                                 where.append(" ").append(judge).append(" ");
118                                 where.append(" ").append(column).append(" != ? ");
119                                 param.add(val);
120                             }else{
121                                 where.append(" ").append(judge).append(" ").append(column);
122                                 where.append(" = ? ");
123                                 param.add(val);
124                             }
125                         }
126                     }
127                     if(val instanceof List && ((List<Object>)val).size() > 0 ){
128                         where.append(" ").append(judge).append(" ").append(column).append(" in (");
129                         for(int j = 0;j<((List<Object>)val).size();j++){
130                             if(((List<Object>)val).get(j) != null){
131                                 where.append(" ? ");
132                                 if(j+1!=((List<Object>)val).size()){
133                                     where.append(",");
134                                 }
135                                 param.add(((List<Object>)val).get(j));
136                             }
137                         }
138                         where.append(" ) ");
139                     }
140                     if(val instanceof Object[] && ((Object[])val).length > 0 ){
141                         where.append(" ").append(judge).append(" ").append(column).append(" in (");
142                         for(int j = 0;j<((Object[])val).length;j++){
143                             if(((Object[])val)[j] != null){
144                                 where.append(" ? ");
145                                 if(j+1!=((Object[])val).length){
146                                     where.append(",");
147                                 }
148                                 param.add(((Object[])val)[j]);
149                             }
150                         }
151                         where.append(" ) ");
152                     }
153                     if(brackets.equalsIgnoreCase(")")){
154                         where.append(" ) ");
155                     }
156                 }
157             }else{
158                 System.out.println("字段和值的数量不相等!!!");
159             }
160         }
161         
162         return where.toString();
163     }
164     

例子:

1         System.out.println(getSqlWhere(new String[]{"claId","claId|or","claIds","claIds|and|like"},new Object[]{1,2,new Object[]{1,2,1,3},88},new ArrayList()));
2         System.out.println(getSqlWhere(new String[]{"btpTeaId||>=","btpClaId","btpClaArrTeaUserId","btpTeaId||not","or|(","detpId",")"}, new Object[]{"asd","1,1,21".split(","),3,null,null,45,null},new ArrayList()));
3         System.out.println(getSqlWhere(new String[]{"or|(","detpId",")","(","detpId",")"}, new Object[]{null,45,null,null,45,null},new ArrayList()));
4         System.out.println(getSqlWhere(new String[]{"(deptId","deptName)|or|!=","(deptId","deptName)|or|!="}, new Object[]{32,"光秃",32,"光秃"},new ArrayList()));
5         System.out.println(getSqlWhere(new String[]{"postName||like","postStatus","postIsDeleted","(","da","rr"+"|or|is",")"}, new Object[]{44,0,0,null,21,null,null},new ArrayList()));

 

以上是关于个人小工具的主要内容,如果未能解决你的问题,请参考以下文章

VS中添加自定义代码片段——偷懒小技巧

提效小技巧——记录那些不常用的代码片段

massCode 一款优秀的开源代码片段管理器

个人小工具

json 个人的vscode的代码片段

SnippetsLab for Mac 1.9 中文共享版 – 强大的代码收藏管理工具