zoukankan      html  css  js  c++  java
  • mybatis中的where

    在多个查询条件下,由于需要拼接sql语句,所以会在前面加上 where 1 = 1

       select id,name,gender,email from emp
            where 1 = 1
                <if test="id != null and id != ''">
                     and id =  #{id}
                </if>
               <if test="name != null and name != ''">
                    and name = #{name}
               </if>
        </select>

     可以使用<where></where>代替:

     select id,name,gender,email from emp
           <where>
               <if test="id != null and id != ''">
                   and id = #{id}
               </if>
               <if test="name != null and name != ''">
                   and name = #{name}
               </if>
           </where>

    还可以使用<trim></trim>代替:

    trim标签:

    1》prefix="":前缀:trim标签体中是整个字符串拼串 后的结果,prefix给拼串后的整个字符串加一个前缀
    2》prefixOverrides="":前缀覆盖: 去掉整个字符串前面多余的字符
    3》suffix="":后缀,suffix给拼串后的整个字符串加一个后缀
    4》suffixOverrides=""后缀覆盖:去掉整个字符串后面多余的字符

      select id,name,gender,email from emp
           <trim prefix="where" prefixOverrides="and">
               <if test="id != null and id != ''">
                   and id =  #{id}
               </if>
               <if test="name != null and name != ''">
                   and name = #{name}
               </if>
           </trim>
  • 相关阅读:
    第12组(78) 团队展示(组长)
    第一次结对编程作业
    Alpha冲刺总结
    第02组Alpha冲刺(6/6)
    第02组Alpha冲刺(5/6)
    第02组Alpha冲刺(4/6)
    第02组Alpha冲刺(3/6)
    第02组Alpha冲刺(2/6)
    第02组Alpha冲刺(1/6)
    第02组(51)需求分析报告
  • 原文地址:https://www.cnblogs.com/tdyang/p/12745006.html
Copyright © 2011-2022 走看看