mybatis的mapper參數傳遞


簡單參數傳遞

簡單參數傳遞是指:

  • 傳遞單個基本類型參數,數字類型、String
  • 傳遞多個基本類型參數

parameterType 屬性可以省略;

傳遞單個基本類型參數

  •  SQL語句中參數的引用名稱並不需要和接口中的參數名稱相同,如selectActorById元素的where語句改為  where actor_id=#{abc} 也能夠得到正確的結果;
  Actor selectActorById(Long id);

 

    <select id="selectActorById"  resultType="canger.study.chapter04.bean.Actor">
        select actor_id as id, first_name as firstName ,last_name as lastName
        from actor
        where actor_id=#{abc} </select>

 

Actor actor = mapper.selectActorById(1L)

 

傳遞布爾類型參數

待續

傳遞多個基本類型參數

  •  使用map進行參數傳遞
  Boolean insertActorByMap(Map map);

 

    <insert id="insertActorByMap" parameterType="map">
        insert into actor(first_name,last_name) values (#{firstName},#{lastName})
    </insert>
Map<String,String> map = new HashMap<String, String>(){
    {
        put("firstName","James");
        put("lastName","Harden");
    }
};
Boolean result = mapper.insertActorByMap(map);
System.out.println(result);
  •  通過param1、param2進行多參數引用(此時接口方法中的參數可以使用任意名稱,SQL語句中使用 param1、param2進行引用)
  Boolean insertActorByString(String var1,String var2);
    <insert id="insertActorByString">
        insert into actor(first_name,last_name) values (#{param1},#{param2})
    </insert>
    Boolean result = mapper.insertActorByString("James", "Harden");
    System.out.println(result);
  • 通過命名參數進行引用,通過使用@Parma注解,可以在SQL語句中使用命名參數引用,注意命名參數區分大小寫
  Boolean insertActorByParamString(@Param("firstName")String var1, @Param("lastName")String var2);
    <insert id="insertActorByParamString">
        insert into actor(first_name,last_name) values (#{firstName},#{lastName})
    </insert>
    Boolean result = mapper.insertActorByParamString("James", "Harden");
    System.out.println(result)
  •  命名參數和非命名參數混合使用,此時非命名參數只能采用 param1,param2...的方式使用,命名參數即能采用@Param指定名字也能使用 param1,param2...的方式進行引用
Boolean insertActorByMixedString(String var1, @Param("lastName")String var2);

 

    <!--使用命名參數和非命名參數傳遞-->
    <insert id="insertActorByMixedString">
        insert into actor(first_name,last_name) values (#{param1},#{lastName})
    </insert>
或者
<!--使用命名參數和非命名參數傳遞--> <insert id="insertActorByMixedString"> insert into actor(first_name,last_name) values (#{param1},#{param2}) </insert>

 自定義類型參數傳遞

  • 傳遞單個自定義類型參數,定義用於傳遞參數的Bean,如 id=“insertActor”語句中的 canger.study.chapter04.bean.Actor,可以直接在SQL語句中使用Bean的屬性名;

  需要注意的是,如果此時采用命名參數(如@Param("actor"))傳遞單個自定義類型參數,則不能直接引用屬性名,而需要采用級聯引用(actor.firstName param1.firstName

Boolean insertActor(Actor actor);
    <!--參數Bean-->
    <insert id="insertActor" parameterType="canger.study.chapter04.bean.Actor">
        insert into actor(first_name,last_name) values (#{firstName},#{lastName})
    </insert>
Actor actor = new Actor("James","Harden");
Boolean  result = mapper.insertActor(actor);
  • 傳遞自定義參數和基本參數

  這種情況和傳遞多個基本參數沒有什么打的區別,唯一需要注意的是需要使用級聯引用,才能使用自定義參數屬性的值

集合類型參數傳遞

  • 傳遞單個非命名集合參數

  此時的引用規則如下:

    • Set 類型參數的引用名為 collection
    • List 類型參數的引用名為 collection 或 list
    • Array 類型參數的引用名為 array
    List<Actor> selectActorByIdList( List<Long> list);
    List<Actor> selectActorByIdSet( Set<Long> set);
    List<Actor> selectActorByIdArray( Long[] array);

 

    <select id="selectActorByIdList"  resultType="canger.study.chapter04.bean.Actor">
        select actor_id as id, first_name as firstName ,last_name as lastName
        from actor
        where actor_id  in
        <foreach collection="list" item="actorId" index="index"
                 open="(" close=")" separator=",">
            #{actorId}
        </foreach>
    </select>

    <select id="selectActorByIdSet"  resultType="canger.study.chapter04.bean.Actor">
        select actor_id as id, first_name as firstName ,last_name as lastName
        from actor
        where actor_id  in
        <foreach collection="collection" item="actorId" index="index"
                 open="(" close=")" separator=",">
            #{actorId}
        </foreach>
    </select>


    <select id="selectActorByIdArray"  resultType="canger.study.chapter04.bean.Actor">
        select actor_id as id, first_name as firstName ,last_name as lastName
        from actor
        where actor_id  in
        <foreach collection="array" item="actorId" index="index"
                 open="(" close=")" separator=",">
            #{actorId}
        </foreach>
    </select>
 List<Actor> actors = mapper.selectActorByIdList(asList(1L, 2L, 3L));
 System.out.println(actors);
 actors =mapper.selectActorByIdSet(new HashSet<Long>(){
      {
           add(4L);
           add(6L);
           add(5L);
       }
 });
 mapper.selectActorByIdArray(new Long[]{7L,8L,9L});
  • 單個命名集合參數傳遞

  此時默認名稱 collection、list、array均失效,只能使用指定的名稱或param1進行引用

  • 引用集合中的單個元素,通過索引操作符 []進行引用
List<Actor> selectActorByIdList( @Param("actorList") List<Long> list);

 

    <select id="selectActorByIdList"  resultType="canger.study.chapter04.bean.Actor">
        select actor_id as id, first_name as firstName ,last_name as lastName
        from actor
        where actor_id  = #{actorList[0]}
    </select>

 

  • 集合參數和其他參數一起使用

  無特殊之處


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM