Java to achieve a simple SQL dynamic assembly tools

First Edition

package com.zh.oukele.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class CreateSqlUtil {

    public static void main(String[] args) {

        Map<String ,Object> map = new HashMap<>();
        map.put("stuName","欧可乐");
        map.put("stuAge",20);
        map.put("stuSex","男");
        map.put("Key_stuId","ASDF");
        map.put("Key_stuSex","ASDF");
        the try { 
            System.out.println (getSql ( "table_name", "Delete", Map, to false , "" )); 
        } the catch (Exception E) { 
            e.printStackTrace (); 
        } 

    } 

    / ** 
     * Simple dynamic assembly sql syntax 
     * @param tableName table 
     * @param operation operation identifier select | delete | update, the default is the SELECT 
     * @param the Map mapData collection of data 
     * @param useMySQL to true | false, false to use dynamic assembly SQL, true to their own use the sql 
     * @param mySql own sql 
     * Note: update here, where xxx = xxx, when, mapData where the key must have Key_ prefix (the other is not affected) 
     * 
     * @return 
     * @throws Exception
      * / 
    public  static String getSql (String tableName, String Operation, the Map MapData, <,??> boolean useMySQL, String mySql) throws Exception { 
        String sql = null ;
         // use the assembly sql function 
        IF (! useMySQL) {
             IF ((tableName =!! null && tableName.equals! ( "") && tableName.length ()> 0 )) {
                 the throw  new new Exception(" 参数 tableName 的值为空!");
            }else if( !(mapData != null && !mapData.equals("") && mapData.size() > 0 ) ){
                throw new Exception(" 参数 mapData 的值为空!");
            }
            // 操作标识 默认为 select
            String operations = "select";
            String condition = " a.* from " + tableName + " a where ";
            if( operation != null && !operation.equals("") ){
                if( operation.equals("update") || operation.equals("UPDATE") ){
                    operations = "update";
                    condition = " " + tableName + " a set ";
                }else if( operation.equals("delete") || operation.equals("DELETE") ){
                    operations = "delete";
                    condition = " from " + tableName + " a where ";
                }else if( operation.equals("insert") || operation.equals("INSERT") ){
                    operations = "insert";
                    condition = " into " + tableName + " values (";
                    String link = "";
                    Iterator<?> iterator = mapData.keySet().iterator();
                    while (iterator.hasNext()) {
                        String next = (String) iterator.next();
                        condition += link + next;
                        link = ",";
                    }
                    condition += ") values( ";
                }
            }
            String value= "";
            String link ="";
            String keyValueOperations = " where ";
            Iterator<? extends Map.Entry<?, ?>> iterator = mapData.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<?, ?> next = iterator.next();
                if( next.getValue() instanceof String ){
                    value = "'" + next.getValue() +"'";
                }else {
                    value = "" + next.getValue() +"";
                }
                if( next.getKey().toString().lastIndexOf("Key_") == -1 ){
                    if( !operations.equals("insert")){
                        if( operations.equals("select") || operations.equals("delete")){
                            condition += link + "a." + next.getKey();
                            condition += "=" + value;
                            link = " and ";
                        }else {
                            condition += link + "a." + next.getKey();
                            condition += "=" + value;
                            link = ",";
                        } 
                    } The else  {
                        for condition Condition + Link + = value; 
                        Link = "," ; 
                    } 
                } the else {
                     Continue ; 
                } 
            } 

            // assembled end insert sql 
            IF (operations.equals ( "INSERT" )) { 
                for condition Condition + = ")" ; 
            } the else  IF (operations.equals ( "Update")) { // assembled end update sql
                condition += " where ";
                String and = "";
                Iterator<? extends Map.Entry<?, ?>> iterator1 = mapData.entrySet().iterator();
                while (iterator1.hasNext()) {
                    Map.Entry<?, ?> next = iterator1.next();
                    if( next.getValue() instanceof String ){
                        value = "'" + next.getValue() +"'";
                    }else {
                        value = "" + next.getValue() +"";
                    }
                    String key = next.getKey().toString();
                    if( key.lastIndexOf("Key_") != -1 ){
                        key =  key.substring(key.indexOf("Key_")+ 4,key.length());
                       condition += and +"a." +key + "=" + value;
                       and = " and ";
                    }
                }
            }

            sql = operations + condition;
        }else { // 不使用组装sql的功能
            sql = mySql;
        }
        return sql;
    }
}

 

Guess you like

Origin www.cnblogs.com/oukele/p/11517890.html