Mybatis 传参的各种姿势,看这一篇就足够
首先大家都清楚,Mybatis里面传参方式分别有使用 #{} 和 ${}。
对于使用$符存在安全问题的,该篇不做分析和介绍(其实就是如果传参的话,使用$需要手动拼接‘ ' ,这就存在注入的风险)
接下来,进入正题,通过简单举例介绍,
#{}
第一种情形,传入单个参数 userId:
service层:
@Override
public User getUserInfo(Integer userId) {
User user = userMapper.getUserInfo(userId);
//省略 业务代码...
return user;
}
mapper层:
User getUserInfo(Integer userId);
mapper.xml:
<!--查询-->
<select id="getUserInfo" resultType="com.demo.elegant.pojo.User">
select userId
from users
where userId=#{userId};
</select>
--------------------------------------------------------------------------------------
第二种情况,传入多个参数 userId,sex 使用索引对应值:
注意mapper层和xml层!
service层:
@Override
public User getUserInfo(Integer userId,String sex) {
User user = userMapper.getUserInfo(userId,sex);
//省略 业务代码...
return user;
}
mapper层:
User getUserInfo(Integer userId,String sex);
mapper.xml:
<!--查询-->
<select id="getUserInfo" resultType="com.demo.elegant.pojo.User">
select userId
from users
where userId=#{0} and sex=#{1};
</select>
--------------------------------------------------------------------------------------
第三种情形,传入多个参数 userId,sex 使用注解@Param :
service层:
@Override
public User getUserInfo(Integer userId,String sex) {
User user = userMapper.getUserInfo(userId,sex);
//省略 业务代码...
return user;
}
mapper层:
User getUserInfo(@Param("userId")Integer userId,@Param("sex")String sex);
mapper.xml:
<!--查询-->
<select id="getUserInfo" resultType="com.demo.elegant.pojo.User">
select userId
from users
where userId=#{userId} and sex=#{sex};
</select>
--------------------------------------------------------------------------------------
第四种情形,传入多个参数 使用User实体类传入,
service层:
@Override
public User getUserInfo(User user) {
User userInfo = userMapper.getUserInfo(user);
//省略 业务代码...
return userInfo;
}
mapper层:
User getUserInfo(User User);
mapper.xml:
<!--查询-->
<select id="getUserInfo" parameterType="User" resultType="com.demo.elegant.pojo.User">
select userId
from users
where userId=#{userId} and sex=#{sex};
</select>
--------------------------------------------------------------------------------------
第五种情形,传入多个参数, 使用Map类传入,
service层:
@Override
public User getUserInfo(Map map) {
User user = userMapper.getUserInfo(map);
//省略 业务代码...
return user;
}
mapper层:
User getUserInfo(Map map);
mapper.xml层:
<!--查询-->
<select id="getUserInfo" parameterType="Map" resultType="com.demo.elegant.pojo.User">
select userId
from users
where userId=#{userId} and sex=#{sex};
</select>
--------------------------------------------------------------------------------------
第六种情形,传入多个参,使用 map封装实体类传入,
这种情况其实使用场景比较少,因为上面的各种姿势其实已经够用了
service层:
@Override
public User getUserInfo1(Integer userId,String sex) {
User userInfo = new User(userId,sex);
Map<String,Object> map=new HashMap<String,Object>();
map.put("user",userInfo);
User userResult= userMapper.getUserInfo(map);
//省略 业务代码...
return userResult;
}
mapper层:
User getUserInfo(Map map);
mapper.xml:
<!--查询-->
<select id="getUserInfo" parameterType="Map" resultType="com.demo.elegant.pojo.User">
select userId
from users
where userId=#{userInfo.userId} and sex=#{userInfo.sex};
</select>
--------------------------------------------------------------------------------------
第七种情形,即需要传入实体类,又需要传入多个单独参,使用注解@Param ,
service层:
@Override
public User getUserInfo(User user,Integer age) {
User userResult = userMapper.getUserInfo(user,age);
//省略 业务代码...
return userResult;
}
mapper层:
User getUserInfo(@Param("userInfo") User user,@Param("age") Integer age);
mapper.xml:
<!--查询-->
<select id="getUserInfo" resultType="com.demo.elegant.pojo.User">
select userId
from users
where userId=#{userInfo.userId} and sex=#{userInfo.sex} and age=#{age};
</select>
--------------------------------------------------------------------------------------
${}
使用这个的时候,只需要注意,如果是传递字段名或者表名,是直接做参数传入即可,
但是如果作为sql'语句里面使用的值, 记得需要手动拼接 ' ' 号。
例如, 传入单个参数 sex:
service层:
@Override
public User getUserInfo(String sex) {
sex="'"+sex+"'";
User user = userMapper.getUserInfo(sex);
//省略 业务代码...
return user;
}
mapper层:
User getUserInfo(String sex);
mapper.xml:
<!--查询-->
<select id="getUserInfo" resultType="com.demo.elegant.pojo.User">
select userId
from users
where sex=${sex};
</select>
多个参数,那也就是使用注解@Param取名字解决即可。