拼接查询条件为list集合的sql函数
2017-08-30 23:42
323 查看
当deptId 为1时 sql语句不做更改
当deptId 为其他数字时 list中的id项作为sql查询条件
刚开始编写的时候思路是用or语句进行循环遍历 重复加上or的sql语句来查询
发现比较复杂 最后采取了sql的in函数来实现
编写完成后发现函数有可以提高函数的复用性,将mtMaintenanceStandard.dept_Id设为传入的变量
最后得到
更为通用的版本是不用判断deptId是否为1
当deptId 为其他数字时 list中的id项作为sql查询条件
刚开始编写的时候思路是用or语句进行循环遍历 重复加上or的sql语句来查询
发现比较复杂 最后采取了sql的in函数来实现
public String getDeptIdSql(List<Long> deptIdList){ Iterator<Long> it = deptIdList.iterator(); //当部门id为1时 不采用筛选 while(it.hasNext()){ Long deptId = it.next(); if(deptId == 1){ return ""; } } //拼接in条件语句 String s = ""; for(int i = 0; i < deptIdList.size();i++){ if(i!=(deptIdList.size()-1)){ s += deptIdList.get(i) + ","; }else{ s += deptIdList.get(i); } } String sql = " and mtMaintenanceStandard.dept_Id in (" + s + ") "; return sql; }
编写完成后发现函数有可以提高函数的复用性,将mtMaintenanceStandard.dept_Id设为传入的变量
最后得到
public String getDeptIdSql(String condition,List<Long> deptIdList){ Iterator<Long> it = deptIdList.iterator(); //当部门id为1时 不采用筛选 while(it.hasNext()){ Long deptId = it.next(); if(deptId == 1){ return ""; } } //拼接in条件语句 String s = ""; for(int i = 0; i < deptIdList.size();i++){ if(i!=(deptIdList.size()-1)){ s += deptIdList.get(i) + ","; }else{ s += deptIdList.get(i); } } String sql = " and " + condition + " in (" + s + ") "; return sql; }
更为通用的版本是不用判断deptId是否为1
public String getDeptIdSql(String condition,List<Long> deptIdList){ String s = ""; for(int i = 0; i < deptIdList.size();i++){ if(i!=(deptIdList.size()-1)){ s += deptIdList.get(i) + ","; }else{ s += deptIdList.get(i); } } String sql = " and " + condition + " in (" + s + ") "; return sql; }
相关文章推荐
- 用list集合做多条件中含日期范围的查询实例
- 在List范型集合中实现自定义条件复杂查询
- 条件查询List集合,由查询的结果,算出一行数据,添加到集合中!
- List集合使用Linq进行in条件查询
- iBatis查询条件迭代list集合
- 在List范型集合中实现自定义条件复杂查询 .
- 在List范型集合中实现自定义条件复杂查询
- 在List范型集合中实现自定义条件复杂查询
- mybatis中传入一个List或Map集合作为查询条件的参数
- 指定查询条件,查询对应的集合List(单表)
- 多条件查询的sql语句字符串拼接
- 利用反射拼接SQL查询条件字符串
- JavaBean的list集合拼接成Insertl语句
- Mybatis查询实例,sql中的in在Mybatis中怎么写--以list为查询条件
- SQL中多条件查询的处理,避免使用SQL拼接
- list集合根据条件删除元素
- LINQ To SQL 拼接查询条件
- C# SQL多条件查询拼接技巧
- 查询一张表内某字段不用条件的排序集合
- 易语言的sql操作时的多个查询条件拼接方法