记录一下mybatis的简单动态查询用法

记录一下mybatis的简单动态查询用法

  <select id="findByCondition" resultMap="BaseResultMap" parameterType="hashmap" >
          select *
        from equipmentcheckresult
        <trim  prefix="where" prefixOverrides="and|or">
            <if test="toolId!=null and toolId!='' ">  
                AND tool_id=#{toolId}  
            </if> 
             <if test="beginTime != null and beginTime != ''">AND check_time <![CDATA[>=]]> #{beginTime}</if>
             <if test="endTime != null and endTime != ''">AND check_time <![CDATA[<=]]>#{endTime}</if>
             <if test="elementId != null">
                 AND element_id IN
                <foreach collection="elementId" index="index" item="item"
                    open="(" separator="," close=")">
                    #{item}
                </foreach>
             </if>
             <if test="sort != null and sort != '' ">order by id ${sort}</if> 
        </trim>
  </select>

1.hashmap 为mybatis内置对象能够直接使用sql

2.属性“prefix”表示:加入前缀where数组

3.属性“prefixOverrides”表示:自动覆盖第一个“and”或者“or”mybatis

4.<![CDATA[   ]]> 是什么,这是XML语法。在CDATA内部的全部内容都会被解析器忽略。ide

在使用mybatis 时咱们sql是写在xml 映射文件中,若是写的sql中有一些特殊的字符的话,在解析xml文件的时候会被转义,但咱们不但愿他被转义,因此咱们要使用<![CDATA[ ]]>来解决。ui

若是文本包含了不少的"<"字符 <=和"&"字符——就象程序代码同样,那么最好把他们都放到CDATA部件中。spa

5.elementId为集合或者数组经过foreach进行遍历 index为下表,item为遍历元素 (数组用length计算长度,集合类型用size())code

6.order by 的时候 排序参数 不可用#{} 而是要用 ${}xml

相关文章
相关标签/搜索