MyBatis Mapper.XML 標簽使用說明


 直接將值返回給對象

<select id="list" resultType="com.vipsoft.base.entity.UserInfo">
   SELECT Id,Title FROM User
</select>

 

如果字段和屬性名不一致時,通過 resultMap 做映射

<resultMap id="StudentResult" type="com.mybatis3.domain.Student">
  <id column="stud_id" property="studId"  jdbcType="VARCHAR"/>
  <result column="user_name" property="name"  jdbcType="VARCHAR"/>
  <result column="email"  property="email"  jdbcType="VARCHAR" />
  <result column="phone"  property="phone"  jdbcType="VARCHAR" />
  <result column="status" property="status" jdbcType="INTEGER" />
  <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
</resultMap>

<select id="findAllStudents" resultMap="StudentResult" >
  SELECT * FROM STUDENTS
</select>

 

實體引用另一個實體的查詢結果返回

<resultMap type="Course" id="CourseResult">
  <id column="course_id" property="courseId"/>
  <result column="name" property="name"/>
  <result column="description" property="description"/>
  <result column="start_date" property="startDate"/>
  <result column="end_date" property="endDate"/>
</resultMap>
  
<resultMap type="Tutor" id="TutorResult">
  <id column="tutor_id" property="tutorId"/>
  <result column="tutor_name" property="name"/>
  <result column="email" property="email"/>
  <collection property="courses" resultMap="CourseResult"/>
</resultMap>
  
<select id="findTutorById" parameterType="int" resultMap="TutorResult">
  SELECT T.TUTOR_ID, T.NAME AS TUTOR_NAME, EMAIL, C.COURSE_ID, C.NAME, DESCRIPTION, START_DATE, END_DATE
  FROM TUTORS T LEFT OUTER JOIN ADDRESSES A ON T.ADDR_ID=A.ADDR_ID
  LEFT OUTER JOIN COURSES C ON T.TUTOR_ID=C.TUTOR_ID
  WHERE T.TUTOR_ID=#{tutorId}
</select>

choose 該方式適用於多個條件中選擇一個滿足條件的來生成sql

<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 TUTOR start_date  &gt;= now()
    </otherwise>
  </choose>
</select>

where 適用於從多個條件中選擇所有滿足條件的來構成condtions

<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  &gt;= #{startDate}
    </if>
    <if test="endDate != null">
      AND end_date  &lt;= #{endDate}
    </if>
  </where>
</select>

 

foreach

<select id="selectUserByListId" parameterType="com.ys.vo.UserVo" resultType="com.ys.po.User">
select * from user
  <where>
    <!--
    collection:指定輸入對象中的集合屬性.該屬性的值有三種:list,array,map,根據傳入的集合類型而設定該值。
    item:每次遍歷生成的對象
    index:當前迭代的次數
    open:開始遍歷時的拼接字符串
    close:結束時拼接的字符串
    separator:遍歷對象之間需要拼接的字符串
    select * from user where 1=1 and id in (1,2,3)
    -->
    <foreach collection="list" item="id" open="(" separator="," close=")">
     #{id}
    </foreach>
  </where>
</select>

select * from user where id=1 or id=2 or id=3

<select id="selectUserByListId" parameterType="com.ys.vo.UserVo" resultType="com.ys.po.User">
select * from user
  <where>
    <foreach collection="list" item="id" open="and (" close=")" separator="or">
       id=#{id}
    </foreach>
  </where>
</select>

 

< sql >標簽
該標簽主要定義復用的sql語句片段,在執行的sql語句標簽直接引用即可。可以提高編碼效率、簡化代碼和提高可讀性。

需要配置id熟悉,表示該sql片段的唯一標識。

引用:通過<include refid=" " / >標簽引用,refid的值就是< sql>的id屬性的值。

<sql id="Base_Column_List">
  id, question, answer 
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
  select 
  <include refid="Base_Column_List" />
  from java
  where id = #{id,jdbcType=BIGINT}
</select> 

<set> : 主要用來替換sql語句中的set字段,一般在update中使用。

<update>
  update user 
  <set>
    <if test="name != null and name.length()>0">name = #{name},</if>
    <if test="age != null and age .length()>0">age = #{age },</if>
  </set>
  where id = #{id}
</update>  

批量更新,一次執行多條SQL

<update>
    <foreach collection="list" item="item" index="index" separator=";">
        update user 
        <set>
            <if test="name != null and name.length()>0">name = #{name},</if>
            <if test="age != null and age .length()>0">age = #{age },</if>
        </set>
        <where>
            <if test="id != null and id != ''">
                AND id = #{id,jdbcType=VARCHAR}
            </if>
            <if test="userName != null and userName != ''">
                <bind name="bindUserName" value="'%' + userName + '%'" />
                AND UserName like #{bindUserName}
            </if> 
            <if test="status != null">
                AND Status = #{status,jdbcType=INTEGER}
            </if>
        </where>
    </foreach>
</update>  

 

<trim> : 是一個格式化的標記,可以完成set或者是where標記的功能。

select * from user 
<trim prefix="WHERE" prefixoverride="AND |OR">
  <if test="name != null and name.length()>0"> AND name=#{name}</if>
  <if test="age != null and age.length()>0"> AND age=#{age}</if>
</trim> 


假如說name和age的值都不為null的話打印的SQL為:select * from user where name = ‘xx’ and age = ‘xx’
在where的后面是不存在第一個and的,上面兩個屬性的意思如下:
  prefix:前綴      
  prefixoverride:去掉第一個and或者是or 
 
update user
<trim prefix="set" suffixoverride="," suffix=" where id = #{id} ">
  <if test="name != null and name.length()>0"> name=#{name} , </if>
  <if test="age!= null and age.length()>0"> age=#{age} ,  </if>
</trim>
假如說name和age的值都不為null的話打印的SQL為:update user set name=‘xx’ , age=‘xx’ where id=‘x’

在age='xx’的后面不存在逗號,而且自動加了一個set前綴和where后綴,上面三個屬性的意義如下,其中prefix意義如上:
   suffixoverride:去掉最后一個逗號(也可以是其他的標記,就像是上面前綴中的and一樣)
   suffix:后綴 

 choose & foreach list 有一個值,條件=,有多個值 in 

<update id="updateStatus" parameterType="java.util.ArrayList" >
  update User
  set Status = 1,UpdateTime=(select GETDATE())
  <choose>
    <when test="idList != null and idList.size==1">
      WHERE Id= #{id,jdbcType=VARCHAR}
    </when>
    <when test="idList != null and idList.size>1">
      WHERE Id in
      <foreach collection="idList" item="id" open="(" close=")" separator=",">
        <if test="id != null and id != '' ">
          #{id,jdbcType=VARCHAR}
        </if>
      </foreach>
    </when>
    <otherwise>
      WHERE 1=0
    </otherwise>
  </choose>
</update>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM