0
点赞
收藏
分享

微信扫一扫

mybatis - 获取参数值

一、获取参数值

1.只有一个参数情况

<delete id="deleteUserById"> 
delete from t_user where id = #{id}
delete from t_user where id = '${id}'
</delete>

${} 和 #{} 都可以用来获取参数值,但是两种方式对于底层sql拼接不一样。

${} : 底层用的是字符串拼接的方式来注入sql语句,使用${}要加上单引号

#{} : 底层用的是占位符的方式来注入sql语句

2.有多个参数的情况

<select id="getUser" resultType="user">
select * from t_user where username = #{arg0} and password = #{arg1}
select * from t_user where username = '${param0}' and password = '${param1}'
</select>

以arg0,arg1...为键,参数为值

以param0,param1...为键,参数为值

3.有多种参数的情况,使用map

mapper接口

User checkUserByMap(Map<String,Object> map);

测试类

LinkedHashMap map = new LinkedHashMap<String,Object>();
map.put("username","admin");
map.put("password","123456");
usermapper.checkUserByMap(map)

映射文件

<select id="checkUserByMap" resultType="user">
select * from t_user where username = #{username} and password = #{password}
select * from t_user where username = '${param0}' and password = '${param1}'
</select>

以map中的key为键,参数为值

4.参数为实体类的情况

<insert id="addUser">
insert into t_user values(#{id},#{username},#{password})
</insert>

5.使用注解来获取参数值   (用来处理一个或者多个参数的情况)

User checkUserByMap(@Param("username") String username, @Param("password") String password);

<select id="checkUserByAnnotaion" resultType="user">
select * from t_user where username = #{username} and password = #{password}
</select>

以Param注解的值...为键,参数为值

以param0,param1...为键,参数为值

举报

相关推荐

0 条评论