溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Mybatis輸入輸出映射及動態(tài)SQL Review

發(fā)布時(shí)間:2020-09-29 19:33:03 來源:腳本之家 閱讀:125 作者:鐘艾伶 欄目:編程語言

一、輸入映射

    通過parameterType指定輸入?yún)?shù)的類型,可以是簡單類型、pojo包裝類、HashMap等

1、輸入簡單類型

<select id="findUserById" parameterType="int" resultType="com.mybatis.po.User"> 
    select * from user where id=#{id} 
</select> 

2、輸入pojo包裝類

<select id="findUserById" parameterType="om.mybatis.po.User" resultType="com.mybatis.po.User"> 
    select * from user where username like ‘%{user.username}%' 
</select>

     Pojo類可根據(jù)業(yè)務(wù)需求,創(chuàng)建某單一實(shí)體的擴(kuò)展實(shí)體,User類的擴(kuò)展類-User和訂單實(shí)體的綜合實(shí)體。

3、輸入HashMap類型

<select id="findUserById" parameterType="hashmap" resultType="com.mybatis.po.User"> 
    select * from user where id=#{id} and username like ‘%{username}%' 
</select> 

     參數(shù)id 和username 對應(yīng)hashmap中的key-value

二、輸出映射

1、resultType類型輸出

     使用resultType進(jìn)行輸出映射,只有查詢出來的列名和pojo中的屬性名一致,該列才可以映射成功。適用于單表查詢,級聯(lián)查詢時(shí)使用該類型輸出需要重新創(chuàng)建關(guān)聯(lián)pojo擴(kuò)展類進(jìn)行映射。

     如果查詢出來的列名和pojo中的屬性名全部不一致,就不會創(chuàng)建該pojo對象。只要查詢出來的列名和pojo中的屬性有一個(gè)一致,就會創(chuàng)建pojo對象。映射失敗的查詢字段返回為空。

2、resultMap類型輸出

     如果查詢出來的列名和pojo的屬性名不一致時(shí),可使用resultMap,通過定義一個(gè)resultMap列名和pojo屬性名之間作一個(gè)映射關(guān)系。得以映射輸出結(jié)果。

1)定義resultMap

<!-- 定義resultMap 
將SELECT id id_,username username_ FROM USER 和User類中的屬性作一個(gè)映射關(guān)系   
type:resultMap最終映射的java對象類型,可以使用別名 
id:對resultMap的唯一標(biāo)識 
 --> 
 <resultMap type="user" id="userResultMap"> 
  <!-- id表示查詢結(jié)果集中唯一標(biāo)識  
  column:查詢出來的列名 
  property:type指定的pojo類型中的屬性名 
  最終resultMap對column和property作一個(gè)映射關(guān)系 (對應(yīng)關(guān)系) 
  --> 
  <id column="id_" property="id"/> 
  <!-- result:對普通名映射定義 
  column:查詢出來的列名 
  property:type指定的pojo類型中的屬性名 
  最終resultMap對column和property作一個(gè)映射關(guān)系 (對應(yīng)關(guān)系) 
   --> 
  <result column="username_" property="username"/> 
 </resultMap> 

2)使用resultMap作為statement的輸出映射類型

<!-- 使用resultMap進(jìn)行輸出映射 
resultMap:指定定義的resultMap的id,如果這個(gè)resultMap在其它的mapper文件,前邊需要加namespace 
--> 
<select id="findUserByIdResultMap" parameterType="int" resultMap="userResultMap"> 
  SELECT id id_,username username_ FROM USER WHERE id=#{value} 
</select> 

三、動態(tài)SQL

     Mybatis核心 對sql語句進(jìn)行靈活操作,通過表達(dá)式進(jìn)行判斷,對sql進(jìn)行靈活拼接、組裝。

 1、動態(tài)SQL示例

     首先創(chuàng)建pojo類,提供該pojo對應(yīng)的mapper映射文件,對crud方法分別配置

<update id="updateByExampleSelective" parameterType="map" > 
  update items 
  <set > 
   <if test="record.id != null" > 
    id = #{record.id,jdbcType=INTEGER}, 
   </if> 
   <if test="record.name != null" > 
    name = #{record.name,jdbcType=VARCHAR}, 
   </if> 
   <if test="record.price != null" > 
    price = #{record.price,jdbcType=REAL}, 
   </if> 
   <if test="record.pic != null" > 
    pic = #{record.pic,jdbcType=VARCHAR}, 
   </if> 
   <if test="record.createtime != null" > 
    createtime = #{record.createtime,jdbcType=TIMESTAMP}, 
   </if> 
   <if test="record.detail != null" > 
    detail = #{record.detail,jdbcType=LONGVARCHAR}, 
   </if> 
  </set> 
  <if test="_parameter != null" > 
   <include refid="Update_By_Example_Where_Clause" /> 
  </if> 
 </update> 

2、SQL片段

     將SQL中的判斷條件進(jìn)行封裝,提高復(fù)用

1)定義sql片段

<!-- 定義sql片段 
  id:sql片段的唯 一標(biāo)識 
  最好基于單表來定義sql片段,這樣話這個(gè)sql片段可重用性才高 
  在sql片段中不要包括 where 
   --> 
  <sql id="query_user_where"> 
    <if test="userCustom!=null"> 
      <if test="userCustom.sex!=null and userCustom.sex!=''"> 
        and user.sex = #{userCustom.sex} 
      </if> 
      <if test="userCustom.username!=null and userCustom.username!=''"> 
        and user.username LIKE '%${userCustom.username}%' 
      </if> 
      <if test="ids!=null"> 
      <!-- 使用 foreach遍歷傳入ids 
      collection:指定輸入 對象中集合屬性 
      item:每個(gè)遍歷生成對象中 
      open:開始遍歷時(shí)拼接的串 
      close:結(jié)束遍歷時(shí)拼接的串 
      separator:遍歷的兩個(gè)對象中需要拼接的串 
       --> 
       <!-- 使用實(shí)現(xiàn)下邊的sql拼接: 
       AND (id=1 OR id=10 OR id=16)  
       --> 
      <foreach collection="ids" item="user_id" open="AND (" close=")" separator="or"> 
        <!-- 每個(gè)遍歷需要拼接的串 --> 
        id=#{user_id} 
      </foreach> 
      <!-- 實(shí)現(xiàn) “ and id IN(1,10,16)”拼接 --> 
      <!-- <foreach collection="ids" item="user_id" open="and id IN(" close=")" separator=","> 
        每個(gè)遍歷需要拼接的串 
        #{user_id} 
      </foreach> --> 
      </if> 
    </if> 
  </sql> 

2)引用sql片段

<!-- 用戶信息綜合查詢 
  #{userCustom.sex}:取出pojo包裝對象中性別值 
  ${userCustom.username}:取出pojo包裝對象中用戶名稱 
   --> 
<select id="findUserList" parameterType="cn.itcast.mybatis.po.UserQueryVo" resultType="cn.itcast.mybatis.po.UserCustom"> 
  SELECT * FROM USER 
  <!-- 
  where可以自動去掉條件中的第一個(gè)and 
   --> 
  <where> 
    <!-- 引用sql片段 的id,如果refid指定的id不在本mapper文件中,需要前邊加namespace --> 
    <include refid="query_user_where"></include> 
    <!-- 在這里還要引用其它的sql片段 --> 
  </where> 
</select> 

注:在使用動態(tài)sql時(shí)注意

1、#{}和${}的不同

     #{}表示一個(gè)占位符號,#{}接收輸入?yún)?shù),類型可以是簡單類型,pojo、hashmap。當(dāng)使用#{}接收簡單類型參數(shù)時(shí),#{}中可以寫成value或其它名稱。當(dāng)接收pojo對象值,寫入對象的屬性值,形如對象.屬性.屬性.屬性...的方式獲取。

     ${}${}表示一個(gè)拼接符號,接收輸入?yún)?shù),類型可以是簡單類型,pojo、hashmap。接收簡單類型,${}中只能寫成value。接受pojo對象時(shí),與#{}相同。注意使用$拼接,會引用sql注入,所以不建議使用${}。

2、使用where 標(biāo)簽,第一個(gè)and 條件為空時(shí),自動跳過。所以可以不添加where 1=1 保證sql語法正確。

3、使用sql執(zhí)行insert之后返回主鍵id-selectKey元素的使用&SELECT LAST_INSERT_ID() 函數(shù)的使用

1)id為自增類型

<!-- 添加用戶  
parameterType:指定輸入 參數(shù)類型是pojo(包括 用戶信息) 
#{}中指定pojo的屬性名,接收到pojo對象的屬性值,mybatis通過OGNL獲取對象的屬性值 
--> 
<insert id="insertUser" parameterType="cn.itcast.mybatis.po.User"> 
  <!--  
  將插入數(shù)據(jù)的主鍵返回,返回到user對象中 
  SELECT LAST_INSERT_ID():得到剛insert進(jìn)去記錄的主鍵值,只適用與自增主鍵 
  keyProperty:將查詢到主鍵值設(shè)置到parameterType指定的對象的哪個(gè)屬性 
  order:SELECT LAST_INSERT_ID()執(zhí)行順序,相對于insert語句來說它的執(zhí)行順序 
  resultType:指定SELECT LAST_INSERT_ID()的結(jié)果類型 
   --> 
  <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer"> 
    SELECT LAST_INSERT_ID() 
  </selectKey> 
  insert into user(username,birthday,sex,address) value(#{username},#{birthday},#{sex},#{address})     
</insert> 

2)id非自增,uuid類型-mysql select uuid()函數(shù)

<!--  
使用mysql的uuid()生成主鍵 
執(zhí)行過程: 
首先通過uuid()得到主鍵,將主鍵設(shè)置到user對象的id屬性中 
其次在insert執(zhí)行時(shí),從user對象中取出id屬性值 
 --> 
<selectKey keyProperty="id" order="BEFORE" resultType="java.lang.String"> 
  SELECT uuid() 
</selectKey> 
insert into user(id,username,birthday,sex,address) value(#{id},#{username},#{birthday},#{sex},#{address}) 

以上所述是小編給大家介紹的Mybatis輸入輸出映射及動態(tài)SQL Review,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對億速云網(wǎng)站的支持!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI