一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Mybatis輸入輸出映射及動態SQL Review

Mybatis輸入輸出映射及動態SQL Review

2020-08-11 18:51鐘艾伶 Java教程

這篇文章主要介紹了Mybatis輸入輸出映射及動態SQL Review,需要的朋友可以參考下

一、輸入映射

    通過parameterType指定輸入參數的類型,可以是簡單類型、pojo包裝類、HashMap等

1、輸入簡單類型

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

2、輸入pojo包裝類

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

 

     Pojo類可根據業務需求,創建某單一實體的擴展實體,User類的擴展類-User和訂單實體的綜合實體。

3、輸入HashMap類型

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

     參數id 和username 對應hashmap中的key-value

二、輸出映射

1、resultType類型輸出

     使用resultType進行輸出映射,只有查詢出來的列名和pojo中的屬性名一致,該列才可以映射成功。適用于單表查詢,級聯查詢時使用該類型輸出需要重新創建關聯pojo擴展類進行映射。

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

2、resultMap類型輸出

     如果查詢出來的列名和pojo的屬性名不一致時,可使用resultMap,通過定義一個resultMap列名和pojo屬性名之間作一個映射關系。得以映射輸出結果。

1)定義resultMap

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!-- 定義resultMap
將SELECT id id_,username username_ FROM USER 和User類中的屬性作一個映射關系  
type:resultMap最終映射的java對象類型,可以使用別名
id:對resultMap的唯一標識
 -->
 <resultMap type="user" id="userResultMap">
  <!-- id表示查詢結果集中唯一標識 
  column:查詢出來的列名
  property:type指定的pojo類型中的屬性名
  最終resultMap對column和property作一個映射關系 (對應關系)
  -->
  <id column="id_" property="id"/>
  <!-- result:對普通名映射定義
  column:查詢出來的列名
  property:type指定的pojo類型中的屬性名
  最終resultMap對column和property作一個映射關系 (對應關系)
   -->
  <result column="username_" property="username"/>
 </resultMap>

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

?
1
2
3
4
5
6
<!-- 使用resultMap進行輸出映射
resultMap:指定定義的resultMap的id,如果這個resultMap在其它的mapper文件,前邊需要加namespace
-->
<select id="findUserByIdResultMap" parameterType="int" resultMap="userResultMap">
  SELECT id id_,username username_ FROM USER WHERE id=#{value}
</select>

三、動態SQL

     Mybatis核心 對sql語句進行靈活操作,通過表達式進行判斷,對sql進行靈活拼接、組裝。

 1、動態SQL示例

     首先創建pojo類,提供該pojo對應的mapper映射文件,對crud方法分別配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<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中的判斷條件進行封裝,提高復用

1)定義sql片段

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<!-- 定義sql片段
  id:sql片段的唯 一標識
  最好基于單表來定義sql片段,這樣話這個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:每個遍歷生成對象中
      open:開始遍歷時拼接的串
      close:結束遍歷時拼接的串
      separator:遍歷的兩個對象中需要拼接的串
       -->
       <!-- 使用實現下邊的sql拼接:
       AND (id=1 OR id=10 OR id=16
       -->
      <foreach collection="ids" item="user_id" open="AND (" close=")" separator="or">
        <!-- 每個遍歷需要拼接的串 -->
        id=#{user_id}
      </foreach>
      <!-- 實現 “ and id IN(1,10,16)”拼接 -->
      <!-- <foreach collection="ids" item="user_id" open="and id IN(" close=")" separator=",">
        每個遍歷需要拼接的串
        #{user_id}
      </foreach> -->
      </if>
    </if>
  </sql>

2)引用sql片段

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!-- 用戶信息綜合查詢
  #{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可以自動去掉條件中的第一個and
   -->
  <where>
    <!-- 引用sql片段 的id,如果refid指定的id不在本mapper文件中,需要前邊加namespace -->
    <include refid="query_user_where"></include>
    <!-- 在這里還要引用其它的sql片段 -->
  </where>
</select>

注:在使用動態sql時注意

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

     #{}表示一個占位符號,#{}接收輸入參數,類型可以是簡單類型,pojo、hashmap。當使用#{}接收簡單類型參數時,#{}中可以寫成value或其它名稱。當接收pojo對象值,寫入對象的屬性值,形如對象.屬性.屬性.屬性...的方式獲取。

     ${}${}表示一個拼接符號,接收輸入參數,類型可以是簡單類型,pojo、hashmap。接收簡單類型,${}中只能寫成value。接受pojo對象時,與#{}相同。注意使用$拼接,會引用sql注入,所以不建議使用${}。

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

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

1)id為自增類型

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!-- 添加用戶 
parameterType:指定輸入 參數類型是pojo(包括 用戶信息)
#{}中指定pojo的屬性名,接收到pojo對象的屬性值,mybatis通過OGNL獲取對象的屬性值
-->
<insert id="insertUser" parameterType="cn.itcast.mybatis.po.User">
  <!-- 
  將插入數據的主鍵返回,返回到user對象中
  SELECT LAST_INSERT_ID():得到剛insert進去記錄的主鍵值,只適用與自增主鍵
  keyProperty:將查詢到主鍵值設置到parameterType指定的對象的哪個屬性
  order:SELECT LAST_INSERT_ID()執行順序,相對于insert語句來說它的執行順序
  resultType:指定SELECT LAST_INSERT_ID()的結果類型
   -->
  <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()函數

?
1
2
3
4
5
6
7
8
9
10
<!-- 
使用mysql的uuid()生成主鍵
執行過程:
首先通過uuid()得到主鍵,將主鍵設置到user對象的id屬性中
其次在insert執行時,從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輸入輸出映射及動態SQL Review,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!

原文鏈接:http://blog.csdn.net/daybreak1209/article/details/51814047

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 手机能看的黄色网站 | 亚洲免费精品视频 | 91嫩草私人成人亚洲影院 | 男生操女生动态图 | 亚欧洲乱码视频一二三区 | 天天干夜夜玩 | 亚洲va在线va天堂成人 | 1919gogo女厕盗摄 | 四虎永久在线精品国产 | 俄罗斯图书馆无打码久久 | 久久久伊人影院 | 日韩欧美成末人一区二区三区 | 亚洲男人的天堂在线 | 久久视频在线视频观看精品15 | 国产成人精品一区二区不卡 | 妇伦小说| 91果冻制片厂天美传媒 | 国产伊人久久 | 日本在线看免费 | 四虎成人国产精品视频 | ts视频在线观看 | 欧美综合在线 | 三级全黄的视频 | 日本免费久久久久久久网站 | 波多野结衣亚洲一区 | tkvk视频| 国产三级精品91三级在专区 | 高h生子双性美人受 | 久久综合狠狠综合久久综合88 | 91九色国产porny | 成人免费网站视频ww | 日本免费一区二区三区 | 欧美特级午夜一区二区三区 | 国产成人精品一区二区仙踪林 | 国产资源站 | 精品国产香蕉 | 欧美精品一区二区三区免费播放 | 午夜福利自怕 | 高h巨肉play| 午夜亚洲视频 | 久久婷婷五月综合色丁香花 |