• JAVA通过Map拼接SQL语句(Insert Update语句)


    package com.lynch.erp.core.util;
    
    import java.util.Map;
    
    import org.apache.commons.collections.MapUtils;
    import org.apache.commons.lang3.StringUtils;
    
    public class SQLUtils {
         
        /**
         * 通过Map拼接Insert SQL语句
         * 
         * @param tableName
         * @param dataMap
         * @return
         */
        public static String genSqlInsert(String tableName, Map<String, String> dataMap) {
            if(MapUtils.isEmpty(dataMap)) {
                return null;
            }
            
            //生成INSERT INTO table(field1,field2) 部分
            StringBuffer sbField = new StringBuffer();
            //生成VALUES('value1','value2') 部分
            StringBuffer sbValue = new StringBuffer();
            
            sbField.append("INSERT INTO " + tableName.toLowerCase() + "(");
            for(Map.Entry<String, String> entry : dataMap.entrySet()){
                String mapKey = entry.getKey();
                String mapValue = entry.getValue();
                if(StringUtils.equals(mapKey, CamelUnderlineUtil.PK)) {
                    continue;
                }
                
                sbField.append("`" + mapKey + "`,");
                sbValue.append("'" + mapValue + "',");
            }
            
            sbField = Util.deleteLastChar(sbField);
            sbValue = Util.deleteLastChar(sbValue);
            return sbField.toString() + ") VALUES(" + sbValue.toString() + ")";
        }
        
        /**
         * 通过Map拼接Update SQL语句
         * 
         * @param tableName
         * @param operation
         * @param dataMap
         * @return
         */
        public static String genSqlUpdate(String tableName, Map<String, String> dataMap) {
            if(MapUtils.isEmpty(dataMap)) {
                return null;
            }
            
            String idColumn = dataMap.get(CamelUnderlineUtil.PK);
            String idValue = dataMap.get(idColumn);
            
            StringBuffer sb = new StringBuffer();
            sb.append("UPDATE "+ tableName.toLowerCase() +" SET ");
            for(Map.Entry<String, String> entry : dataMap.entrySet()){
                String mapKey = entry.getKey();
                String mapValue = entry.getValue();
                if(StringUtils.equals(mapKey, CamelUnderlineUtil.PK)) {
                    continue;
                }
                if(StringUtils.equals(mapKey.toLowerCase(), idColumn)) {
                    continue;
                }
                sb.append("`" + mapKey + "`='" + mapValue + "',");
            }
            
            sb = Util.deleteLastChar(sb);
            
            return  String.format("%s where %s='%s'", sb.toString(), idColumn, idValue);
        }
    
    
    }
  • 相关阅读:
    Leetcode(680) ;验证回文字符串 Ⅱ
    mysql常用操作语句
    组合索引问题
    php生成一维码以及保存-转载
    php后台实现页面跳转的方法-转载
    php操作表格(写)
    虚拟机复制后上网冲突的问题
    centos下安装nginx(转载)
    虚拟机与宿主机可以互相ping通,但是外网不能
    防火墙设置:虚拟机ping不通主机,但是主机可以ping通虚拟机(转载)
  • 原文地址:https://www.cnblogs.com/linjiqin/p/13554591.html
Copyright © 2020-2023  润新知