mybatis智能标签1

    
if+where   
复制代码
复制代码
 <select id="getOne" resultType="com.mybatis.entity.SmbmsProviderEntity">
        SELECT * FROM smbms_provider
        <where>
            <if test="proCode!=null and proCode!=''">
                and proCode LIKE CONCAT('%',#{proCode},'%')
            </if>
            <if test="proName!=null and proName!=''">
                and  proName LIKE CONCAT('%',#{proName},'%')
            </if>
        </where>
    </select>
复制代码
复制代码

if+set 语句

      

复制代码
复制代码
<update id="update">

        update User

          <set>

            <if test="userCode!=null and userCode!=''">

              userCode=#{userCode},

            </if>       

           <if test="userName!=null and userName!=''">

              userName=#{userName}

             </if>

          </set>

          where id=#{id}

       </update>
复制代码
复制代码

if+trim 语句

     

复制代码
复制代码
 <update id="update">

        update User

          <trim prefix="SET" suffixOverrides=",">

            <if test="userCode!=null and userCode!=''">

              userCode=#{userCode},

            </if>       

            <if test="userName!=null and userName!=''">

              userName=#{userName}

             </if>

          </trim>

          where id=#{id}

       </update>
复制代码
复制代码

    prefix:前缀,作用是通过自动识别是否有返回值后,在trim包含的内容上加上前缀,如此出的where;

    suffix:后缀,作用是在trim包含的内容上加上后缀;

    prefixOverrides:对于trim包含内容的首部进行指定内容忽略;

    suffixOverrides:对于trim包含内容的首尾部进行指定内容的忽略;

mybatis入参为数组类型的 foreach 迭代  

    Dao层:

      

     小配置文件:

     
复制代码
 <select id="getTwo" resultType="com.mybatis.entity.SmbmsProviderEntity">
          select * from smbms_provider where proCode in
            <foreach collection="array" item="pr" open="(" close=")" separator="," >
                #{pr}
            </foreach>
      </select>
复制代码

    测试类:

       

     

mybatis入参为List类型的 foreach 迭代  

   

        
复制代码
<select id="getThree" resultType="com.mybatis.entity.SmbmsProviderEntity">
            select * from smbms_provider where proCode in
              <foreach collection="list" item="pr" open="(" close=")" separator="," >
                  #{pr}
              </foreach>
        </select>
复制代码

      测试类:

        

     注意:foreach元素非常强大,允许我们指定一个集合,并指定开始和结束的字符,也可以加入一个分隔符到迭代器中,并能够智能处理该分隔符,不会出现多余的分隔符;

mybatis入参为Map类型的 foreach 迭代

     Dao层:

        

      小配置文件:

      
复制代码
<select id="getMap" resultType="com.mybatis.entity.SmbmsProviderEntity">
          select * from smbms_provider where proCode in
            <foreach collection="maps" item="map" open="(" close=")" separator="," >
                #{map}
            </foreach>
        </select>
复制代码

    测试类:

       

原文地址:https://www.cnblogs.com/Chencheno/p/11658369.html