MyBatis3傳遞多個參數(shù)(Multiple Parameters)
傳遞多個參數(shù)一般用在查詢上,比如多個條件組成的查詢,有以下方式去實(shí)現(xiàn):
版本信息:
MyBatis:3.4.4
1、自帶方法
<select id="getUserArticlesByLimit" resultMap="resultUserArticleList">
select user.id,user.userName,user.userAddress,article.id as aid,article.title,article.content from user,article where user.id=article.userid and user.id=#{arg0} limit #{arg1},#{arg2}
</select>
public List<Article> getUserArticlesByLimit(int id,int start,int limit); List<Article> articles=userMapper.getUserArticlesByLimit(1,0,2);
說明,arg0...也可以寫成param0...
2、直接傳遞對象
<select id="dynamicIfTest" parameterType="Article" resultType="Article">
select * from article where 1 = 1
<if test="title != null">
and title = #{title}
</if>
<if test="content != null">
and content = #{content}
</if>
limit 1
</select>
public Article dynamicIfTest(Article article);
Article inArticle = new Article();
inArticle.setTitle("test_title");
Article outArticle = userOperation.dynamicIfTest(inArticle);
3、使用@Praam標(biāo)注
<select id="dynamicChooseTest" resultType="Article">
select * from article where 1 = 1
<choose>
<when test="title != null">
and title = #{title}
</when>
<when test="content != null">
and content = #{content}
</when>
<otherwise>
and tile = "test_title"
</otherwise>
</choose>
</select>
public Article dynamicChooseTest(
@Param("title")
String title,
@Param("content")
String content);
Article outArticle2 = userOperation.dynamicChooseTest("test_title",null);
說明:這種方法同樣可以用在一個參數(shù)的時候。
4、使用HashMap
<select id="getArticleBeanList" resultType="ArticleBean">
select * from article where id = #{id} and name = #[code]
</select>
說明:parameterType="hashmap"可以不用寫。
public List<ArticleBean> getArticleBeanList(HashMap map);
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("id", 1);
map.put("code", "123");
List<Article> articless3 = userOperation.getArticleBeanList(map);
特殊的HashMap示例,用在foreach節(jié)點(diǎn):
<select id="dynamicForeach3Test" resultType="Article">
select * from article where title like "%"#{title}"%" and id in
<foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>
public List<Article> dynamicForeach3Test(Map<String, Object> params);
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("title", "title");
map.put("ids", new int[]{1,3,6});
List<Article> articless3 = userOperation.dynamicForeach3Test(map);
5、List結(jié)合foreach節(jié)點(diǎn)一起使用
<select id="dynamicForeachTest" resultType="Article">
select * from article where id in
<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>
public List<Article> dynamicForeachTest(List<Integer> ids);
List<Integer> ids = new ArrayList<Integer>();
ids.add(1);
ids.add(3);
ids.add(6);
List<Article> articless = userOperation.dynamicForeachTest(ids);
6、數(shù)組結(jié)合foreach節(jié)點(diǎn)一起使用
<select id="dynamicForeach2Test" resultType="Article">
select * from article where id in
<foreach collection="array" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>
public List<Article> dynamicForeach2Test(int[] ids);
int[] ids2 = {1,3,6};
List<Article> articless2 = userOperation.dynamicForeach2Test(ids2);
參考:
http://www.yihaomen.com/article/java/426.htm
到此這篇關(guān)于MyBatis3傳遞多個參數(shù)(Multiple Parameters)的文章就介紹到這了,更多相關(guān)MyBatis3傳遞多個參數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Jdk11使用HttpClient提交Http2請求的實(shí)現(xiàn)方法
這篇文章主要介紹了Jdk11使用HttpClient提交Http2請求的實(shí)現(xiàn)方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08
Spring?Security過濾器鏈加載執(zhí)行流程源碼解析
Spring?Boot?對于?Spring?Security?提供了自動化配置方案,可以使用更少的配置來使用?Spring?Security。那么這個過濾器鏈?zhǔn)窃趺醇虞d和實(shí)現(xiàn)攔截的呢,對Spring?Security過濾器鏈加載執(zhí)行流程感興趣的朋友一起看看吧2021-12-12
劍指Offer之Java算法習(xí)題精講二叉樹的構(gòu)造和遍歷
跟著思路走,之后從簡單題入手,反復(fù)去看,做過之后可能會忘記,之后再做一次,記不住就反復(fù)做,反復(fù)尋求思路和規(guī)律,慢慢積累就會發(fā)現(xiàn)質(zhì)的變化2022-03-03
Spring Boot中的 6 種API請求參數(shù)讀取方式示例詳解
使用Spring Boot開發(fā)API的時候,讀取請求參數(shù)是服務(wù)端編碼中最基本的一項(xiàng)操作,Spring Boot中也提供了多種機(jī)制來滿足不同的API設(shè)計要求,這篇文章主要介紹了Spring Boot中的 6 種API請求參數(shù)讀取方式示例詳解,需要的朋友可以參考下2024-05-05
Java使用EasyExcel模版導(dǎo)出詳細(xì)操作教程
業(yè)務(wù)中經(jīng)常需要按照一個特定的模板導(dǎo)出特定內(nèi)容,有些單元格還要求特殊的格式,所以下面這篇文章主要給大家介紹了關(guān)于Java使用EasyExcel模版導(dǎo)出的相關(guān)資料,需要的朋友可以參考下2023-10-10
java多線程關(guān)鍵字final和static詳解
這篇文章主要介紹了java多線程關(guān)鍵字final和static詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-01-01
通過圖例了解IDEA引入JQuery實(shí)現(xiàn)步驟
這篇文章主要介紹了IDEA引入JQuery實(shí)現(xiàn)步驟圖解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09

