SQL高级查询

本文主要是对下图的查询方式的条件判断进行讲解:

如果还有更多的查询条件,只要在下面SqlCondition类中加入相对应的字段,然后添加相应的get和set方法,最后在

getQueryCondition方法中加入相对应的条件判断语句if就可以了。
public class SqlCondition {
    
    //职位名称:用于查询的条件判断
    private String  title;
    //职位类型:用于查询的条件判断
    private Integer positiontype;
    
    public SqlCondition() {
        super();
    }
    
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Integer getPositiontype() {
        return positiontype;
    }

    public void setPositiontype(Integer positiontype) {
        this.positiontype = positiontype;
    }
    
    /**
     * 查询语句的条件判断
     * 方法一(推荐使用):
     */
    public String getQueryCondition_01(){
        String whereSql="";
        if(title !=null && !"".equals(title)){
            whereSql += " and title like '%"+title+"%'";
        }
        if(positiontype!=null && !"".equals(positiontype)){
            whereSql+=" and positiontype = "+positiontype;
        }
        //replace:替换;First:第一个
        return whereSql.replaceFirst("and", "where");
    }
        
     /**
     * 查询语句的条件判断
     * 方法二(不推荐使用): where 1==1 会影响查询的性能
     */
    public String getQueryCondition_02(){
        String  whereSql = "where 1==1";

        if(title != null && !"".equals(title)){
            whereSql+= "and title like '%"+title+"%'";
        }
        if(positiontype !=null){
            whereSql += "and positiontype = " +positiontype;
        }

        return whereSql;
    }

    /**
     * 查询语句的条件判断
     * 方法三(准备一个标识(即一个flag)
         如果标识是true,条件前就加where,如果是false,条件前就加and):
     */
    public String getQueryCondition_03(){
        //标识:flag
        boolean flag = true;
        String whereSql = "";
        //title不为null,并且不为空字符串
        if(title!=null && !"".equals(title)){
            if(flag){
                whereSql+= " where ";
                flag = false;
            }else{
                whereSql+= " and ";
            }
            whereSql += " title like '%"+title+"%' ";
        }
        if(positiontype!=null){
            if(flag){
                whereSql+= " where ";
                flag = false;
            }else{
                whereSql+= " and ";
            }
            whereSql += " positiontype = "+positiontype;
        }
        return whereSql;
    }

    /**
     * 查询语句的条件判断
     * 方法四(准备一个集合):
     */
    public String getQueryCondition_04(){
        //准备一个集合(里面会装咱们的所有条件)
        List<String> sqlList = new ArrayList<>();

        String whereSql = "";
        //title不为null,并且不为空字符串
        if(title!=null && !"".equals(title)){
            sqlList.add(" title like '%"+title+"%' ");
        }
        if(positiontype!=null){
            sqlList.add(" positiontype = "+positiontype);
        }
        //查询条件加多了,只要在这加if语句就可以了
        //遍历这个集合
        for (int i = 0; i < sqlList.size(); i++) {
            if(i==0){
                //第一次循环,前面肯定是加where
                whereSql += " where ";
            }else{
                //第2-n次循环,前面肯定是加and
                whereSql += " and ";
            }
            whereSql += sqlList.get(i);
        }

        return whereSql;
    }
}

最后在需要的SQL语句中引入条件判断方法即可。

四种方法特点分析:

  方法一:比较简单,容易看懂,不会影响查询性能,推荐使用。

  方法二:虽然比方法一少了几个代码,但 where == 1在sql查询中会影响查询性能,不建议使用。

  方法三:代码比较多,容易写错。

  方法四:比较难理解,使用相对其它几个方法比较麻烦。

原文地址:https://www.cnblogs.com/wanghj-15/p/10952746.html