mybatis動態sql

mybatis中靜態sql語句有時不足以知足用戶的需求,所以其提供了動態sql標籤。java

IF標籤

if標籤經過條件測試,動態插入sql片斷,例如:sql

<select id="searchCourses" parameterType="hashmap" resultMap="CourseResult">apache

<![CDATA[
select * from courses where tutor_id=#{tutorId}
<if test="courseName!=null">
    and name like #{courseName}
</if>
<if test="startDate!=null">
    and start_date>=#{startDate}
</if>
<if test="endDate!=null">
    and end_start<=#{endDate}
</if>
]]>

</select>數組

<![CDATE[ ]]>保證之間的內容做爲通常的字符處理,不作特殊處理。mybatis

choose標籤

choose標籤用於選擇第一個選擇條件,例如:
<select id="searchCourses" parameterType="hashmap" resultMap="CourseResult">
    select * from courses
    <choose>
        <when test="searchBy=='Tutor'">
            where tutor_id=#{tutorId}
        </when>
        <when test="searchBy=='CourseName'">
            where name like #{CourseName}
        </when>
        <otherwise>
         where start_date=now();
        </otherwise>
    </choose>
</select>

where標籤

有時,全部的查詢條件可能都是可選的,可是其中至少有一個查詢是須要的,可是如有多個查詢條件都知足條件,這時就須要在查詢條件添加and 或or。mybatis提供了where標籤用於支持創建這種類型的sql語句,當第一個知足的條件前面有and 或 or 等鏈接詞時,會自動刪除鏈接詞。例子以下所示:
<select id="searchCourses" parameterType="hashmap" resultMap="CourseResult">
  select * from courses
  <where>
   <if test="tutorId!=null">
        tutor_id=#{tutorId}
   </if>
   <if test="courseName!=null">
       and name like #{courseName}
   </if>
   <if test="startDate!=null">
       and start_date=#{startDate}
   </if>
   <if test="endDate !=null">
      and end_date=#{endDate}
   </if>
  </where>
</select>

trim標籤

trim標籤比where標籤更加靈活,由於它能夠在條件前面加上鍊接詞或刪除鏈接詞 也能夠在條件後面加上鍊接詞或刪除鏈接詞;舉例以下:
<select id="searchCourses" parameterType="hashmap" resultMap="CourseResult">
  select * from courses
  <trim prefix="where" prefixOverrides="and | or" >
   <if test="tutorId!=null">
        tutor_id=#{tutorId}
   </if>
   <if test="courseName!=null">
       and name like #{courseName}
   </if>
   <if test="startDate!=null">
       and start_date=#{startDate}
   </if>
   <if test="endDate !=null">
      and end_date=#{endDate}
   </if>
  </trim>
</select>

foreach標籤

foreach元素的屬性主要有 item,index,collection,open,separator,close。
item表示集合中每個元素進行迭代時的別名.
index指 定一個名字,用於表示在迭代過程當中,每次迭代到的位置.
open表示該語句以什麼開始.
separator表示在每次進行迭代之間以什麼符號做爲分隔 符.
close表示以什麼結束.
舉例以下:
<select id="searchCourse" parameterType="hashMap" resultMap="Course">
    select * from courses
    <if test="tutorIds!=null">
        <where>
            <foreach item="tutorId" collection="tutorIds" >
                or tutor_id=#{tutorId}
            </foreah>
        </where>
    </if>
</select>

<select id="searchCourse" parameterType="hashMap" resultMap="Course">
    select * from courses
    <if test="tutorIds!=null">
        <where>
             tutor_id in
            <foreach item="tutorId" collection="tutorIds" open="(" separator="," close=")">
                #{tutorId}
            </foreah >
        </where>
    </if>
</select>

set標籤

set標籤相似於where標籤,會在返回的條件前面插入set關鍵字,並移除最後一個條件的後面符號,舉例以下所示:
<update id="updateStudent" parameterType="Student">
  update students 
  <set>
      <if test="name!=null"> name=#{name},</if>
      <if test="email!=null">email=#{email},</if>
      <if test="phone!=null">phone=#{phone},</if>
  </set>
</update>

若是三個條件都是ture,phone後面的逗號將被移除。

枚舉類型

mybatis提供了提供了持久化的枚舉類型。假設 Student表結構的gender列,使用varchar類型存儲MALE或FEMALE,Student對象使用枚舉類型標識gender.
public enum Gender{
    FEMALE,MALE
}

public class Student
{
    ....
    private Gender gender;
    ....
}
<insert id="insert" parameterType="Student">
    insert into student (id,name,gender)
    values(#{id},#{name},#{gender})
</insert>
當執行insert語句時,MALE或FEMALE會存儲到gender列,若是想要存儲的時枚舉值而不是枚舉名字,就須要配置類型處理器:
<typeHander handler="org.apache.ibatis.type.EnumOrdinalTypeHandler" javaType="com.mybatis3.domain.Gender">

CLOB和BLOB

mybatis提供了對clob和blob的內檢支持,將clob映射爲java.lang.String,將blob映射爲byte[]數組。所以用戶能夠像原始類型那樣處理clob和blob類型。

傳遞多個參數

mybatis提供了傳遞多個參數的內建功能,並經過#{param}語法引用參數。舉例以下:

List<Student> findAllStudentByNameEmail(String name,String email);

<select id="findAllStudentByNameEmail" resultMap="Student">
    select * from students where name=#{param1} and email=#{param2}
</select>

將多行數據存儲到map中,並以某個列值做爲key

要完成上述功能,須要使用sqlSession的selectMap方法;
舉例以下:

<select id="findAllStudent" resultMap="Student">
    select * from students 
</select>

Map<Integer,Student> studentMap=sqlSession.selectMap("com.mybatis3.mappers.StudentMapper.findAllStudent","studId");
相關文章
相關標籤/搜索