Mybatis的幾種傳參方式詳解
前言
- 前幾天恰好面試一個(gè)應(yīng)屆生,問了一個(gè)很簡單的問題:你了解過Mybatis中有幾種傳參方式嗎?
- 沒想到其他問題回答的很好,唯獨(dú)這個(gè)問題一知半解,勉強(qiáng)回答了其中兩種方式。
- 于是這篇文章就來說一說Mybatis傳參的幾種常見方式,給正在面試或者準(zhǔn)備面試的朋友鞏固一下。
單個(gè)參數(shù)
單個(gè)參數(shù)的傳參比較簡單,可以是任意形式的,比如#{a}、#或者#{param1},但是為了開發(fā)規(guī)范,盡量使用和入?yún)r(shí)一樣。
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>
多個(gè)參數(shù)
多個(gè)參數(shù)的情況下有很多種傳參的方式,下面一一介紹。
使用索引【不推薦】
- 多個(gè)參數(shù)可以使用類似于索引的方式傳值,比如
#{param1}對應(yīng)第一個(gè)參數(shù),#{param2}對應(yīng)第二個(gè)參數(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這個(gè)注解用于指定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)然也行,此時(shí)#{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【推薦】
多個(gè)參數(shù)可以使用實(shí)體類封裝,此時(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>
實(shí)體類如下:
@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)文章
使用Spring Boot Maven插件的詳細(xì)方法
這篇文章主要介紹了如何使用Spring Boot Maven插件,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05
Spring整合Quartz定時(shí)任務(wù)并在集群、分布式系統(tǒng)中的應(yīng)用
這篇文章主要介紹了Spring整合Quartz定時(shí)任務(wù)并在集群、分布式系統(tǒng)中的應(yīng)用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-04-04
Spring Boot 項(xiàng)目中整合 MyBatis 和 PageHel
這篇文章主要介紹了Spring Boot 項(xiàng)目中整合 MyBatis 和 PageHelper的操作步驟,整合 PageHelper 到 Spring Boot 項(xiàng)目中主要包括添加依賴、配置數(shù)據(jù)源與 MyBatis、配置 PageHelper 以及在業(yè)務(wù)邏輯中使用 PageHelper 進(jìn)行分頁查詢,需要的朋友可以參考下2024-04-04
springboot2整合redis使用lettuce連接池的方法(解決lettuce連接池?zé)o效問題)
這篇文章主要介紹了springboot2整合redis使用lettuce連接池(解決lettuce連接池?zé)o效問題),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12

