Mybatis輸入輸出映射及動態(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è)務需求,創(chuàng)建某單一實體的擴展實體,User類的擴展類-User和訂單實體的綜合實體。
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 對應hashmap中的key-value
二、輸出映射
1、resultType類型輸出
使用resultType進行輸出映射,只有查詢出來的列名和pojo中的屬性名一致,該列才可以映射成功。適用于單表查詢,級聯(lián)查詢時使用該類型輸出需要重新創(chuàng)建關(guān)聯(lián)pojo擴展類進行映射。
如果查詢出來的列名和pojo中的屬性名全部不一致,就不會創(chuàng)建該pojo對象。只要查詢出來的列名和pojo中的屬性有一個一致,就會創(chuàng)建pojo對象。映射失敗的查詢字段返回為空。
2、resultMap類型輸出
如果查詢出來的列名和pojo的屬性名不一致時,可使用resultMap,通過定義一個resultMap列名和pojo屬性名之間作一個映射關(guān)系。得以映射輸出結(jié)果。
1)定義resultMap
<!-- 定義resultMap 將SELECT id id_,username username_ FROM USER 和User類中的屬性作一個映射關(guān)系 type:resultMap最終映射的java對象類型,可以使用別名 id:對resultMap的唯一標識 --> <resultMap type="user" id="userResultMap"> <!-- id表示查詢結(jié)果集中唯一標識 column:查詢出來的列名 property:type指定的pojo類型中的屬性名 最終resultMap對column和property作一個映射關(guān)系 (對應關(guān)系) --> <id column="id_" property="id"/> <!-- result:對普通名映射定義 column:查詢出來的列名 property:type指定的pojo類型中的屬性名 最終resultMap對column和property作一個映射關(guān)系 (對應關(guān)系) --> <result column="username_" property="username"/> </resultMap>
2)使用resultMap作為statement的輸出映射類型
<!-- 使用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>
三、動態(tài)SQL
Mybatis核心 對sql語句進行靈活操作,通過表達式進行判斷,對sql進行靈活拼接、組裝。
1、動態(tài)SQL示例
首先創(chuàng)建pojo類,提供該pojo對應的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中的判斷條件進行封裝,提高復用
1)定義sql片段
<!-- 定義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:結(jié)束遍歷時拼接的串
separator:遍歷的兩個對象中需要拼接的串
-->
<!-- 使用實現(xiàn)下邊的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>
<!-- 實現(xiàn) “ 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片段
<!-- 用戶信息綜合查詢
#{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>
注:在使用動態(tài)sql時注意
1、#{}和${}的不同
#{}表示一個占位符號,#{}接收輸入?yún)?shù),類型可以是簡單類型,pojo、hashmap。當使用#{}接收簡單類型參數(shù)時,#{}中可以寫成value或其它名稱。當接收pojo對象值,寫入對象的屬性值,形如對象.屬性.屬性.屬性...的方式獲取。
${}${}表示一個拼接符號,接收輸入?yún)?shù),類型可以是簡單類型,pojo、hashmap。接收簡單類型,${}中只能寫成value。接受pojo對象時,與#{}相同。注意使用$拼接,會引用sql注入,所以不建議使用${}。
2、使用where 標簽,第一個and 條件為空時,自動跳過。所以可以不添加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進去記錄的主鍵值,只適用與自增主鍵
keyProperty:將查詢到主鍵值設置到parameterType指定的對象的哪個屬性
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()得到主鍵,將主鍵設置到user對象的id屬性中
其次在insert執(zhí)行時,從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,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Mybatis查詢Sql結(jié)果未映射到對應得實體類上的問題解決
使用mybatis查詢表數(shù)據(jù)得時候,發(fā)現(xiàn)對應得實體類字段好多都是null,本文主要介紹了Mybatis查詢Sql結(jié)果未映射到對應得實體類上的問題解決,具有一定的參考價值,感興趣的可以了解一下2024-02-02
Intellij IDEA 2019 最新亂碼問題及解決必殺技(必看篇)
大家在使用Intellij IDEA 的時候會經(jīng)常遇到各種亂碼問題,今天小編給大家分享一些關(guān)于Intellij IDEA 2019 最新亂碼問題及解決必殺技,感興趣的朋友跟隨小編一起看看吧2020-04-04
Spring實戰(zhàn)之使用ClassPathResource加載xml資源示例
這篇文章主要介紹了Spring實戰(zhàn)之使用ClassPathResource加載xml資源,結(jié)合實例形式分析了Spring使用ClassPathResource加載xml資源的具體實現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下2019-12-12
分布式醫(yī)療掛號系統(tǒng)Nacos微服務Feign遠程調(diào)用數(shù)據(jù)字典
這篇文章主要為大家介紹了分布式醫(yī)療掛號系統(tǒng)Nacos微服務Feign遠程調(diào)用數(shù)據(jù)字典,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪<BR>2022-04-04
Mybatis RowBounds 限制查詢條數(shù)的實現(xiàn)代碼
Oracle 數(shù)據(jù)庫查詢增加RowBounds限制查詢條數(shù),默認是0到1000條。下面給大家分享Mybatis RowBounds 限制查詢條數(shù)的實現(xiàn)代碼,需要的朋友參考下吧2016-11-11
SpringBoot使用AOP實現(xiàn)日志記錄功能詳解
這篇文章主要為大家介紹了SpringBoot使用AOP實現(xiàn)日志記錄功能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07

