mapping.mapper.xml文件中的標簽詳解


mapper.xml中常用的標簽詳解

一、SQL語句標簽:

  1. <!--查詢語句-->  
  2. <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >  
  3. select   
  4. </select>  
  5. <!--插入語句-->  
  6. <insert id="insert" parameterType="pojo.OrderTable" >  
  7. insert into ordertable (order_id, cid, address,   
  8. create_date, orderitem_id)  
  9. values (#{orderId,jdbcType=VARCHAR}, #{cid,jdbcType=VARCHAR}, #{address,jdbcType=VARCHAR},   
  10. #{createDate,jdbcType=TIMESTAMP}, #{orderitemId,jdbcType=VARCHAR})  
  11. </insert>  
  12. <!--刪除語句-->  
  13. <delete id="deleteByPrimaryKey" parameterType="java.lang.String" >  
  14. delete from ordertable  
  15. where order_id = #{orderId,jdbcType=VARCHAR}  
  16. </delete>  
  17. <!--修改語句-->  
  18. <update id="updateByPrimaryKey" parameterType="pojo.OrderTable" >  
  19. update ordertable  
  20. set cid = #{cid,jdbcType=VARCHAR},  
  21. address = #{address,jdbcType=VARCHAR},  
  22. create_date = #{createDate,jdbcType=TIMESTAMP},  
  23. orderitem_id = #{orderitemId,jdbcType=VARCHAR}  
  24. where order_id = #{orderId,jdbcType=VARCHAR}  
  25. </update>  

需要配置的屬性:id="xxxx" >>> 表示此段sql執行語句的唯一標識,也是接口的方法名稱【必須一致才能找到】

parameterType="" >>>表示該sql語句中需要傳入的參數, 類型要與對應的接口方法的類型一致【可選】

resultMap=“ ”>>> 定義出參,調用已定義的<resultMap>映射管理器的id值

resultType=“ ”>>>定義出參,匹配普通java類型或自定義的pojo【出參類型若不指定,將為語句類型默認類型,如<insert>語句返回值為int】

p.s: 至於為何<insert><delete><update> 語句的返回值類型為什么是int,有過JDBC操作經驗的朋友可能會有印象,增刪改操作實際上返回的是操作的條數。而Mybatis框架本身是基於JDBC的,所以此處也沿襲這種返回值類型。

傳參和取值:mapper.xml 的靈活性還體現在SQL執行語句可以傳參,參數類型通過parameterType= “” 定義

取值方式1:#{value jdbcType = valuetype}:jdbcType 表示該屬性的數據類型在數據庫中對應的類型,如 #{user jdbcType=varchar} 等價於 String username;

取值方式2:${value } : 這種方式不建議大量使用,可能會發送sql注入而導致安全性問題。一般該取值方式可用在非經常變化的值上,如orderby ${columnName};

二、sql片段標簽<sql>

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

     需要配置的屬性:id="" >>>表示需要改sql語句片段的唯一標識

     引用:通過<include refid="" />標簽引用,refid="" 中的值指向需要引用的<sql>中的id=“”屬

  1. <!--定義sql片段-->  
  2. <sql id="orderAndItem">  
  3. o.order_id,o.cid,o.address,o.create_date,o.orderitem_id,i.orderitem_id,i.product_id,i.count  
  4. </sql>  
  5. <select id="findOrderAndItemsByOid" parameterType="java.lang.String" resultMap="BaseResultMap">  
  6. select  
  7. <!--引用sql片段-->  
  8. <include refid="orderAndItem" />  
  9. from ordertable o  
  10. join orderitem i on o.orderitem_id = i.orderitem_id  
  11. where o.order_id = #{orderId}  
  12. </select> 

三、映射管理器resultMap

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

     需要配置的屬性:<resultMap id="  " type="  "></resutlMap>   id=" ">>>表示這個映射管理器的唯一標識,外部通過該值引用; type = " ">>> 表示需要映射的實體類;

     需要配置的參數:<id column = " " property= " " />    <id>標簽指的是:結果集中結果唯一的列【column】 和 實體屬性【property】的映射關系,注意:<id>標簽管理的列未必是主鍵列,需要根據具體需求指定;

    <result column= " " property=" " />  <result>標簽指的是:結果集中普通列【column】 和 實體屬性【property】的映射關系;

            需要維護的關系:所謂關系維護是值在主表查詢時將其關聯子表的結果也查詢出來

   1)一對一關系<assocation property = " " javaType=" ">   property = “ ” 被維護實體在宿主實體中的屬性名,javaType = " " 被維護實體的類型

Orderitem.java

  1. package pojo;  
  2. public class Orderitem {  
  3. private String orderitemId;  
  4. private String productId;  
  5. private Integer count;  
  6. private Product product;     

從上方代碼段可以看出:Product 對象在 Orderitem 實體中以 product 屬性存在 

Orderitemmapper.xml

  1. <resultMap id="BaseResultMap" type="pojo.Orderitem" >  
  2. <id column="orderitem_id" property="orderitemId" jdbcType="VARCHAR" />  
  3. <result column="product_id" property="productId" jdbcType="VARCHAR" />  
  4. <result column="count" property="count" jdbcType="INTEGER" />  
  5. <!-- 通過association 維護 一對一關系 -->  
  6. <association property="product" javaType="pojo.Product">  
  7. <id column="product_id" property="productId"/>  
  8. <result column="product_factroy" property="productFactroy"/>  
  9. <result column="product_store" property="productStore"/>  
  10. <result column="product_descript" property="productDescript"/>  
  11. </association>  
  12. </resultMap>  

通過xml的配置可以看出,在resultMap映射管理器中,通過<association> 進行了維護,也就是在查詢Orderitem對象時,可以把關聯的Product對象的信息也查詢出來

2)一對多關系的維護<collection property=" " ofType=" "> property = “ ” 被維護實體在宿主實體中的屬性名 ,ofType=“ ”是被維護方在宿主類中集合泛型限定類型

【由於在一對多關系中,多的一放是以List形式存在,因此ofType的值取用Lsit<?> 的泛型對象類型】

OrderTable.java

  1. public class OrderTable {  
  2. private String orderId;  
  3. private String cid;  
  4. private String address;  
  5. private Date createDate;  
  6. private String orderitemId;
  7. private List<Orderitem> orderitemList ; 
  8. }

 

OrderTableMapper.xml;  

  1. <resultMap id="BaseResultMap" type="pojo.OrderTable" >  
  2. <!--  
  3. WARNING - @mbggenerated  
  4. This element is automatically generated by MyBatis Generator, do not modify.  
  5. This element was generated on Fri May 06 15:49:42 CST 2016.  
  6. -->  
  7. <id column="order_id" property="orderId" jdbcType="VARCHAR" />  
  8. <result column="cid" property="cid" jdbcType="VARCHAR" />  
  9. <result column="address" property="address" jdbcType="VARCHAR" />  
  10. <result column="create_date" property="createDate" jdbcType="TIMESTAMP" />  
  11. <result column="orderitem_id" property="orderitemId" jdbcType="VARCHAR" />  
  12. <!--維護一對多的關系  -->  
  13. <collection property="orderitemList" ofType="pojo.Orderitem">  
  14. <id column="orderitem_id" property="orderitemId"/>  
  15. <result column="product_id" property="productId"/>  
  16. <result column="count" property="count"/>  
  17. </collection>   
  18. </resultMap>

 

3)在resultMap 中需要注意兩點:

3.1)關聯關系的維護可以根據實體類之間的實際情況進行嵌套維護

3.2)關於出現重復列名的處理:在實際操作過程中,查詢到的結果可能會出現相同的列名,這樣會對映射到實體屬性帶來影響甚至出現報錯,那么對待這個問題可以通過對列取別名的方式處理

 

四:常用的動態語句標簽

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

 <where> : 使用其可以代替sql語句中的where關鍵字,一般防止在條件查詢的最外層

         <if >:條件判斷標簽,配置屬性test=" 條件字符串 ",判斷是否滿足條件,滿足則執行,不滿足則跳過

  1. <select id="findOrderItemDetail" parameterType="pojo.Orderitem" resultMap="BaseResultMap">  
  2. select orderitem.orderitem_id,product.*   
  3. from orderitem,product  
  4. <where>  
  5. <if test="orderitemId!=null and orderitemId!=''">  
  6. and orderitem.orderitem_id = #{orderitemId}  
  7. </if>  
  8. <if test="productId!=null and productId!=''">  
  9. and orderitem.product_id = #{productId}  
  10. </if>  
  11. <if test="count!=null">  
  12. and orderitem.count = #{count}  
  13. </if>  
  14. </where>  
  15. </select>  

<set>:常用於<update>更新語句中,替代 sql中的“set”關鍵字,特別是在聯合<if>進行判斷是,可以有效方式當某個參數為空或者不合法是錯誤的更新到數據庫中

  1. <update id="updateByPrimaryKeySelective" parameterType="pojo.Orderitem" >  
  2. update orderitem  
  3. <set >  
  4. <if test="productId != null" >  
  5. product_id = #{productId,jdbcType=VARCHAR},  
  6. </if>  
  7. <if test="count != null" >  
  8. count = #{count,jdbcType=INTEGER},  
  9. </if>  
  10. </set>  
  11. where orderitem_id = #{orderitemId,jdbcType=VARCHAR}  
  12. </update>  

<choose><when></when><otherwise></otherwise></choose> 標簽組:也是一個用於條件判斷的標簽組,和<if>的不同之處在於條件從<choose>進入,去匹配<when>中的添加,一旦匹配馬上結束;若到找不到匹配項,將執行<other>中的語句;可以理解為<if>是 && 關系 <choose>是 || 關系

  1. <!-- 查詢學生list,like姓名、或=性別、或=生日、或=班級,使用choose -->       
  2. <select id="getStudentListChooseEntity" parameterType="StudentEntity" resultMap="studentResultMap">       
  3. SELECT * from STUDENT_TBL ST        
  4. <where>       
  5. <choose>       
  6. <when test="studentName!=null and studentName!='' ">       
  7. ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')        
  8. </when>       
  9. <when test="studentSex!= null and studentSex!= '' ">       
  10. AND ST.STUDENT_SEX = #{studentSex}        
  11. </when>       
  12. <when test="studentBirthday!=null">       
  13. AND ST.STUDENT_BIRTHDAY = #{studentBirthday}        
  14. </when>       
  15. <when test="classEntity!=null and classEntity.classID !=null and classEntity.classID!='' ">       
  16. AND ST.CLASS_ID = #{classEntity.classID}        
  17. </when>       
  18. <otherwise>       
  19. </otherwise>       
  20. </choose>       
  21. </where>       
  22. </select>     

<foreach>標簽:該標簽的作用是遍歷集合類型的條件 

  屬性:collection=“array” / collection = “list”  ----->是數組類型,還是集合類型

              item=“ productId ”------> 參數名

      open="(" separator="," close=")"  ------>開始符號,分隔符號,結束符號 

      index=“ ” ---->結束下標位置,不配置該參數時,默認為全部遍歷

  1. <delete id="deleteByPriKeys" parameterType="java.lang.String">  
  2. delete from product where product_Id in  
  3. <foreach collection="list" item="productId" open="(" separator="," close=")">  
  4. #{productId,jdbcType = VARCHAR}  
  5. </foreach>  
  6. </delete>   

 


免責聲明!

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



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