• Mybatis解决多条件查询不能得到正确SQL的两种方法 恒等式 where标签


    在Mybatis编写多条件查询语句中,我们经常采用下面的写法

    <select id="selectByCondition" resultMap="brandResultMap">
            select * from tb_brand
            where status = #{status}
            and company_name like #{companyName}
            and brand_name like #{brandName};
    select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    上述代码同时查询status,company_name和brand_name这三个条件是否满足要求,如果这三个条件同时存在的话是可以正常运行的,但是如果其中有一个条件没有值为空,比如我只查询company_name和brand_name这两个条件,那么第一个status就会赋值为null,从而查询不到任何结果

    使用if标签初步解决问题

    Mybatis想到了这个问题,提供了一个if标签来解决问题,你不就是怕不是所有条件都会有值吗,那么加上条件就好了,就比如如果status为null的话,那么就不添加这个条件,写在代码里就是下面的代码

    if标签的主要功能就是判断一个条件是否为空

    <select id="selectByCondition" resultMap="brandResultMap">
            select * from tb_brand
            where
            <if test="status != null">
                status = #{status}
            if>
            <if test="companyName != null and companyName != '' ">
                and company_name like #{companyName}
            if>
            <if test="brandName != null and companyName != '' ">
                and brand_name like #{brandName};
            if>
    select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    上述代码如果不填company_name和brand_name都是可以正常工作的,但是如果你不填status就不能正常工作了,因为如果status=null的话,where后面直接跟and,这是一个错误的sql语法

    那么怎么办呢,我们可以用恒等式的方法暴力解决这个问题

    使用恒等式1=1暴力解决问题,但是并不优雅

    我们可以给所有的条件里面都加一个and,再在where后面带上一个1=1
    这样不管我们选择哪几个条件都能得到正确的SQL语句,恒等式解决问题的代码如下

    <select id="selectByCondition" resultMap="brandResultMap">
            select * from tb_brand
            where 1 = 1
            <if test="status != null">
                and status = #{status}
            if>
            <if test="companyName != null and companyName != '' ">
                and company_name like #{companyName}
            if>
            <if test="brandName != null and companyName != '' ">
                and brand_name like #{brandName};
            if>
    select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    上述代码的确不会报错,但是并不优雅,有没有更加优雅的方法呢?

    where标签自动得到正确的SQL语句

    Mybatis除了提供了上述的if标签,同时还有一个where标签,我们只需要把所有的查询条件都放入这个where标签就能永远得到正确的SQL语句,因为内部已经处理好了

    <select id="selectByCondition" resultMap="brandResultMap">
            select * from tb_brand
            <where>
                <if test="status != null">
                    and status = #{status}
                if>
                <if test="companyName != null and companyName != '' ">
                    and company_name like #{companyName}
                if>
                <if test="brandName != null and companyName != '' ">
                    and brand_name like #{brandName};
                if>
            where>
    select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
  • 相关阅读:
    全志V853芯片在Tina下E907启动方式的选择
    PreScan快速入门到精通第二十三讲2D车辆动力学模型
    什么是单点登录?什么又是 OAuth2.0?
    软件测试工作的难点是什么?针对难点怎么做?
    PC网上订货系统的定义与组成|企业手机APP订单管理软件
    Win11共享文件打不开怎么办?Win11共享文件打不开的解决方法
    OIS、EIS原理
    首次认定20万!2022年武汉市汉阳区高新技术企业认定材料、条件和奖励补助
    自动化信息收集工具 水泽 使用教程
    【目标检测】一文干翻xml文件的读取
  • 原文地址:https://blog.csdn.net/qq_44343213/article/details/127742462