MyBatis3傳遞多個(gè)參數(shù)(Multiple Parameters)
傳遞多個(gè)參數(shù)一般用在查詢上,比如多個(gè)條件組成的查詢,有以下方式去實(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、直接傳遞對(duì)象
<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);
說明:這種方法同樣可以用在一個(gè)參數(shù)的時(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傳遞多個(gè)參數(shù)(Multiple Parameters)的文章就介紹到這了,更多相關(guān)MyBatis3傳遞多個(gè)參數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 淺談為什么要使用mybatis的@param
- mybatis多個(gè)接口參數(shù)的注解使用方式(@Param)
- MyBatis中傳入?yún)?shù)parameterType類型詳解
- Mybatis中@Param的用法和作用詳解
- 解決Python paramiko 模塊遠(yuǎn)程執(zhí)行ssh 命令 nohup 不生效的問題
- python requests包的request()函數(shù)中的參數(shù)-params和data的區(qū)別介紹
- vue.js this.$router.push獲取不到params參數(shù)問題
- Mybatis使用@param注解四種情況解析
相關(guān)文章
SpringBoot的配置文件application.yml及加載順序詳解
這篇文章主要介紹了SpringBoot的配置文件application.yml及加載順序,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
Java中System.setProperty()用法與實(shí)際應(yīng)用場景
System.setProperty是Java中用于設(shè)置系統(tǒng)屬性的方法,它允許我們在運(yùn)行時(shí)為Java虛擬機(jī)(JVM)或應(yīng)用程序設(shè)置一些全局的系統(tǒng)屬性,下面這篇文章主要給大家介紹了關(guān)于Java中System.setProperty()用法與實(shí)際應(yīng)用場景的相關(guān)資料,需要的朋友可以參考下2024-04-04
Java中的SimpleDateFormat的線程安全問題詳解
這篇文章主要介紹了Java中的SimpleDateFormat的線程安全問題詳解,sonar 是一個(gè)代碼質(zhì)量管理工具,SonarQube是一個(gè)用于代碼質(zhì)量管理的開放平臺(tái),為項(xiàng)目提供可視化報(bào)告, 連續(xù)追蹤項(xiàng)目質(zhì)量演化過程,需要的朋友可以參考下2024-01-01
關(guān)于Mybatis-plus設(shè)置字段為空的正確寫法
這篇文章主要介紹了關(guān)于Mybatis-plus設(shè)置字段為空的正確寫法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
SpringMVC 異常處理機(jī)制與自定義異常處理方式
這篇文章主要介紹了SpringMVC 異常處理機(jī)制與自定義異常處理方式,具有很好的開車價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
spring boot多數(shù)據(jù)源動(dòng)態(tài)切換代碼實(shí)例
這篇文章主要介紹了spring boot多數(shù)據(jù)源動(dòng)態(tài)切換代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01
Mybatis中resultMap標(biāo)簽和sql標(biāo)簽的設(shè)置方式
這篇文章主要介紹了Mybatis中resultMap標(biāo)簽和sql標(biāo)簽的設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01

