AEyanami 2021-11-28 10:36 采纳率: 50%
浏览 57
已结题

请问关于Mybaits的动态SQL的if标签如有三个条件中间的为空,后面条件不执行如何解决?

问题遇到的现象和发生背景

-在学习Mybaits时发现如果Mybaits的动态SQL的if标签中如果有三个条件, 第二个条件的为空。
运行时只执行了第一个。为空的条件和后面的条件都不会执行如何解决呢?
谢谢

问题相关代码,请勿粘贴截图
**<resultMap id="brnadResultMap" type="brand">
        <result column="brand_name" property="brandName"/>
        <result column="company_name" property="companyName"/>
 </resultMap>


<select id="selectByConfige" resultMap="brnadResultMap">
        select *
        from t_brand
        <where>
        <if test="status != NULL">
        and status = #{status}
        </if>
        <if test=" companyName != null and companyName !='' ">
            and company_name like #{companyName}
        </if>
        <if test=" brand_name != null and brand_name !='' ">
            and brand_name like #{brandName};
        </if>
        </where>
    </select>
**

运行结果及报错内容

当中间的条件companyName为空时 运行的结果(部分)
[DEBUG] 10:31:39.233 [main] c.a.m.B.selectByConfige - ==> Preparing: select * from tb_brand WHERE status = ?
[DEBUG] 10:31:39.259 [main] c.a.m.B.selectByConfige - ==> Parameters: 1(Integer)
[DEBUG] 10:31:39.270 [main] c.a.m.B.selectByConfige - <== Total: 3

当最后的条件brandName为空时 运行的结果(部分)
[DEBUG] 10:32:54.654 [main] c.a.m.B.selectByConfige - ==> Preparing: select * from tb_brand WHERE status = ? and company_name like ?
[DEBUG] 10:32:54.680 [main] c.a.m.B.selectByConfige - ==> Parameters: 1(Integer), %华为%(String)
[DEBUG] 10:32:54.695 [main] c.a.m.B.selectByConfige - <== Total: 1

我想要达到的结果

想问下有没有什么方法让中间条件没有时,也能执行后面有值的条件?谢谢

  • 写回答

2条回答 默认 最新

  • hello_world_1996 2021-11-28 11:26
    关注

    第三个if test里面的字段名是brand_name 下面的sql里面的字段名是brandName

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月6日
  • 已采纳回答 11月28日
  • 创建了问题 11月28日

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么