mybatis中mapper.xml文件的常用屬性及標簽講解

${}和#{}的區別

#{}會自動在你要插入字段兩端 加上引號。例如:你寫的是order by #{username},傳的是 zhangsan,那麼會解析成order by “zhangsan”。

${}是將傳入的數據直接顯示生成在sql中。如:order by ${user_id},如果傳入的值是111,那麼解析成sql時的值為order by 111 如果傳入的值是id,則解析成的sql為order by id.

#{}: 解析為一個 JDBC 預編譯語句(prepared statement)的參數標記符,一個 #{ } 被解析為一個參數占位符 。

$ {}: 僅僅為一個純碎的 string 替換,在動態 SQL 解析階段將會進行變量替換。在使用order by 時,就需要使用$;

常見的屬性

屬性 作用
namespace 對應接口的路徑
id 表示此段sql執行語句的唯一標識,也是接口的方法名稱【必須一致才能找到方法】
parameterType 表示該sql語句中需要傳入的參數, 類型要與對應的接口方法的類型一致【可選】
resultMap 定義出參,調用已定義的映射管理器的id值
resultType 定義出參,匹配普通Java類型或自定義的pojo【出參類型若不指定,將為語句類型默認類型,如語句返回值為int】

常見標簽

< 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>

< where >和< if >標簽

< where > : 主要用來替換sql語句中的where字段,他的作用主要是用來簡化sql語句中where條件判斷的書寫的

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

 <select id="selectByParams" parameterType="map" resultType="user">
    select * from user
    <where>
      <if test="id != null ">id=#{id}</if>
      <if test="name != null and name.length()>0" >and name=#{name}</if>
      <if test="age != null and age.length()>0">and age = #{age}</if>
    </where>
  </select> 

  

如果當id值為空時,此時打印的sql應是:select * from user where name=“xx” and age=“xx”

where 標記會自動將其後第一個條件的and或者是or給忽略掉

< set >標簽

< 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 user set name=‘xxx' , age=‘xx' where id=‘x'

在上面age=”xx”的後是沒有逗號的,也就是說set標記已經自動幫助我們把最後一個逗號給去掉瞭

set 標記會自動將其後第一個條件後的逗號忽略掉

< trim>標簽

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

示例1:

   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

示例2:

  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 >標簽

< where > : choose標簽是按順序判斷其內部when標簽中的test條件出否成立,如果有一個成立,則 choose 結束。當 choose 中所有 when 的條件都不滿則時,則執行 otherwise 中的sql。類似於Java 的 switch 語句,choose 為 switch,when 為 case,otherwise 則為 default。

 <select id="selectByParams" parameterType="map" resultType="user">
    select * from user where 1 = 1
        <choose>  
             <when test="id !=null ">  
                 AND id = #{id}
             </when >  
             <when test="username != null and username != '' ">  
                 AND username = #{username}  
             </when >  
             <when test="age != null and age !=''">  
                 AND age = #{age}  
             </when >  
             <otherwise>  
             </otherwise>  
          </choose>
  </select> 

mybatis 的xml文件中標簽錯誤

mybatis 的xml文件中對應關系,如果包含一對一和一對多,那麼一對一的標簽association必須放在collection前面,resultMap內的標簽的都是有順序的。

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: