欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2017年02月14日 13:58:53   作者:鐘艾伶  
這篇文章主要介紹了Mybatis輸入輸出映射及動(dòng)態(tài)SQL Review,需要的朋友可以參考下

一、輸入映射

    通過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 對(duì)應(yīng)hashmap中的key-value

二、輸出映射

1、resultType類型輸出

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

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

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

三、動(dòng)態(tài)SQL

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

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

     首先創(chuàng)建pojo類,提供該pojo對(duì)應(yīng)的mapper映射文件,對(duì)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)識(shí) 
  最好基于單表來定義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:指定輸入 對(duì)象中集合屬性 
      item:每個(gè)遍歷生成對(duì)象中 
      open:開始遍歷時(shí)拼接的串 
      close:結(jié)束遍歷時(shí)拼接的串 
      separator:遍歷的兩個(gè)對(duì)象中需要拼接的串 
       --> 
       <!-- 使用實(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包裝對(duì)象中性別值 
  ${userCustom.username}:取出pojo包裝對(duì)象中用戶名稱 
   --> 
<select id="findUserList" parameterType="cn.itcast.mybatis.po.UserQueryVo" resultType="cn.itcast.mybatis.po.UserCustom"> 
  SELECT * FROM USER 
  <!-- 
  where可以自動(dòng)去掉條件中的第一個(gè)and 
   --> 
  <where> 
    <!-- 引用sql片段 的id,如果refid指定的id不在本mapper文件中,需要前邊加namespace --> 
    <include refid="query_user_where"></include> 
    <!-- 在這里還要引用其它的sql片段 --> 
  </where> 
</select> 

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

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

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

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

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

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

1)id為自增類型

<!-- 添加用戶  
parameterType:指定輸入 參數(shù)類型是pojo(包括 用戶信息) 
#{}中指定pojo的屬性名,接收到pojo對(duì)象的屬性值,mybatis通過OGNL獲取對(duì)象的屬性值 
--> 
<insert id="insertUser" parameterType="cn.itcast.mybatis.po.User"> 
  <!--  
  將插入數(shù)據(jù)的主鍵返回,返回到user對(duì)象中 
  SELECT LAST_INSERT_ID():得到剛insert進(jìn)去記錄的主鍵值,只適用與自增主鍵 
  keyProperty:將查詢到主鍵值設(shè)置到parameterType指定的對(duì)象的哪個(gè)屬性 
  order:SELECT LAST_INSERT_ID()執(zhí)行順序,相對(duì)于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對(duì)象的id屬性中 
其次在insert執(zhí)行時(shí),從user對(duì)象中取出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輸入輸出映射及動(dòng)態(tài)SQL Review,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論