最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Mybatis往Mapper.xml文件中傳遞多個(gè)參數(shù)問(wèn)題

 更新時(shí)間:2024年05月18日 14:40:57   作者:w32718155  
這篇文章主要介紹了Mybatis往Mapper.xml文件中傳遞多個(gè)參數(shù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Mybatis往Mapper.xml傳遞多個(gè)參數(shù)

場(chǎng)景1

當(dāng)Mapper接口定義了多個(gè)參數(shù)的時(shí)候就需要使用Param注解來(lái)給參數(shù)取個(gè)名字,然后在Mapper.xml文件中,使用Param注解中的值(名字),告訴Mybatis你使用的變量是哪一個(gè),用在哪,此時(shí)insert就不用在聲明參數(shù)類(lèi)型了,因?yàn)橛卸鄠€(gè)參數(shù)類(lèi)型,而paramType只能聲明一個(gè),同理,update,delete,select,都是一樣的。

場(chǎng)景2

當(dāng)Mapper接口只有一個(gè)參數(shù)的時(shí)候,在Mapper.xml文件中需要聲明其參數(shù)類(lèi)型,此時(shí)我們可以使用任意名稱(chēng)的變量來(lái)獲取傳入的值,因?yàn)閭鬟f進(jìn)來(lái)的參數(shù)只有一個(gè)。(包括集合)

場(chǎng)景3

當(dāng)Mapper接口定義了一個(gè)集合參數(shù)和簡(jiǎn)單類(lèi)型參數(shù)的時(shí)候也需要使用Param注解來(lái)給參數(shù)取個(gè)名字,然后在Mapper.xml文件中使用取得名字來(lái)使用它們。

集合在foreach中的colloection標(biāo)簽中,也是寫(xiě)的參數(shù)的名字,如果接口方法中只有一個(gè)集合參數(shù),那么foreach中的colloection標(biāo)簽隨便寫(xiě)什么都是可以的。

mapper.xml傳參及其使用

@Param(“name”):用來(lái)給xml準(zhǔn)確獲取參數(shù)使用

mapper.xml傳參

1.傳多個(gè)參數(shù)

mapper層方法:

List<Files> test(String name ,Integer size);

//xml:#{0}代表接收的是 dao 層中的第一個(gè)參數(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 層中的第一個(gè)參數(shù),#{1}代表 dao 層中第二
//參數(shù),更多參數(shù)一致往后加即可
<select id="test" resulttype="files">
    select * from ss_files where name = #{0} and size=#{1}
</select>

2.鍵值對(duì)傳參

Service層:
    Map paramMap=new hashMap();
    paramMap.put(“name ”, value);
    paramMap.put(“size”,value);
    
mapper層方法:
List<Files> test(Map paramMap);
//直接通過(guò)屬性名獲取值
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);
//通過(guò).點(diǎn)屬性名的方式獲取值
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  序號(hào):index 集合:collection  分割符:separator
//-----------------------------數(shù)組
mapper層方法:
List<Files> test(@Param("arrayIds") Integer[] arrayIds);
//通過(guò).點(diǎn)屬性名的方式獲取值
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);
//通過(guò).點(diǎn)屬性名的方式獲取值
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.傳對(duì)象參數(shù)

mapper層方法:

List<Files> test(Files file);
//直接通過(guò)屬性名獲取值
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);
//通過(guò).點(diǎn)屬性名的方式獲取值
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í)傳多個(gè)參數(shù)和對(duì)象

//使用注釋
mapper層方法:
List<Files> test(@Param(value="file") Files file,@Param(value="size") Integer size);
//通過(guò).點(diǎn)屬性名的方式獲取值
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ū)別:

兩者都是表示查詢(xún)結(jié)果集與java對(duì)象之間的一種關(guān)系,處理查詢(xún)結(jié)果集,映射到j(luò)ava對(duì)象。

  • resultMap:將查詢(xún)結(jié)果集中的列一一映射到bean對(duì)象的各個(gè)屬性,映射的查詢(xún)結(jié)果集中的列標(biāo)簽可以根據(jù)需要靈活變化。
  • resultType:的是bean中的對(duì)象類(lèi),必須保證查詢(xún)結(jié)果集中的屬性 和 bean對(duì)象類(lèi)中的屬性是一一對(duì)應(yīng),大小寫(xiě)不敏感,但是有限制。

2.parameterMap(不推薦) & parameterType

parameterMap和resultMap類(lèi)似,表示將查詢(xún)結(jié)果集中列值的類(lèi)型一一映射到j(luò)ava對(duì)象屬性的類(lèi)型上,在開(kāi)發(fā)過(guò)程中不推薦這種方式。

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

湾仔区| 屏边| 蓝田县| 和田市| 开封县| 荥阳市| 巴马| 西宁市| 哈巴河县| 元朗区| 静宁县| 巧家县| 沙河市| 炉霍县| 武威市| 柘城县| 开平市| 柘荣县| 丁青县| 大竹县| 肇东市| 思茅市| 寿宁县| 改则县| 肃南| 宜兴市| 天全县| 读书| 张家界市| 濮阳市| 常德市| 仁布县| 柳江县| 绥中县| 沾化县| 安多县| 长寿区| 习水县| 罗源县| 浮梁县| 新兴县|