代码演示

图:from tuser、sql语句 - 注入 的流程。

图:from tuser、sql语句 - 注入 的流程。

1
2
3
4
5
6
7
8
9
10
11
12
1.在接口中使用@Param注解
public User login(@Param("userCode") String userCode,
@Param("password") String password);

2.同时在sql语句中使用注解内重定义名
<!-- mapper动态代理 - 传入多个参数数据 - 不可使用parameterType
集合mybatis参数注解实现-->
<select id="login" resultType="user">
select userId,userCode,name
from tuser
where userCode= #{userCode} and password= #{password}
</select>
1
2
3
4
5
6
7
8
9
10
11
Java Bean传参法     #{}里面的名称对应的是User类里面的成员属性。
<!-- select查询 - 返回值
sql语句 - 注入参数数据
1: #{bean对象属性}(无需写bean对象名)
-->

<select id="loginUser" parameterType="User" resultType="user">
select userId, userCode, name
from tuser
where userCode = #{userCode} and password = #{password}
</select>
1
2
3
4
5
6
7
8
9
10
11
map传参法

public User selectUser(Map<String, Object> params);

<select id="selectUser" parameterType="java.util.Map" resultMap="UserResultMap">
select * from user
where user_name = #{userName} and dept_id = #{deptId}
</select>

#{}里面的名称对应的是Map里面的key名称。
这种方法适合传递多个参数,且参数易变能灵活传递的情况。
1
顺序传参   where user_name = #{0} and dept_id = #{1}    (不推荐)