在MyBatis中可以用以下的方式来传递多个参数
http://my.oschina.net/ydsakyclguozi/blog/147950
1. 用java.util.Map来传递, Code 如下:
1
2
3
4
5
6
7
|
public
List<User> getAllUsersByUserName(String username,
int
start,
int
limit){
Map<String,Object> params =
new
HashMap<String, Object>(
3
);
params.put(
"username"
,username);
params.put(
"start"
,start);
params.put(
"limit"
,limit);
return
userMapper.getAllUsersByUserName(params);
}
|
对应的XXMapper.xml文件中如下:
1
2
3
|
<
select
id
=
"getAllUsersByUserName"
parameterType
=
"map"
resultType
=
"User"
>
SELECT u.* FROM User u WHERE u.username LIKE #{username} '%' LIMIT #{start}, #{limit}
</
select
>
|
2. 用JavaBean的方式来传递, Code如下:
1
2
3
4
5
|
public
List<User> getUsersByUserName(String username){
User user =
new
User();
user.setUsername(username);
return
userMapper.getUserByUsername(user);
}
|
对应的XXMapper.xml文件中如下:
1
2
3
|
<
select
id
=
"getAllUsersByUserName"
parameterType
=
"User"
resultType
=
"User"
>
SELECT u.* FROM User u WHERE u.username = #{username}
</
select
>
|
3.使用@Param这个注解,对应的XXMapper.xml文件同上
1
|
List<User> getAllUsersByUserName(
@Param
(
"username"
)String name,
@Param
(
"start"
)Integer start,
@Param
(
"limit"
)Integer limit);
|