zoukankan      html  css  js  c++  java
  • 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>
    复制代码

        测试类:

           

  • 相关阅读:
    基于jQuery解决ios10以上版本缩放问题
    移动端h5模拟长按事件
    一篇讲SpringBoot+kafka很好的文章
    Liquibase+SpringBoot的简单使用笔记!update+rollback
    集合异同,找出新增元素和删除元素
    spring-security-结合JWT的简单demo
    IDEA SpringBoot+JPA+MySql+Redis+RabbitMQ 秒杀系统
    提取swagger内容到csv表格,excel可打开
    spring mvc 黑马 笔记
    手机页面图片显示高低不一致
  • 原文地址:https://www.cnblogs.com/Chencheno/p/11658369.html
Copyright © 2011-2022 走看看