`
砺雪凝霜
  • 浏览: 158662 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

给大家推荐一个比较方便的拼凑sql的方法

 
阅读更多

 

前台传参数的时候可以这么传:

<input name = "sp[0]" value = ""/>

<input name = "sp[1]" value = ""/>                  

........................ sp[14].................

sql拼凑的时候就可以这么写

private String getsqlcsp(List<String> sp) {
  String sqlc = "";
  for (int i = 0; i < sp.size(); i++) {
   if (StringUtils.isNotBlank(sp.get(i))) {
    String[] code = sp.get(i).split(";");
    if (code.length > 0) {
     for (int j = 0; j < code.length; j++) {
      switch (i) {
      case 0:
       if (code[j] == "") {
        break;
       } else {
        sqlc += " and t.gender = '" + code[j] + "'";
        break;
       }
      case 1:

       if (j == 0) {
        if (code.length == 1) {
         sqlc += " and ( " + code[j] + ")";
        } else {
         sqlc += " and ( " + code[j];
        }
       } else if (j == code.length - 1) {
        sqlc += " or " + code[j] + ")";
       } else {
        sqlc += " or " + code[j];
       }
       break;
      case 2:
       if (j == 0) {
        if (code.length == 1) {
         sqlc += " and t.id in (select m.user_id from user_organization m where m.organization_name like '%"
           + code[j] + "%')";
        } else {
         sqlc += " and t.id in (select m.user_id from user_organization m where m.organization_name like '%"
           + code[j] + "%'";
        }
       } else if (j == code.length - 1) {
        sqlc += " or m.organization_name like '%"
          + code[j] + "%')";
       } else {
        sqlc += " or m.organization_name like '%"
          + code[j] + "%'";
       }
       break;
      case 3:
       sqlc += " and t.contact = '" + code[j] + "'";
       break;
      case 4:
       sqlc += " and t.area1 = '" + code[j] + "'";
       break;
      case 5:
       sqlc += " and t.area2 = '" + code[j] + "'";
       break;
      case 6:
       code[j] = code[j].trim();
       if (j == 0) {
        if (code.length == 1) {
         sqlc += " and t.id in (select s.user_id from user_role s  where  s.role_code = '"
           + code[j] + "')";
        } else {
         sqlc += " and t.id in ( select s.user_id from user_role s where s.role_code = '"
           + code[j] + "'";
        }
       } else if (j == code.length - 1) {
        sqlc += " or  s.role_code = '" + code[j] + "')";
       } else {
        sqlc += " or  s.role_code = '" + code[j] + "'";
       }
       break;
      case 7:
       code[j] = code[j].trim();
       if (j == 0) {
        if (code.length == 1) {
         sqlc += " and ( " + code[j] + ")";
        } else {
         sqlc += " and ( " + code[j];
        }
       } else if (j == code.length - 1) {
        sqlc += " or " + code[j] + ")";
       } else {
        sqlc += " or " + code[j];
       }
       break;
      case 8:
       code[j] = code[j].trim();
       if (j == 0) {
        if (code.length == 1) {
         sqlc += " and t.job_title = '" + code[j]
           + "'";
        } else {
         sqlc += " and (t.job_title = '" + code[j]
           + "'";
        }
       } else if (j == code.length - 1) {
        sqlc += " or t.job_title = '" + code[j] + "')";
       } else {
        sqlc += " or t.job_title = '" + code[j] + "'";
       }
       break;
      case 9:
       code[j] = code[j].trim();
       if (j == 0) {
        if (code.length == 1) {
         sqlc += " and t.pc_activities like '%"
           + code[j] + "%'";
        } else {
         sqlc += " and (t.pc_activities like '%"
           + code[j] + "%'";
        }
       } else if (j == code.length - 1) {
        sqlc += " or t.pc_activities like '%" + code[j]
          + "%')";
       } else {
        sqlc += " or t.pc_activities like '%" + code[j]
          + "%'";
       }
       break;
      case 10:
       sqlc += " and t.ms_eff_date > to_date('" + code[j]
         + " 00:00:00','yyyy/mm/dd hh24:mi:ss')";
       break;
      case 11:
       sqlc += " and t.ms_eff_date < to_date('" + code[j]
         + " 23:59:59','yyyy/mm/dd hh24:mi:ss')";
       break;
      case 12:
       sqlc += " and t.time_for_now > to_date('" + code[j]
         + " 00:00:00','yyyy/mm/dd hh24:mi:ss')";
       break;
      case 13:
       sqlc += " and t.time_for_now < to_date('" + code[j]
         + " 23:59:59','yyyy/mm/dd hh24:mi:ss')";
       break;
      case 14:
       sqlc += " and to_char(sysdate,'yyyy') - to_char(ms_eff_date,'yyyy') >= "
         + Integer.parseInt(code[j]);
       break;
      case 15:
       sqlc += " and to_char(sysdate,'yyyy') - to_char(ms_eff_date,'yyyy') <= "
         + Integer.parseInt(code[j]);
       break;
      case 16:
       code[j] = code[j].trim();
       if (j == 0) {
        if (code.length == 1) {
         sqlc += " and t.id in (select s.user_id from log_user_sing s where s.sing_code like '%"
           + code[j] + "%')";
        } else {
         sqlc += " and t.id in (select s.user_id from log_user_sing s where s.sing_code like '%"
           + code[j] + "%'";
        }
       } else if (j == code.length - 1) {
        sqlc += " or s.sing_code like '%" + code[j]
          + "%')";
       } else {
        sqlc += " or s.sing_code like '%" + code[j]
          + "%'";
       }
       break;
      }
     }
    }
   }
  }
  return sqlc;
 }

 

分享到:
评论

相关推荐

    ORM机制 本人的项目开发经验

    2. **静态且不清晰的SQL拼凑**:手动拼接SQL语句不仅效率低下,而且容易出错,特别是当SQL需要动态构建时。IBatisNet提供了一种动态SQL语法,允许开发者在配置文件中定义SQL,使SQL更加清晰,更易于维护,并支持动态...

    爬取邮编爬虫,拼图小游戏源码

    在IT领域,爬虫技术与游戏开发是两个重要的分支,这次我们关注的是一款"邮编爬虫"和一个"拼图小游戏"的源码。首先,让我们深入了解一下这两个概念。 **邮编爬虫**: 邮编爬虫是一种利用编程语言(如Python)编写的...

    信息技术教学辅助平台1.04版源码

    2010.02.23增加一个作品评价等级:推荐,分值为12分,将作为优秀作品选的依据;增加天气预报开关(管理员面板--系统设置) 2010.01.28修正投票时所有作品可查看,但不能再重新提交作品,不投票则可修改作品 2010.01....

    二十三种设计模式【PDF版】

    各司其职的类串成一串,好象击鼓传花,当然如果自己能完成,就不要推委给下一个. 设计模式之 Mediator(中介) Mediator 很象十字路口的红绿灯,每个车辆只需和红绿灯交互就可以. 设计模式之 State(状态) 状态是编程中...

    数据挖掘模拟题与部分答案

    - **数据仓库是从一个点上观察整个企业,而不是许多小定义的“地下仓库”的拼凑集合**:数据仓库能够整合来自不同源的数据,提供统一视图。 - **数据挖掘记录的是最令人感兴趣的详细的数据**:通过数据挖掘技术,...

    ThinkPHP标签制作教程

    该方法接收一个父ID值,返回所有符合条件的栏目信息。代码示例如下: ```php public function getCategorys($parentid, $withSelf = 0) { $parentid = intval($parentid); $categorys = $this-&gt;where(array('...

Global site tag (gtag.js) - Google Analytics