一、輸入映射
通過parameterType指定輸入參數的類型,可以是簡單類型、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類可根據業務需求,創建某單一實體的擴展實體,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>
參數id 和username 對應hashmap中的key-value
二、輸出映射
1、resultType類型輸出
使用resultType進行輸出映射,只有查詢出來的列名和pojo中的屬性名一致,該列才可以映射成功。適用于單表查詢,級聯查詢時使用該類型輸出需要重新創建關聯pojo擴展類進行映射。
如果查詢出來的列名和pojo中的屬性名全部不一致,就不會創建該pojo對象。只要查詢出來的列名和pojo中的屬性有一個一致,就會創建pojo對象。映射失敗的查詢字段返回為空。
2、resultMap類型輸出
如果查詢出來的列名和pojo的屬性名不一致時,可使用resultMap,通過定義一個resultMap列名和pojo屬性名之間作一個映射關系。得以映射輸出結果。
1)定義resultMap
<!-- 定義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的輸出映射類型
<!-- 使用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方法分別配置
<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:結束遍歷時拼接的串
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片段
<!-- 用戶信息綜合查詢
#{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為自增類型
<!-- 添加用戶
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()函數
<!--
使用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,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。