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

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

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

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

場(chǎng)景1

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

場(chǎng)景2

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

場(chǎng)景3

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

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

mapper.xml傳參及其使用

@Param(“name”):用來給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);
//直接通過屬性名獲取值
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);
//通過.點(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);
//通過.點(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);
//通過.點(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);
//直接通過屬性名獲取值
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);
//通過.點(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);
//通過.點(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ū)別:

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

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

2.parameterMap(不推薦) & parameterType

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

總結(jié)

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

相關(guān)文章

最新評(píng)論

理塘县| 江门市| 安远县| 乐山市| 沐川县| 昌乐县| 长顺县| 汕头市| 汶上县| 民和| 平和县| 保定市| 秀山| 峡江县| 敦煌市| 象山县| 息烽县| 太原市| 龙门县| 龙陵县| 英超| 伊川县| 永春县| 阳高县| 乐亭县| 乌兰浩特市| 雅江县| 肇庆市| 旌德县| 潼关县| 高台县| 营山县| 万全县| 西乡县| 乐平市| 平遥县| 瑞昌市| 宁夏| 顺昌县| 永登县| 庄河市|