Mybatis(ParameterType)傳遞多個不同類型的參數(shù)方式
Mybatis傳遞多個不同類型的參數(shù)
在一些場景下,傳參是需要多個參數(shù)的。一個參數(shù)不太夠用,如:parameterType="ImGroup"。
最開始的想法是封裝一個專用用來當(dāng)參數(shù)的對象,把多個對象包裝到一起,這樣就要以實現(xiàn)多個參數(shù)的傳遞。
但是總感覺這樣的方法太笨了,而且封裝的對象有可能只能在參數(shù)這塊用一下,重用性不高。還會導(dǎo)致項目中多一個類文件。
那么應(yīng)該還可以使用map或者list封裝對象。但是畢竟集合的可變性比較高,使用起來又沒有類方便。
比起這些思路,我感覺注解的方式最方便。
基于注解
public List<ImGroup> selectImGroupListByUserId(@Param("userId")String userId, @Param("group") ImGroup imGroup);去掉parameterType屬性。
這里ImGroup 是一個對象,里面有一堆屬性,比如:name
<select id="getXXXBeanList" resultType="XXBean">
select t.* from tableName where id = #{userId} and name = #{group.name} ?
</select> ?由于是多參數(shù)那么就不能使用parameterType, 這里用@Param來指定哪一個
我的實際代碼如下 :
ImGroupMapper.java
? ?/**
? ? ?* 通過UserId來查詢
? ? ?* @param imGroup
? ? ?* @return
? ? ?*/
? ? public List<ImGroup> selectImGroupListByUserId(@Param("userId")String userId, @Param("group") ImGroup imGroup);ImGroupMapper.xml
?<select id="selectImGroupListByUserId" resultMap="ImGroupResult">
? ? ? ? select g.* from im_group_user gu left join im_group g on gu.group_id=g.group_id
? ? ? ? <where>
? ? ? ? ? ? gu.user_id=#{userId}
? ? ? ? ? ? <if test="group.userCount != null "> and g.user_count = #{group.userCount}</if>
? ? ? ? ? ? <if test="group.groupName != null ?and group.groupName != ''"> and g.group_name like concat('%', #{group.groupName}, '%')</if>
? ? ? ? ? ? <if test="group.groupIntroduction != null ?and group.groupIntroduction != ''"> and g.group_introduction = #{group.groupIntroduction}</if>
? ? ? ? ? ? <if test="group.groupPortrait != null ?and group.groupPortrait != ''"> and g.group_portrait = #{group.groupPortrait}</if>
? ? ? ? ? ? <if test="group.groupOwner != null ?and group.groupOwner != ''"> and g.group_owner = #{group.groupOwner}</if>
? ? ? ? ? ? <if test="group.groupManager != null ?and group.groupManager != ''"> and g.group_manager = #{group.groupManager}</if>
? ? ? ? </where>
? ? </select>Mybatis傳遞單個String類型的參數(shù)
使用mybatis接口參數(shù)只有一個string的時候 如果不指定@Param 的話mybatis去會把parameterType參數(shù)默認(rèn)成接口的參數(shù)類型然后對于xml里的#{a}參數(shù) 去調(diào)用該類型下參數(shù)a 的get/set方法然后就報錯了。
使用了@Param注解 mybatis就會一一對應(yīng)賦值就不會導(dǎo)致這個錯誤。
接口如下:

xml文件如下:

報錯如下:

改成如下就可以了:

總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java代碼實現(xiàn)mysql分表操作(用戶行為記錄)
這篇文章主要介紹了java代碼實現(xiàn)mysql分表操作(用戶行為記錄),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02
Java基礎(chǔ)之JDK1.8新特性lambda表達(dá)式詳解
函數(shù)式接口有且僅有一個抽象方法,但是可以有多個非抽象方法的接口,函數(shù)式接口可以被隱式轉(zhuǎn)換為lambda表達(dá)式,這篇文章主要介紹了Java基礎(chǔ)之lambda表達(dá)式(JDK1.8新特性),需要的朋友可以參考下2023-08-08
解決springboot 連接 mysql 時報錯 using password: NO的方案
在本篇文章里小編給大家整理了關(guān)于解決springboot 連接 mysql 時報錯 using password: NO的方案,有需要的朋友們可以學(xué)習(xí)下。2020-01-01
SpringBoot @CompentScan excludeFilters配置無效的解決方案
這篇文章主要介紹了SpringBoot @CompentScan excludeFilters配置無效的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
Spring?Cloud?Gateway?整合?knife4j?聚合接口文檔功能
這篇文章主要介紹了Spring?Cloud?Gateway?整合?knife4j?聚合接口文檔的相關(guān)知識,我們可以基于?Spring?Cloud?Gateway?網(wǎng)關(guān)?+?nacos?+?knife4j?對所有微服務(wù)項目的接口文檔進(jìn)行聚合,從而實現(xiàn)我們想要的文檔管理功能,需要的朋友可以參考下2022-02-02
解決idea中java出現(xiàn)無效的源發(fā)行版問題
這篇文章主要給大家介紹了關(guān)于解決idea中java出現(xiàn)無效的源發(fā)行版問題的相關(guān)資料,無效的源發(fā)行版是指IntelliJ IDEA無法正確識別和處理的源代碼版本,這可能是由于錯誤的配置、缺少依賴項、不兼容的插件或其他問題導(dǎo)致的,需要的朋友可以參考下2024-01-01

