Bootstrap

MyBatis获取参数值的五种情况

MyBatis获取参数值的两种方式:${}和#{}
${}本质:字符串拼接
#{}本质:占位符赋值

MyBatis获取参数值的各种情况:

1. mapper接口方法的参数为单个的字面量类型:

可以通过${}和#{}以任意的名称获取参数值,但是需要注意${}的单引号问题

ParameterMapper接口:

User getUserByUsername(String username);

ParameterMapper.xml:

<select id="getUserByUsername" resultType="User">
    <!-- select * from t_user where username = #{username};-->
    select * from t_user where username = '${username}';
</select>

ParameterMapperTest:

@Test
public void testGetUserByUsername() {
    SqlSession sqlSession = SqlSessionUtils.getSqlSession();
    ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
    User user = mapper.getUserByUsername("admin");
    System.out.println(user);
}

 

2. mapper接口方法的参数为多个时:

此时MyBatis会将这些参数放在一个map集合中,以两种方式进行存储

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

b>以param1,param2...为键,以参数为值

一次只需要通过#{}和${}以键的方式访问即可,但是需要注意${}的单引号问题

ParameterMapper接口:

User checkLogin(String username, String password);

ParameterMapper.xml:

<select id="checkLogin" resultType="User">
    <!-- select * from t_user where username = '${arg0}' and password = '${arg1}';-->
    select * from t_user where username = #{arg0} and password = #{arg1};
</select>

ParameterMapperTest:

@Test
public void testCheckLogin() {
    SqlSession sqlSession = SqlSessionUtils.getSqlSession();
    ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
    User user = mapper.checkLogin("admin", "123456");
    System.out.println(user);
}

 

3. 若mapper接口方法的参数有多个时,可以手动将这些参数放在一个map中存储:

只需要通过#{}和${}以键的方式访问值即可,但是需要注意${}的单引号问题

ParameterMapper接口:

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

ParameterMapper.xml:

<select id="checkLoginByMap" resultType="User">
    select *
    from t_user
    where username = #{username}
      and password = #{password};
</select>

ParameterMapperTest:

@Test
public void testCheckLoginByMap() {
    SqlSession sqlSession = SqlSessionUtils.getSqlSession();
    ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
    HashMap<String, Object> map = new HashMap<>();
    map.put("username", "admin");
    map.put("password", "123456");
    User user = mapper.checkLoginByMap(map);
    System.out.println(user);
}

 

4. mapper接口方法的参数是实体类类型的参数:

只需要通过#{}和${}以属性值的方式访问值即可,但是需要注意${}的单引号问题

ParameterMapper接口:

int insertUser(User user);

ParameterMapper.xml:

<insert id="insertUser">
    insert into t_user
    values (null, #{username}, #{password}, #{age}, #{sex}, #{email});
</insert>

ParameterMapperTest:

@Test
public void testInsertUser() {
    SqlSession sqlSession = SqlSessionUtils.getSqlSession();
    ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
    int num = mapper.insertUser(new User(null, "李四", "789789", 25, "男", "[email protected]"));
    System.out.println(num);
}

 

5. 使用@Param注解命名参数:

此时MyBatis会将这些参数放在一个map集合中,以两种方式进行存储

a>以@Param注解的值为键,以参数为值

b>以param1,param2...为键,以参数为值

因此只需要通过#{}和${}以键的方式访问值即可,但是需要注意${}的单引号问题

ParameterMapper接口:

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

ParameterMapper.xml:

<!--User checkLoginByParam(@Param("username") String username, @Param("password") String password);-->
<select id="checkLoginByParam">
    select *
    from t_user
    where username = #{username}
      and password = #{password};
</select>

ParameterMapperTest:

@Test
public void testCheckLoginByParam() {
    SqlSession sqlSession = SqlSessionUtils.getSqlSession();
    ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
    User user = mapper.checkLoginByParam("admin", "123456");
    System.out.println(user);
}

总结:

上面的五种情况想要记清楚还是比较难的,所以我们可以分为两种情况:

一种是在参数中加上@Param

一种是使用属性值

;