MyBatis3傳遞多個參數(shù)(Multiple Parameters)
傳遞多個參數(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標注
<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é)點:
<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結合foreach節(jié)點一起使用
<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ù)組結合foreach節(jié)點一起使用
<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
到此這篇關于MyBatis3傳遞多個參數(shù)(Multiple Parameters)的文章就介紹到這了,更多相關MyBatis3傳遞多個參數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot的配置文件application.yml及加載順序詳解
這篇文章主要介紹了SpringBoot的配置文件application.yml及加載順序,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
Java中System.setProperty()用法與實際應用場景
System.setProperty是Java中用于設置系統(tǒng)屬性的方法,它允許我們在運行時為Java虛擬機(JVM)或應用程序設置一些全局的系統(tǒng)屬性,下面這篇文章主要給大家介紹了關于Java中System.setProperty()用法與實際應用場景的相關資料,需要的朋友可以參考下2024-04-04
Java中的SimpleDateFormat的線程安全問題詳解
這篇文章主要介紹了Java中的SimpleDateFormat的線程安全問題詳解,sonar 是一個代碼質量管理工具,SonarQube是一個用于代碼質量管理的開放平臺,為項目提供可視化報告, 連續(xù)追蹤項目質量演化過程,需要的朋友可以參考下2024-01-01
spring boot多數(shù)據(jù)源動態(tài)切換代碼實例
這篇文章主要介紹了spring boot多數(shù)據(jù)源動態(tài)切換代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-01-01
Mybatis中resultMap標簽和sql標簽的設置方式
這篇文章主要介紹了Mybatis中resultMap標簽和sql標簽的設置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01

