简单参数传递
简单参数传递是指:
- 传递单个基本类型参数,数字类型、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>
Mapmap = new HashMap (){ { 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);
<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
ListselectActorByIdList( List list); List selectActorByIdSet( Set set); List 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>
Listactors = mapper.selectActorByIdList(asList(1L, 2L, 3L)); System.out.println(actors); actors =mapper.selectActorByIdSet(new HashSet (){ { add(4L); add(6L); add(5L); } }); mapper.selectActorByIdArray(new Long[]{7L,8L,9L});
- 单个命名集合参数传递
此时默认名称 collection、list、array均失效,只能使用指定的名称或param1进行引用
- 引用集合中的单个元素,通过索引操作符 []进行引用
ListselectActorByIdList( @Param("actorList") List 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>
- 集合参数和其他参数一起使用
无特殊之处