# mybatis获取参数值的两种方式
# ${}本质是字符串拼接
select * from t_user where username = ${username}
# 转化后(假设参数值是admin)
select * from t_user where username = admin # 显然错误,字符串要加''
# 正确写法
select * from t_user where username = '${username}'
# 转化后
select * from t_user where username = 'admin'
# #{}本质是占位符赋值
select * from t_user where username = #{username}
# 转化后
select * from t_user where username = ?
单个参数
// POJO类型: 参数占位符名称xxx和属性名一致
// Map集合: 参数占位符名称xxx和键名一致
// int,String等基本类型: #{xxx},参数占位符名称xxx可以任意写
// Collection: mybatis封装为Map集合, map.put("collection",collection集合)
// List: mybatis封装为Map集合, map.put("collection",list集合)或map.put("list",list集合)
// Array: mybatis封装为Map集合, map.put("array",数组)
多个参数
有多个参数的话,mybatis会将参数封装到Map集合中
Map集合键的名字,由mybatis给默认值
// map集合的大小是参数个数的两倍
map.put("arg0",参数值1)
map.put("arg1",参数值2)
map.put("param1",参数值1)
map.put("param2",参数值2)
<!--对于这样的一个接口方法-->
List<User> selectAll(String username, String password);
<select id="selectAll" resultType="User">
<!--ok-->
select * from user where username = #{arg0} and password = #{arg1};
<!--error-->
<!--Cause: org.apache.ibatis.binding.BindingException:-->
<!--Parameter 'username' not found. Available parameters are [arg1, arg0, param1, param2]-->
select * from user where username = #{username} and password = #{password};
</select>
@Param注解,使用指定的键名,将mybatis默认的arg键名替换掉
<!--自定义map集合中的键名-->
<!--解决sql中占位符 只能使用arg0和param1-->
List<User> selectAll(@Param("username") String username, @Param("password") String password);
<select id="selectAll" resultType="User">
select * from user where username = #{username} and password = #{password};
</select>