MyBatis方法传入参数的获取
1.通过索引获取
1.#{arg0},#{arg1}的形式,方法参数的传入存放在一个数组中
2.#{param1},#{param2}.....依次获得方法中的参数
//通过#{arg0}的形式或的方法传入参数的第一个参数
public User login(String name,String password);
//通过#{param1}的形式或的方法传入参数的第一个参数
public User login1(String name,String password);
<select id="login" resultType="user">
select * from t_user where name =#{arg0} and password =#{arg1};
</select>
<select id="login1" resultType="user">
select * from t_user where name =#{param1} and password =#{param2};
</select>
2.通过注解给方法参数起别名
@Param(参数别名)
//通过注解为方法的参数起别名用于获取
public User login2(@Param("name") String name,@Param("password") String password);
<select id="login2" resultType="user">
select * from t_user where name =#{name} and password =#{password};
</select>
3.通过实例化对象获得方法参数
方法的参数类型是一个实例化对象,通过#{属性名}获得对应的属性值
<select id="login3" resultType="user">
select * from t_user where name =#{name} and password =#{password};
</select>
//传入一个实例化对象通过#{属性名}的形式获得对应的值
public User login3(User user);
4.通过传入一个map对象获得方法参数
方法的参数数据类型是Map<String,Object>通过map的key值获得对应的value值
//传入一个map对象,通过#{key值}获得对应的value值
public User login4(Map<String,Object> map);
<select id="login4" resultType="user">
select * from t_user where name =#{n} and password =#{p};
</select>
总结:
方法参数少,可以用注解的方法,如果参数多余5个就要想办法将其封装成一个对象,或者封装成一个map进行数据的传递