皆为空语归于寂静 2021-10-11 23:03 采纳率: 0%
浏览 38

关于mybatis里的<if></if>标签和<where></where>


<select id = "test01" resultType="DemoUser">
        select * from demo_user where
    <if test="name != null">name = #{name}</if>
    <if test="name !=null and age !=null">and </if>
    <if test="age != null">age=#{age}</if>
        <if test="(name !=null or age !=null) or sex != nell">and </if>
    <if test="sex != null">sex=#{sex}</if>
    </select>

我发现if标签不用放在where标签里也可以解决and多出的问题,这样写可以吗?

  • 写回答

3条回答 默认 最新

  • CSDN专家-sinJack 2021-10-11 23:04
    关注

    可以啊,动态标签都可以单独使用。
    不过你这种写法最好加一个恒成立表达式,否则if都不满足的情况下会报错,还有少了连接and。
    完整代码如下:

    <select id = "test01" resultType="DemoUser">
            select * from demo_user where 1=1
        <if test="name != null and name!=''">and name = #{name}</if>
        <if test="age!= null">and age=#{age}</if>
        <if test="sex!= null and sex!=''">and sex=#{sex}</if>
        </select>
    
    评论

报告相同问题?

问题事件

  • 创建了问题 10月11日

悬赏问题

  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度