Mybatis(ParameterType)傳遞多個不同類型的參數(shù)方式
Mybatis傳遞多個不同類型的參數(shù)
在一些場景下,傳參是需要多個參數(shù)的。一個參數(shù)不太夠用,如:parameterType="ImGroup"。
最開始的想法是封裝一個專用用來當(dāng)參數(shù)的對象,把多個對象包裝到一起,這樣就要以實(shí)現(xiàn)多個參數(shù)的傳遞。
但是總感覺這樣的方法太笨了,而且封裝的對象有可能只能在參數(shù)這塊用一下,重用性不高。還會導(dǎo)致項(xiàng)目中多一個類文件。
那么應(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來指定哪一個
我的實(shí)際代碼如下 :
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方法然后就報(bào)錯了。
使用了@Param注解 mybatis就會一一對應(yīng)賦值就不會導(dǎo)致這個錯誤。
接口如下:
xml文件如下:
報(bào)錯如下:
改成如下就可以了:
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot如何實(shí)現(xiàn)同域SSO(單點(diǎn)登錄)
單點(diǎn)登錄(SingleSignOn,SSO),就是通過用戶的一次性鑒別登錄。即在多個應(yīng)用系統(tǒng)中,只需要登錄一次,就可以訪問其他相互信任的應(yīng)用系統(tǒng),本文將介紹SpringBoot如何實(shí)現(xiàn)同域SSO(單點(diǎn)登錄)2021-05-05Spring SpringMVC在啟動完成后執(zhí)行方法源碼解析
這篇文章主要介紹了SpringMVC在啟動完成后執(zhí)行方法源碼解析,還是非常不錯的,在這里分享給大家,需要的朋友可以參考下。2017-09-09Mybatis中的config.xml配置文件詳細(xì)解析
這篇文章主要介紹了詳解Mybatis-config.xml配置文件,需要的朋友可以參考下2017-12-12Spring Boot的listener(監(jiān)聽器)簡單使用實(shí)例詳解
監(jiān)聽器(Listener)的注冊方法和 Servlet 一樣,有兩種方式:代碼注冊或者注解注冊。接下來通過本文給大家介紹Spring Boot的listener(監(jiān)聽器)簡單使用,需要的朋友可以參考下2017-04-04Java ES(Elasticsearch) 中的and 和 or 查
Elasticsearch 是一個分布式、高擴(kuò)展、高實(shí)時的搜索與數(shù)據(jù)分析引擎,es中match查詢中,查詢字符串分詞后,默認(rèn)是or或者的關(guān)系,這篇文章主要介紹了ES 中的and 和 or 查詢,需要的朋友可以參考下2022-11-11