码迷,mamicode.com
首页 > 数据库 > 详细

SQL拼接小技巧(一)

时间:2018-06-21 19:45:35      阅读:665      评论:0      收藏:0      [点我收藏+]

标签:enum   user   name   tin   BMI   dateutils   dex   字段名   方式   

# SQL拼接小技巧(一) #

2018/6/21 18:54:36         

----------


**关键字:  SQL , 1 = 1 ,1 = 2**    

----------


如下代码中SQL拼接使用了1=1和1=2防止请求参数中条件为空或SQL注入的方式猜测表名,字段名

    public DomainPage<RealNameVerifyInfo> getVerifyList(String vin, String name, String phoneNum, String status,
    String dealerCode, String type, long startTime,
    long endTime, int pageIndex, int pageSize) {
    StringBuilder sb = new StringBuilder("SELECT * FROM REAL_NAME_VERIFY_INFO r WHERE 1 = 1 ");
    if (StringUtils.isNotBlank(vin)) {
    sb.append(" and r.vin like ‘%").append(vin).append("%‘ ");
    }
    if (StringUtils.isNotBlank(name)) {
    sb.append(" and r.userName = ‘").append(name).append("‘");
    }
    if (StringUtils.isNotBlank(dealerCode)) {
    sb.append(" and r.dealerCode = ‘").append(dealerCode).append("‘");
    }
    if (StringUtils.isNotBlank(type)) {
    sb.append(" and r.type = ‘").append(type).append("‘");
    }
    if (StringUtils.isNotBlank(phoneNum)) {
    sb.append(" and r.phoneNum = ‘").append(phoneNum).append("‘");
    }
    if (startTime > 0) {
    sb.append(" and r.createdTime >= ‘").append(DateUtilsXX.dateToString(new Date(startTime), 1)).append("‘");
    }
    
    if (endTime > 0) {
    sb.append(" and r.createdTime < ‘").append(DateUtilsXX.dateToString(new Date(endTime), 1)).append("‘");
    }
    if (StringUtils.isNotBlank(status)) {
    String[] var1 = status.split(",");
    sb.append(" and ( 1 = 2 ");
    for (String var2 : var1) {
    if (var2.equals("VERIFYING")) {
    sb.append(" or ( r.status = ‘VERIFYING‘ )");
    } else if (var2.equals("SUCCEED")) {
    sb.append(" or ( r.status = ‘SUCCEED‘ )");
    } else if (var2.equals("FAILED")) {
    sb.append(" or ( r.status = ‘FAILED‘ )");
    }
    }
    sb.append(" ) ");
    } else {
    sb.append(" and r.status != ‘").append(VerifyStatus.UNVERIFY).append("‘");
    sb.append(" and r.status != ‘").append(VerifyStatus.SUBMITED).append("‘");
    sb.append(" and r.status != ‘").append(VerifyStatus.PIC_DEALING).append("‘");
    sb.append(" and r.status != ‘").append(VerifyStatus.PIC_WAITING).append("‘");
    sb.append(" and r.status != ‘").append(VerifyStatus.CANCEL).append("‘");
    sb.append(" and r.status != ‘").append(VerifyStatus.SUBMITTING).append("‘");
    }
    String sql = sb.toString();
    Query query = em().createNativeQuery(" SELECT a.* FROM ( " + sql + " ORDER BY r.createdTime DESC ) as a  GROUP BY a.vin ORDER BY a.createdTime DESC", RealNameVerifyInfo.class);
    log.info(" SELECT a.* FROM ( " + sql + " ORDER BY r.createdTime DESC ) as a  GROUP BY a.vin ORDER BY a.createdTime DESC ");
    query.setFirstResult((pageIndex - 1) * pageSize);
    query.setMaxResults(pageSize);
    List<RealNameVerifyInfo> verifyInfoList = query.getResultList();
    
    query = em().createNativeQuery(" SELECT count(*) FROM ( " + sql + " GROUP BY r.vin ) as a");
    long count = Long.parseLong(String.valueOf(query.getResultList().get(0)));
    DomainPage domainPage = new DomainPage(pageSize, pageIndex, count);
    domainPage.setDomains(verifyInfoList);
    //log.info("verifyInfoList" + JSONUtil.writeValueAsString(verifyInfoList));
    //log.info("domainPage" + JSONUtil.writeValueAsString(domainPage));
    return domainPage;
    }
                                                                         

SQL拼接小技巧(一)

标签:enum   user   name   tin   BMI   dateutils   dex   字段名   方式   

原文地址:https://www.cnblogs.com/zhangqingyan/p/9210578.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!