记录一下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内置对象可以直接使用

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

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

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

在使用mybatis 时我们sql是写在xml 映射文件中,如果写的sql中有一些特殊的字符的话,在解析xml文件的时候会被转义,但我们不希望他被转义,所以我们要使用<![CDATA[ ]]>来解决。

如果文本包含了很多的"<"字符 <=和"&"字符——就象程序代码一样,那么最好把他们都放到CDATA部件中。

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

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

 

posted @ 2018-11-16 10:55  少年喝了这个java吧  阅读(437)  评论(0编辑  收藏  举报