五、Mybatis参数传递

Mybatis参数传递

概述

​ 参数传递在Mybatis中也是非常重要的。存在诸多情况,如下所介绍的单个参数,多个参数,对象,集合和数组等。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1.单个的参数Mybatis不会做特殊处理
#{这里随便写什么都可以} 它都能把这里面的值取到
2.传入对象POJO(普通的java类)..
#{对象的属性名称}
3.多个参数。Mybatis会做特殊处理。会把传入的参数自动封装成Map类型
Map 的key值就是从param1...paramN ..
map.put("param1",name)
map.put("param2,id")
@param("name") 可以使用这个注解 来自定义Map封装数据的key值。
4.直接传入Map
5.Collection(集合)类型(List,Set) ,数组。
Mybatis也会做特殊处理。。
如果是List或者Set 封装到map中
如果是数组
map.put("array",你传入的数组)

单个参数

Mapper接口的方法:

1
List<User> findUsersByUsername(String username);

Mapper.xml文件:

1
2
3
4
5
6
7
8
<select id="findUsersByUsername" resultType="user">
select
<include refid="Base_Column_List"/>
from tb_user
<where>
username = #{value}
</where>
</select>

注意:

​ 其中方法名和id一致,#{}中的参数名与方法中的参数名k可以不一致, 映射结果的时候,select 后的字段列表要和bean中的属性名一致, 如果不一致的可以用 as 来补充,特殊的也可以再mybatis中启用驼峰,比如create_time 和 createTime;

多个参数

方法1:使用方法参数下标

Mapper接口的方法:

1
List<User> findUsersByUsernamePassword1(String username, String password);

对应的Mapper.xml:

1
2
3
4
5
6
7
8
9
<!--方法1.1-->
<select id="findUsersByUsernamePassword1" resultType="user" >
select * from tb_user where username = #{arg0} and password = #{arg1}
</select>

<!--方法1.2-->
<select id="findUsersByUsernamePassword1" resultType="user" >
select * from tb_user where username = #{0} and password = #{1}
</select>

注意:

有 些版本在使用上有不同,我使用的是 org.mybatis:mybatis:3.5.2版本,需要使用方法1.1。

方法2:使用注解 @Param 别名

Mapper接口的方法:

1
2
List<User> findUsersByUsernamePassword2(@Param("username") String username,
@Param("password") String password);

对应的Mapper.xml文件:

1
2
<select id="findUsersByUsernamePassword2" resultType="user">
select * from tb_user where username = #{username} and password = #{password}</select>

方法3:使用实体

Mapper接口的方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
List<User> findUsersByUsernamePassword3(User user);

// 调用方式
@Test
public void testFindUsersByUsernamePassword3(){
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = new User();
user.setUsername("admin");
user.setPassword("admin");
List<User> users = mapper.findUsersByUsernamePassword3(user);
System.out.println(users);
sqlSession.close();
}

Mapper.xml文件:

1
2
3
<select id="findUsersByUsernamePassword3" resultType="user" parameterType="user">
select * from tb_user where username = #{username} and password = #{password}
</select>

方法4:使用Map

Mapper接口的方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*根据username和password查询*/
List<User> findUsersByUsernamePassword(Map map);

//-----调用方式
@Test
public void testFindUsersByUsernamePassword() {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
Map<String, Object> map = new HashMap<>();
map.put("username", "admin");
map.put("password", "admin");
List<User> users = mapper.findUsersByUsernamePassword(map);
System.out.println(users);
sqlSession.close();
}

/*
其中Map是mybatis自己配置好的直接使用就行。map中key的名字就是在#{}使用的个,map如何封装就不用了管了
*/

对应Mapper.xml文件:

1
2
3
<select id="findUsersByUsernamePassword" resultType="user" >
select * from tb_user where username = #{username} and password = #{password}
</select>

注意:

​ 这里使用Map来读取参数,需要注意的是,使用Map可以像实体那样访问,这里的key就像实体的属性名。

方法5:使用实体和@Param

Mapper接口的方法:

1
List<User> findUsersByUsernamePassword4(@Param("user") User user);

对应Mapper.xml文件:

1
2
3
4
<select id="findUsersByUsernamePassword4" resultType="user">
select * from tb_user where username = #{user.username}
and password = #{user.password}
</select>

注意:

​ 如果使用注解结合实体的方式来传参数的话,那么就需要使用实体.属性名的方式来读取参数。

方法6:使用Map和@Param

Mapper接口的方法:

1
List<User> findUsersByUsernamePassword5(@Param("map") Map map);

对应的Mapper.xml文件:

1
2
3
4
<select id="findUsersByUsernamePassword5" resultType="user">
select * from tb_user where username = #{map.username}
and password = #{map.password}
</select>

使用方法和方法5类似,不再赘述。

可迭代对象

注意:如果使用接口Collection , 在Mapper.xml文件中,直接使用 collection.

传递List

前面的动态SQL中使用到了传递List的情况,这小节主要把几种迭代对象都使用一遍。

Mapper接口方法:

1
List<User> findUsersByIds(List<Integer> id);

对应的Mapper.xml文件:

1
2
3
4
5
6
7
8
9
10
<!--select * from tb_user WHERE id in ( ? , ? , ? ) -->
<select id="findUsersByIds" resultType="user">
select * from tb_user
<where>
id in
<foreach collection="list" item="ite" open="(" separator="," close=")">
#{ite}
</foreach>
</where>
</select>

注意:List集合可以使用list,也可以使用collection。

使用Set

Mapper接口的方法:

1
List<User> findUsersByIds1(Set<Integer> ids);

对应的Mapper.xml文件

1
2
3
4
5
6
<select id="findUsersByIds2" resultType="user">
select * from tb_user where id in
<foreach collection="collection" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>

注意:Set集合没有像List那样,Set只能使用collection。

传递数组

Mapper接口的方法:

1
List<User> findUsersByIds2(Integer[] ids);

对应的Mapper.xml文件

1
2
3
4
5
6
7
<select id="findUsersByIds1" resultType="user">
select * from tb_user
where id in
<foreach collection="array" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>

传递Map

使用Map来做保存操作:

1
void saveUserByMap1(Map<String,Object> map);

对应的Mapper.xml文件:

1
2
3
4
<select id="saveUserByMap1">
insert into tb_user (user_id,username,password) values
(#{user_id},#{username},#{password});
</select>

单元测试:

1
2
3
4
5
6
7
8
9
10
@Test
public void testSaveUserByMap1(){
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
Map<String,Object> map = new HashMap<>();
map.put("user_id","root");
map.put("username","root");
map.put("password","root");
mapper.saveUserByMap1(map);
sqlSession.close();
}

传递List-Map

这里使用List和Map结合使用实现批量操作

Mapper接口文件的方法:

1
void saveUserByMap(List<Map<String,Object>> listMap);

对应的Mapper.xml文件:

1
2
3
4
5
6
7
<!--insert into tb_user (user_id,username,password)values (?,?,?) , (?,?,?) -->
<select id="saveUserByMap">
insert into tb_user (user_id,username,password)values
<foreach collection="collection" item="item" separator=",">
(#{item.userId},#{item.username},#{item.password})
</foreach>
</select>

单元测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@Test
public void testSaveUserByMap(){
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<Map<String,Object>> list = new ArrayList<>();
Map<String,Object> map = new HashMap<>();
map.put("userId","mybatis1");
map.put("username","mybatis1");
map.put("password","mybatis1");

Map<String,Object> map1 = new HashMap<>();
map1.put("userId","mybatis2");
map1.put("username","mybatis2");
map1.put("password","mybatis2");
list.add(map);
list.add(map1);
mapper.saveUserByMap(list);
sqlSession.close();
}

源码地址:

https://gitee.com/ooyhao/JavaRepo_Public/tree/master/Mybatis

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×