java:框架:mybatis:mapper.xml中常用的標籤詳解

1.SQL語句標籤

<!--查詢語句-->  
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >  
    select   
  </select>  

<!--插入語句-->  
<insert id="insert" parameterType="pojo.OrderTable" >  
 insert into ordertable (order_id, cid, address,   
      create_date, orderitem_id)  
    values (#{orderId,jdbcType=VARCHAR}, #{cid,jdbcType=VARCHAR}, #{address,jdbcType=VARCHAR},   
      #{createDate,jdbcType=TIMESTAMP}, #{orderitemId,jdbcType=VARCHAR})  
  </insert>  

<!--刪除語句-->  
<delete id="deleteByPrimaryKey" parameterType="java.lang.String" >  
delete from ordertable  
    where order_id = #{orderId,jdbcType=VARCHAR}  
</delete>  

<!--修改語句-->  
 <update id="updateByPrimaryKey" parameterType="pojo.OrderTable" >  
  update ordertable  
    set cid = #{cid,jdbcType=VARCHAR},  
      address = #{address,jdbcType=VARCHAR},  
      create_date = #{createDate,jdbcType=TIMESTAMP},  
      orderitem_id = #{orderitemId,jdbcType=VARCHAR}  
    where order_id = #{orderId,jdbcType=VARCHAR}  
  </update>  

需要配置的屬性:id=”xxxx” >>> 表示此段sql執行語句的唯一標識,也是接口的方法名稱【必須一致才能找到】 
parameterType=”” >>>表示該sql語句中需要傳入的參數, 類型要與對應的接口方法的類型一致【可選】 
resultMap=“ ”>>> 定義出參,調用已定義的映射管理器的id值 
resultType=“ ”>>>定義出參,匹配普通java類型或自定義的pojo【出參類型若不指定,將爲語句類型默認類型,如語句返回值爲int】 
傳參和取值:mapper.xml 的靈活性還體現在SQL執行語句可以傳參,參數類型通過parameterType= “” 定義

2.SQL片段標籤

通過該標籤可定義能複用的sql語句片段,在執行sql語句標籤中直接引用即可。這樣既可以提高編碼效率,還能有效簡化代碼,提高可讀性

<!--定義sql片段-->  
<sql id="orderAndItem">  
    o.order_id,o.cid,o.address,o.create_date,o.orderitem_id,i.orderitem_id,i.product_id,i.count  
  </sql>  

 <select id="findOrderAndItemsByOid" parameterType="java.lang.String" resultMap="BaseResultMap">  
    select  
<!--引用sql片段-->  
    <include refid="orderAndItem" />  
    from ordertable o  
    join orderitem i on o.orderitem_id = i.orderitem_id  
    where o.order_id = #{orderId}  
  </select>  

需要配置的屬性:id=”” >>>表示需要改sql語句片段的唯一標識 
引用:通過標籤引用,refid=”” 中的值指向需要引用的sql 標籤中的id=”“屬性

3.映射管理器resultMap

映射管理器,是Mybatis中最強大的工具,使用其可以進行實體類之間的關係,並管理結果和實體類間的映射關係

需要配置的屬性:<resultMap id="" type="  "></resutlMap>   id=" " >>>表示這個映射管理器的唯一標識,外部通過該值引用; type = "">>> 表示需要映射的實體類;
需要配置的參數:<id column = " " property= " " />    <id>標籤指的是:結果集中結果唯一的列【column】 和 實體屬性【property】的映射關係,注意:<id>標籤管理的列未必是主鍵列,需要根據具體需求指定;
<result column= " " property=" " />  <result>標籤指的是:結果集中普通【column】 和 實體屬性【property】的映射關係;
需要維護的關係:所謂關係維護是值在主表查詢時將其關聯子表的結果也查詢出來
  •  
  • 一對一關係

    <assocation property = " " javaType=" ">   property = “ ” 被維護實體在宿主實體中的屬性名,javaType = " " 被維護實體的類型
    
<resultMap id="BaseResultMap" type="pojo.Orderitem" >  
   <id column="orderitem_id" property="orderitemId" jdbcType="VARCHAR" />  
   <result column="product_id" property="productId" jdbcType="VARCHAR" />  
   <result column="count" property="count" jdbcType="INTEGER" />  
   <!-- 通過association 維護 一對一關係 -->  
   <association property="product" javaType="pojo.Product">  
    <id column="product_id" property="productId"/>  
    <result column="product_factroy" property="productFactroy"/>  
    <result column="product_store" property="productStore"/>  
    <result column="product_descript" property="productDescript"/>  
   </association>  
 </resultMap>  
  • 一對多關係

        <collection property=" " ofType=" "> property = “ ” 被維護實體在宿主實體中的屬性名 ,ofType=“ ”是被維護方在宿主類中集合泛型限定類型
    
public class OrderTable {  

    private String orderId;  

    private String cid;  

    private String address;  

    private Date createDate;  

    private String orderitemId;  

    private List<Orderitem> orderitemList;
<resultMap id="BaseResultMap" type="pojo.OrderTable" >  
   <!--  
     WARNING - @mbggenerated  
     This element is automatically generated by MyBatis Generator, do not modify.  
     This element was generated on Fri May 06 15:49:42 CST 2016.  
   -->  
   <id column="order_id" property="orderId" jdbcType="VARCHAR" />  
   <result column="cid" property="cid" jdbcType="VARCHAR" />  
   <result column="address" property="address" jdbcType="VARCHAR" />  
   <result column="create_date" property="createDate" jdbcType="TIMESTAMP" />  
   <result column="orderitem_id" property="orderitemId" jdbcType="VARCHAR" />  
     <!--維護一對多的關係  -->  
    <collection property="orderitemList" ofType="pojo.Orderitem">  
        <id column="orderitem_id" property="orderitemId"/>  
        <result column="product_id" property="productId"/>  
        <result column="count" property="count"/>  
    </collection>   
 </resultMap>  

4.動態語句標籤

通過動態sql標籤可以進行條件判斷,條件遍歷等操作從而滿足結果的需要.

<where> : 使用其可以代替sql語句中的where關鍵字,一般防止在條件查詢的最外層
<if >:條件判斷標籤,配置屬性test=" 條件字符串 ",判斷是否滿足條件,滿足則執行,不滿足則跳過
<select id="findOrderItemDetail" parameterType="pojo.Orderitem" resultMap="BaseResultMap">  
        select orderitem.orderitem_id,product.*   
        from orderitem,product  
        <where>  
            <if test="orderitemId!=null and orderitemId!=''">  
                and orderitem.orderitem_id = #{orderitemId}  
            </if>  
            <if test="productId!=null and productId!=''">  
                and orderitem.product_id = #{productId}  
            </if>  
            <if test="count!=null">  
                and orderitem.count = #{count}  
            </if>  
        </where>  
  </select>  
<set>:常用於<update>更新語句中,替代 sql中的“set”關鍵字,特別是在聯合<if>進行判斷是,可以有效方式當某個參數爲空或者不合法是錯誤的更新到數據庫中
<update id="updateByPrimaryKeySelective" parameterType="pojo.Orderitem" >  
   update orderitem  
   <set >  
     <if test="productId != null" >  
       product_id = #{productId,jdbcType=VARCHAR},  
     </if>  
     <if test="count != null" >  
       count = #{count,jdbcType=INTEGER},  
     </if>  
   </set>  
   where orderitem_id = #{orderitemId,jdbcType=VARCHAR}  
 </update>
<choose><when></when><otherwise></otherwise></choose> 標籤組:也是一個用於條件判斷的標籤組,和<if>的不同之處在於條件從<choose>進入,去匹配<when>中的添加,一旦匹配馬上結束;若到找不到匹配項,將執行<other>中的語句;可以理解爲<if>是 && 關係 <choose>是 || 關係
<!-- 查詢學生list,like姓名、或=性別、或=生日、或=班級,使用choose -->       
<select id="getStudentListChooseEntity" parameterType="StudentEntity" resultMap="studentResultMap">       
    SELECT * from STUDENT_TBL ST        
    <where>       
        <choose>       
            <when test="studentName!=null and studentName!='' ">       
                    ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')        
            </when>       
            <when test="studentSex!= null and studentSex!= '' ">       
                    AND ST.STUDENT_SEX = #{studentSex}        
            </when>       
            <when test="studentBirthday!=null">       
                AND ST.STUDENT_BIRTHDAY = #{studentBirthday}        
            </when>       
            <when test="classEntity!=null and classEntity.classID !=null and classEntity.classID!='' ">       
                AND ST.CLASS_ID = #{classEntity.classID}        
            </when>       
            <otherwise>       

            </otherwise>       
        </choose>       
    </where>       
</select>
  <foreach>標籤:該標籤的作用是遍歷集合類型的條件 
  屬性:collection=“array” / collection = “list”  ----->是數組類型,還是集合類型
  item=“ productId ”------> 參數名
  open="(" separator="," close=")"  ------>開始符號,分隔符號,結束符號 
  index=“ ” ---->結束下標位置,不配置該參數時,默認爲全部遍歷
<delete id="deleteByPriKeys" parameterType="java.lang.String">  
     delete from product where product_Id in  
     <foreach collection="list" item="productId" open="(" separator="," close=")">  
         #{productId,jdbcType = VARCHAR}  
     </foreach>  
 </delete>   

 

發佈了268 篇原創文章 · 獲贊 126 · 訪問量 54萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章