Mybatis往Mapper.xml文件中傳遞多個參數(shù)問題
Mybatis往Mapper.xml傳遞多個參數(shù)
場景1
當(dāng)Mapper接口定義了多個參數(shù)的時候就需要使用Param注解來給參數(shù)取個名字,然后在Mapper.xml文件中,使用Param注解中的值(名字),告訴Mybatis你使用的變量是哪一個,用在哪,此時insert就不用在聲明參數(shù)類型了,因為有多個參數(shù)類型,而paramType只能聲明一個,同理,update,delete,select,都是一樣的。
場景2
當(dāng)Mapper接口只有一個參數(shù)的時候,在Mapper.xml文件中需要聲明其參數(shù)類型,此時我們可以使用任意名稱的變量來獲取傳入的值,因為傳遞進來的參數(shù)只有一個。(包括集合)
場景3
當(dāng)Mapper接口定義了一個集合參數(shù)和簡單類型參數(shù)的時候也需要使用Param注解來給參數(shù)取個名字,然后在Mapper.xml文件中使用取得名字來使用它們。
集合在foreach中的colloection標(biāo)簽中,也是寫的參數(shù)的名字,如果接口方法中只有一個集合參數(shù),那么foreach中的colloection標(biāo)簽隨便寫什么都是可以的。
mapper.xml傳參及其使用
@Param(“name”):用來給xml準(zhǔn)確獲取參數(shù)使用
mapper.xml傳參
1.傳多個參數(shù)
mapper層方法:
List<Files> test(String name ,Integer size); //xml:#{0}代表接收的是 dao 層中的第一個參數(shù),#{1}代表 dao 層中第二 //參數(shù),更多參數(shù)一致往后加即可 <select id="test" resulttype="files"> select * from ss_files where name = #{0} and size=#{1} </select> //帶注釋的方式 List<Files> test(@Param(value="name") String name ,@Param(value="size") Integer size); //xml:#{0}代表接收的是 dao 層中的第一個參數(shù),#{1}代表 dao 層中第二 //參數(shù),更多參數(shù)一致往后加即可 <select id="test" resulttype="files"> select * from ss_files where name = #{0} and size=#{1} </select>
2.鍵值對傳參
Service層: Map paramMap=new hashMap(); paramMap.put(“name ”, value); paramMap.put(“size”,value); mapper層方法: List<Files> test(Map paramMap); //直接通過屬性名獲取值 mapper.xml: <select id=”test” resulttype=”files”> select * from ss_files where name = #{name } and size=#{size} </select> //使用注釋 mapper層方法: List<Files> test(@Param(value="paramMap") Map paramMap); //通過.點屬性名的方式獲取值 mapper.xml: <select id=”test” resulttype=”files”> select * from ss_files where name = #{paramMap.name } and size=#{paramMap.size} <if test="paramMap.size!=''"> <![CDATA[and size> #{paramMap.size} ]]> </if> </select>
3.傳數(shù)組/集合
//<foreach > 循環(huán): 循環(huán)體:item 序號:index 集合:collection 分割符:separator //-----------------------------數(shù)組 mapper層方法: List<Files> test(@Param("arrayIds") Integer[] arrayIds); //通過.點屬性名的方式獲取值 mapper.xml: <select id=”test” resulttype=”files” parameterType="Integer[]"> select * from ss_files where <if test="arrayIds!=null and arrayIds.length >0 "> <foreach collection="arrayIds" open=" and id in(" close=")" item="item" separator=","> #{item} </foreach> </if> </select> List<Files> test(@Param("listInt") List<Integer> listInt); //通過.點屬性名的方式獲取值 mapper.xml: <select id=”test” resulttype=”files”> select * from ss_files where <if test="listInt!= null and listInt.size()>0"> and ps.material_code in( <foreach item="item" index="index" collection="listInt" separator=","> #{item} </foreach> ) </if> </select> //-----------------------------集合
4.傳對象參數(shù)
mapper層方法: List<Files> test(Files file); //直接通過屬性名獲取值 mapper.xml: <select id=”test” resulttype=”files”> select * from ss_files where name = #{name } and size=#{size} </select> //使用注釋 mapper層方法: List<Files> test(@Param(value="file") Files file); //通過.點屬性名的方式獲取值 mapper.xml: <select id=”test” resulttype=”files”> select * from ss_files where name = #{file.name } and size=#{file.size} <if test="file.size!=''"> <![CDATA[and size> #{file.size} ]]> </if> </select>
5.同時傳多個參數(shù)和對象
//使用注釋 mapper層方法: List<Files> test(@Param(value="file") Files file,@Param(value="size") Integer size); //通過.點屬性名的方式獲取值 mapper.xml: <select id=”test” resulttype=”files”> select * from ss_files where name = #{file.name } and size=#{size} <if test="file.size!=''"> <![CDATA[and size> #{size} ]]> </if> </select>
mapper.xml部分參數(shù)作用
1.resultMap和 resultType的區(qū)別:
兩者都是表示查詢結(jié)果集與java對象之間的一種關(guān)系,處理查詢結(jié)果集,映射到j(luò)ava對象。
resultMap
:將查詢結(jié)果集中的列一一映射到bean對象的各個屬性,映射的查詢結(jié)果集中的列標(biāo)簽可以根據(jù)需要靈活變化。resultType
:的是bean中的對象類,必須保證查詢結(jié)果集中的屬性 和 bean對象類中的屬性是一一對應(yīng),大小寫不敏感,但是有限制。
2.parameterMap(不推薦) & parameterType
parameterMap和resultMap類似,表示將查詢結(jié)果集中列值的類型一一映射到j(luò)ava對象屬性的類型上,在開發(fā)過程中不推薦這種方式。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
MyBatis的collection和association的使用解讀
這篇文章主要介紹了MyBatis的collection和association的使用解讀2023-12-12springboot實戰(zhàn)權(quán)限管理功能圖文步驟附含源碼
這篇文章主要為大家介紹了springboot實戰(zhàn)權(quán)限管理功能圖文步驟及示例源碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06springboot整合shiro多驗證登錄功能的實現(xiàn)(賬號密碼登錄和使用手機驗證碼登錄)
這篇文章給大家介紹springboot整合shiro多驗證登錄功能的實現(xiàn)方法,包括賬號密碼登錄和使用手機驗證碼登錄功能,本文通過實例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-07-07Java實現(xiàn)轉(zhuǎn)跳不同系統(tǒng)使用枚舉加switch的方式示例
今天小編就為大家分享一篇關(guān)于Java實現(xiàn)轉(zhuǎn)跳不同系統(tǒng)使用枚舉加switch的方式示例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12