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>
    复制代码

        测试类:

           

  • 相关阅读:
    2019-03-18 使用Request POST获取CNABS网站上JSON格式的表格数据,并解析出来用pymssql写到SQL Server中
    2019-03-18 Python time 将2015年11月20日转换为2015-11-20
    2019-03-15 使用Request POST获取中加基金的PDF文件,并下载到本地
    2019-03-15 使用Request POST获取CNABS网站上JSON格式的表格数据,并解析出来用xlwt写到Excel中
    2019-03-15 Python time datetime 获取当下时间 和 格式化时间
    2019-03-14 Python爬虫问题 爬取网页的汉字打印出来乱码
    2019-02-25 SQL:cast(itemvalue as decimal(19,4))
    js设计模式
    css动画库
    React学习笔记
  • 原文地址:https://www.cnblogs.com/Chencheno/p/11658369.html
Copyright © 2011-2022 走看看