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

摘要:
packagecom.lynch.erp.core.util;importjava.util.Map;importorg.apache.commons.collections.MapUtils;importorg.apache.commons.lang3.StringUtils;publicclassSQLUtils{/***通过Map拼接InsertSQL语句**@paramtableName*
packagecom.lynch.erp.core.util;

importjava.util.Map;

importorg.apache.commons.collections.MapUtils;
importorg.apache.commons.lang3.StringUtils;

public classSQLUtils {
     
    /*** 通过Map拼接Insert SQL语句
     * 
     * @paramtableName
     * @paramdataMap
     * @return
     */
    public static String genSqlInsert(String tableName, Map<String, String>dataMap) {
        if(MapUtils.isEmpty(dataMap)) {
            return null;
        }
        
        //生成INSERT INTO table(field1,field2) 部分
        StringBuffer sbField = newStringBuffer();
        //生成VALUES('value1','value2') 部分
        StringBuffer sbValue = newStringBuffer();
        
        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语句
     * 
     * @paramtableName
     * @paramoperation
     * @paramdataMap
     * @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 = newStringBuffer();
        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);
    }


}

免责声明:文章转载自《JAVA通过Map拼接SQL语句(Insert Update语句)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇go json序列化不填充默认值openwrt固件支持3G和4G上网卡下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

使用sdk方式获取微信的用户信息

一、pom.xml 依赖 <dependency> <groupId>com.github.binarywang</groupId> <artifactId>weixin-java-mp</artifactId>...

jsp向后台传中文,tomcat和weblogic下中文乱码解决方法

前端jsp:GBK编码var groupsName = "全体组";window.location.href = "http://t.zoukankan.com/<%=path %>/CustomersServlet?method=service_customer&skillGroups="+groupsName;后台:GBK编码Str...

string中的CopyonWrite技术

在谈这项技术之前,我们先来了解一下string类内存分配。string类有一个私有成员,其类型是一个char*,记录用户从堆上分配内存的地址,其在构造时分配内存,在析构时释放内存。因为是从堆上分配内存,所以string类在维护这块内存上是格外小心的,string类在返回这块内存地址时,只返回const char*,也就是只读的,如果你要写,你只能通过str...

Redis系统学习之自定义RedisTemplate

自定义RedisTemplate 序列化源码分析 在JAVA程序中看到中文是没有问题的,但是在Redis客户端工具,也就是命令行中看见是编码的 继续分析源码 查看RedisTemplate.class 在RedisAutoConfiguration.class中点击 在上面可以看到序列化支持的 往下稍微滑动一些可以看到,默认采用的是JDK的序列化,...

高效的数据压缩编码方式 Protobuf

一. protocol buffers 是什么? Protocol buffers 是一种语言中立,平台无关,可扩展的序列化数据的格式,可用于通信协议,数据存储等。 Protocol buffers 在序列化数据方面,它是灵活的,高效的。相比于 XML 来说,Protocol buffers 更加小巧,更加快速,更加简单。一旦定义了要处理的数据的数据结构之...

java获取Json和http状态码

最近再做接口自动化测试,其中有几个方法比较重要 1.获取http状态码 /* * 返回接口状态码 * */ public static String getHttpCode(String url) { String code = null;...