当前位置:   article > 正文

mybatis sql查询动态标签消除 and 和 or 的方法_mybatis or标签

mybatis or标签

前言

mybatis sql查询动态标签消除 and 和 or的方法

消除 and 和 or的方法有以下两种:

  • 第一种:学会用<where> <if test>等标签
  • 第二种:同时需要掌握 1=1 和 1=0 在mybatis sql中的妙用

例子: user 表

idnameagesexhobbyheightweightscoreschool
1小米18足球17880A北大
2小红15游泳15240B北大
3小美18篮球16250B华大
4小军19游泳18260C没有读书

案例题目

成员4人,评4个奖项

  • 通用要求
    必须是大学生
  • 1、刚强奖(ganqiangj)
    要求:男、成年、分数必须是A
  • 2、柔美奖(roumeigj)
    要求:女,成年、分数必须是B以上
  • 3、可期奖(keqij)
    要求:男女不限,未成年,分数以上

搜索思路

1、可以查询全部
2、也可以按照奖项查询(多个奖项查询)
3、按照年龄排序
4、同时也可以根据爱好种类查询

案例分析

需要掌握的内容:
<where> 标签的使用
<if test> 标签的使用

1. 案例

<select id="getUser" resultType="user" parameterType="map">
    select * from user where
    <if test="id != null">
        id = #{id}
    </if>
    <if test="name!= null">
        and name = #{name}
    </if>
</select>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

当id==null,name!=null的时候,SQL语句变成:select * from user where and name = #{name}

2. 方法1:使用where标签解决问题

<select id="getStudents" resultType="student" parameterType="map">
    select * from mybatis_test.student
    <where>
        <if test="id != null">
            id = #{id}
        </if>
        <if test="name!= null">
            and name = #{name}
        </if> 
    </where>
</select>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

where标签会在sql语句中添加where。
where后面的语句以and或者or开头的时候,where标签会自动删除开头的and和or

3. 方法2:使用1=1解决问题

<select id="getUser" resultType="user" parameterType="map">
    select * from user 
    where 1 = 1
    <if test="id != null">
        and id = #{id}
    </if>
    <if test="name!= null">
        and name = #{name}
    </if>
</select>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

出现问题

以上的案例可以看出 是and或者or开头的时候,where标签会自动删除开头的and和or
但是如果是以下案例,就没办法消除

  • 默认以下案例有3个参数 isGanqiangjisRoumeigjisKeqij

因为奖项是组合判断,如果同时查找满足‘或’条件的两个奖项,需要判断比较麻烦

<select id="getStudents" resultType="student" parameterType="map">
    select * from mybatis_test.student
    <where>
        <if test="id != null">
            id = #{id}
        </if>
        and (
            <if test="isGanqiangj">
                (sex = '男' and age >= 18 and score >= 'A')
            </if>
            <if test="isRoumeigj">
                or (sex = '女' and age >= 18 and score >= 'B')
            </if>
        )
    </where>
</select>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 当isRoumeigj==isGanqiangj!=false的时候,SQL语句变成:select * from user where and name = #{name} and ( or (sex = ‘女’ and age >= 18 and score >= ‘B’) ), 该查询就会报错。

解决问题,采用 1=0

该案例,很明显标签没办法帮忙我们解决问题,所以 1=0 登场了

<select id="getStudents" resultType="student" parameterType="map">
    select * from mybatis_test.student
    <where>
        <if test="id != null">
            id = #{id}
        </if>
        and (
            1 = 0 or
            <if test="isGanqiangj">
                (sex = '男' and age >= 18 and score >= 'A')
            </if>
            <if test="isRoumeigj">
                or (sex = '女' and age >= 18 and score >= 'B')
            </if>
        )
    </where>
</select>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 当isRoumeigj==isGanqiangj!=false的时候,SQL语句变成:select * from user where and name = #{name} and ( 1=0 or (sex = ‘女’ and age >= 18 and score >= ‘B’) ), 完美解决问题
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/855279
推荐阅读
  

闽ICP备14008679号