mybatis多個接口參數(shù)的注解使用方式(@Param)
1 簡介
1.1 單參數(shù)
在 Mybatis 中, 很多時候, 我們傳入接口的參數(shù)只有一個。 對應(yīng)接口參數(shù)的類型有兩種, 一種是基本的參數(shù)類型, 一種是 JavaBean 。
例如在根據(jù)主鍵獲取對象時, 我們只需要傳入一個主鍵的參數(shù)即可。 而在插入, 更新等操作時, 一般會涉及到很多參數(shù), 我們就使用 JavaBean 。
1.2 多參數(shù)
但是, 在實際的情況中, 我們遇到類似這樣的情況可能:
- 接口需要使用的參數(shù)多于一個;
- 接口需要使用的參數(shù)又遠少于對應(yīng) JavaBean 的成員變量, 或者需要多個 JavaBean 對象;
- 或者需要使用的參數(shù)對應(yīng) JavaBean 沒有相應(yīng)的成員變量。
比如 獲取一段時間產(chǎn)生的日志信息, 日志對應(yīng)的 JavaBean 只有一個日期, 那我們使用該 JavaBean 就無法滿足我們的要求。
又比如我們進行模糊搜索, 搜索條件只有兩個, 但對應(yīng)的 JavaBean 有 50+ 個成員變量, 那創(chuàng)建對應(yīng)的 JavaBean 就過于浪費了。
對此, 我知道的有如下幾種方法
2 多個接口參數(shù)的兩種使用方式
2.1 Map 方法(不推薦)
Map 方法的使用很簡單, 就是將對應(yīng)的參數(shù)以 key-value 的方式存儲, key 對應(yīng) SQL 中的參數(shù)名字, value 對應(yīng)需要傳入的參數(shù)值。
以獲取一段時間內(nèi)存儲的用戶為例
2.1.1 創(chuàng)建接口方法
/** * 獲取一段時間內(nèi)的用戶 * @param params * @return */ List<Student> selectBetweenCreatedTime(Map<String, Object> params);
該方法返回的是多個記錄, 因此使用 List 作為返回值。
2.1.2 配置對應(yīng)的SQL
<select id="selectBetweenCreatedTime" parameterType="java.util.Map" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from student
where gmt_created > #{bTime, jdbcType=TIMESTAMP} and gmt_created < #{eTime, jdbcType=TIMESTAMP}
</select>
id 與 之前創(chuàng)建的方法名一樣。
2.1.3 調(diào)用
@Test
public void testSelectBtweenCreatedTimeMap() {
Map<String, Object> params = new HashMap<>();
Calendar bTime = Calendar.getInstance();
// month 是從0~11, 所以9月是8
bTime.set(2018, Calendar.AUGUST, 29);
params.put("bTime", bTime.getTime());
Calendar eTime = Calendar.getInstance();
eTime.set(2018,Calendar.SEPTEMBER,2);
params.put("eTime", eTime.getTime());
SqlSession sqlSession = null;
try {
sqlSession = sqlSessionFactory.openSession();
StudentMapper studentMapper = (StudentMapper) sqlSession.getMapper(StudentMapper.class);
List<Student> students = studentMapper.selectBetweenCreatedTime(params);
for (int i = 0; i < students.size(); i++) {
System.out.println(students.get(i));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}
調(diào)用方法很簡單, 傳入相應(yīng)的 Map 參數(shù)即可。 此時, Map 中的 key 對應(yīng)。 因此, 在此例子中傳入的參數(shù)
- 傳入一個 key 為 btime 的時間, 作為開始時間;
- 傳入一個 key 為 etime 的時間, 作為結(jié)束時間;
2.2 @Param 方法(推薦)
@Param方法就是使用注解的方式,
2.2.1 創(chuàng)建接口方法
/**
* 獲取指定時間內(nèi)的對象
* @param pbTime 開始時間
* @param peTime 結(jié)束時間
* @return
*/
List<Student> selectBetweenCreatedTimeAnno(@Param("bTime")Date pbTime, @Param("eTime")Date peTime);
@Param(“bTime”)就是告訴 mybatis , 參數(shù) pbTime 在 SQL 語句中用 bTime 作為 key 。
也就是說, mybatis 幫我們完成了調(diào)用時, 類似 params.put(“bTime”, pbTime) 這個過程。
2.2.2 配置 SQL 語句
<select id="selectBetweenCreatedTimeAnno" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from student
where gmt_created > #{bTime, jdbcType=TIMESTAMP} and gmt_created < #{eTime, jdbcType=TIMESTAMP}
</select>
此處的 bTime 對應(yīng)** @Param(“bTime”)** 中的 bTime , 需要完全一致。
eTime也是一樣。
2.2.3 調(diào)用
在調(diào)用時, 不需要創(chuàng)建 Map 了, 只需要按參數(shù)提示傳入對應(yīng)的實際參數(shù)即可。
@Test
public void testSelectBtweenCreatedTimeAnno() {
Map<String, Object> params = new HashMap<>();
Calendar bTime = Calendar.getInstance();
// month 是從0~11, 所以9月是8
bTime.set(2018, Calendar.AUGUST, 29);
Calendar eTime = Calendar.getInstance();
eTime.set(2018,Calendar.SEPTEMBER,2);
SqlSession sqlSession = null;
try {
sqlSession = sqlSessionFactory.openSession();
StudentMapper studentMapper = (StudentMapper) sqlSession.getMapper(StudentMapper.class);
List<Student> students = studentMapper.selectBetweenCreatedTimeAnno(bTime.getTime(), eTime.getTime());
for (int i = 0; i < students.size(); i++) {
System.out.println(students.get(i));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}
3 @Param 的優(yōu)勢
Map 方式的缺點就是需要手動創(chuàng)建 Map , 并對 SQL 中的參數(shù)進行賦值。其缺點:
- 手動創(chuàng)建 Map 這個過程很不簡潔, 看著很繁瑣。
- 手動對參數(shù)進行賦值, 很容易出錯。 比如本來是要 params.put(“bTime”, bTime) 可能會不小心寫成 params.put(“bime”, bTime) , 但是這個時候編譯器并不會提示。
相比于 Map 方式, 使用 @Param 時, 我們在使用上就像調(diào)用方法一樣, 傳入對應(yīng)的實際參數(shù)即可。 調(diào)用時基本不會出錯。
4 Github
相應(yīng)代碼, 可以訪問 我的Github-helloMybatis
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java(TM) Platform SE binary 打開jar文件的操作
這篇文章主要介紹了Java(TM) Platform SE binary 打開jar文件的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02
SpringBoot使用easy-captcha 實現(xiàn)驗證碼登錄功能(解決思路)
文章介紹了如何使用Spring Boot和Easy-Captcha實現(xiàn)驗證碼登錄功能,后端通過Easy-Captcha生成驗證碼并存儲在Redis中,前端獲取驗證碼并顯示給用戶,登錄時,前端將用戶輸入的驗證碼和標識符發(fā)送到后端進行驗證,感興趣的朋友跟隨小編一起看看吧2025-02-02
springboot2.6.7集成springfox3.0.0的示例代碼
這篇文章主要介紹了springboot2.6.7集成springfox3.0.0的示例代碼,本文通過示例代碼給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧2024-04-04
Spring中ContextLoaderListener監(jiān)聽詳解
這篇文章主要介紹了Spring中ContextLoaderListener監(jiān)聽詳解,SpringMVC啟動時會啟動WebApplicationContext類型的容器,并且會調(diào)用之前分析的refresh方法,需要的朋友可以參考下2024-01-01
SpringBoot項目War包部署無法注冊到Nacos中的解決
這篇文章主要介紹了SpringBoot項目War包部署無法注冊到Nacos中的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
Java反射根據(jù)不同方法名動態(tài)調(diào)用不同的方法(實例)
下面小編就為大家?guī)硪黄狫ava反射根據(jù)不同方法名動態(tài)調(diào)用不同的方法(實例)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-08-08
idea亂碼修改bin目錄下的idea.exe.vmoptions無效問題
這篇文章主要介紹了idea亂碼修改bin目錄下的idea.exe.vmoptions無效問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04

