mybatis参数有list和实体类_Mybatis的几种传参方式,你了解吗?
生活随笔
收集整理的這篇文章主要介紹了
mybatis参数有list和实体类_Mybatis的几种传参方式,你了解吗?
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
目錄
- 前言
- 單個參數(shù)
- 多個參數(shù)
- 使用索引【不推薦】
- 使用@Param
- 使用Map
- POJO【推薦】
- List傳參
- 數(shù)組傳參
- 總結
前言
- 前幾天恰好面試一個應屆生,問了一個很簡單的問題:你了解過Mybatis中有幾種傳參方式嗎?
- 沒想到其他問題回答的很好,唯獨這個問題一知半解,勉強回答了其中兩種方式。
- 于是這篇文章就來說一說Mybatis傳參的幾種常見方式,給正在面試或者準備面試的朋友鞏固一下。
單個參數(shù)
- 單個參數(shù)的傳參比較簡單,可以是任意形式的,比如
#{a}、#{b}或者#{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}對應第一個參數(shù),#{param2}對應第二個參數(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中即可對應的key入?yún)ⅰ?/li>- 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換成
Map,入?yún)鱉ap當然也行,此時#{key}中的key就對應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>
- 測試如下:
@Testvoid 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ù)可以使用實體類封裝,此時對應的
key就是屬性名稱,注意一定要有get方法。 - 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=1and 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=1and user_id in<foreach collection="array" item="item" open="(" separator="," close=")" >#{item}</foreach></select>
總結
- 以上幾種傳參的方式在面試或者工作中都會用到,不了解的朋友可以收藏下。
- Mybatis專題文章寫到這里也算是到了尾聲,后期準備再寫寫Mybatis的面經(jīng),如果覺得作者寫的不錯,歡迎關注分享。
http://weixin.qq.com/r/dC_0rBDE4k2drVTP93pm (二維碼自動識別)
總結
以上是生活随笔為你收集整理的mybatis参数有list和实体类_Mybatis的几种传参方式,你了解吗?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 上海买房的话一平米大概需要多少钱
- 下一篇: 手指的假肢,中国内地哪家医院做的好一些?