Mybatis的幾種傳參方式詳解
前言
- 前幾天恰好面試一個應(yīng)屆生,問了一個很簡單的問題:你了解過Mybatis中有幾種傳參方式嗎?
- 沒想到其他問題回答的很好,唯獨這個問題一知半解,勉強回答了其中兩種方式。
- 于是這篇文章就來說一說Mybatis傳參的幾種常見方式,給正在面試或者準(zhǔn)備面試的朋友鞏固一下。
單個參數(shù)
單個參數(shù)的傳參比較簡單,可以是任意形式的,比如#{a}、#或者#{param1},但是為了開發(fā)規(guī)范,盡量使用和入?yún)r一樣。
Mapper如下:
UserInfo selectByUserId(String userId);
XML如下:
<select id="selectByUserId" resultType="cn.cb.demo.domain.UserInfo">
select * from user_info where user_id=#{userId} and status=1
</select>
多個參數(shù)
多個參數(shù)的情況下有很多種傳參的方式,下面一一介紹。
使用索引【不推薦】
- 多個參數(shù)可以使用類似于索引的方式傳值,比如
#{param1}對應(yīng)第一個參數(shù),#{param2}對應(yīng)第二個參數(shù)....... - Mapper方法如下:
UserInfo selectByUserIdAndStatus(String userId,Integer status);
XML如下:
<select id="selectByUserIdAndStatus" resultType="cn.cb.demo.domain.UserInfo">
select * from user_info where user_id=#{param1} and status=#{param2}
</select>
注意:由于開發(fā)規(guī)范,此種方式不推薦開發(fā)中使用。
使用@Param
@Param這個注解用于指定key,一旦指定了key,在SQL中即可對應(yīng)的key入?yún)ⅰ?/p>
Mapper方法如下:
UserInfo selectByUserIdAndStatus(@Param("userId") String userId,@Param("status") Integer status);
XML如下:
<select id="selectByUserIdAndStatus" resultType="cn.cb.demo.domain.UserInfo">
select * from user_info where user_id=#{userId} and status=#{status}
</select>
使用Map
Mybatis底層就是將入?yún)⑥D(zhuǎn)換成Map,入?yún)鱉ap當(dāng)然也行,此時#{key}中的key就對應(yīng)Map中的key。
Mapper中的方法如下:
UserInfo selectByUserIdAndStatusMap(Map<String,Object> map);
XML如下:
<select id="selectByUserIdAndStatusMap" resultType="cn.cb.demo.domain.UserInfo">
select * from user_info where user_id=#{userId} and status=#{status}
</select>
測試如下:
@Test
void contextLoads() {
Map<String,Object> map=new HashMap<>();
map.put("userId","1222");
map.put("status",1);
UserInfo userInfo = userMapper.selectByUserIdAndStatusMap(map);
System.out.println(userInfo);
}
POJO【推薦】
多個參數(shù)可以使用實體類封裝,此時對應(yīng)的key就是屬性名稱,注意一定要有g(shù)et方法。
Mapper方法如下:
UserInfo selectByEntity(UserInfoReq userInfoReq);
XML如下:
<select id="selectByEntity" resultType="cn.cb.demo.domain.UserInfo">
select * from user_info where user_id=#{userId} and status=#{status}
</select>
實體類如下:
@Data
public class UserInfoReq {
private String userId;
private Integer status;
}
List傳參
List傳參也是比較常見的,通常是SQL中的in。
Mapper方法如下:
List<UserInfo> selectList( List<String> userIds);
XML如下:
<select id="selectList" resultMap="userResultMap">
select * from user_info where status=1
and user_id in
<foreach collection="list" item="item" open="(" separator="," close=")" >
#{item}
</foreach>
</select>
數(shù)組傳參
這種方式類似List傳參,依舊使用foreach語法。
Mapper方法如下:
List<UserInfo> selectList( String[] userIds);
XML如下:
<select id="selectList" resultMap="userResultMap">
select * from user_info where status=1
and user_id in
<foreach collection="array" item="item" open="(" separator="," close=")" >
#{item}
</foreach>
</select>
總結(jié)
到此這篇關(guān)于Mybatis的幾種傳參方式詳解的文章就介紹到這了,更多相關(guān)Mybatis傳參方式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java數(shù)據(jù)結(jié)構(gòu)及算法實例:漢諾塔問題 Hanoi
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)及算法實例:漢諾塔問題 Hanoi,本文直接給出實現(xiàn)代碼,代碼中包含大量注釋,需要的朋友可以參考下2015-06-06
java如何在項目中實現(xiàn)excel導(dǎo)入導(dǎo)出功能
這篇文章主要介紹了java如何在項目中實現(xiàn)excel導(dǎo)入導(dǎo)出功能的相關(guān)資料,EasyExcel是一個基于Apache?POI開發(fā)的開源Java庫,用于簡化Excel文件的讀寫操作,文中將用法介紹的非常詳細(xì),需要的朋友可以參考下2024-10-10
Java詳細(xì)講解不同版本的接口語法和抽象類與接口的區(qū)別
對于面向?qū)ο缶幊虂碚f,抽象是它的一大特征之一,在?Java?中可以通過兩種形式來體現(xiàn)OOP的抽象:接口和抽象類,下面這篇文章主要給大家介紹了關(guān)于Java入門基礎(chǔ)之抽象類與接口的相關(guān)資料,需要的朋友可以參考下2022-04-04
Spring+Hibernate+Struts(SSH)框架整合實戰(zhàn)
SSH是 struts+spring+hibernate的一個集成框架,是目前比較流行的一種Web應(yīng)用程序開源框架。本篇文章主要介紹了Spring+Hibernate+Struts(SSH)框架整合實戰(zhàn),非常具有實用價值,需要的朋友可以參考下2018-04-04

