// 调用方式 @Test publicvoidtestFindUsersByUsernamePassword3(){ UserMapper mapper = sqlSession.getMapper(UserMapper.class); User user = new User(); user.setUsername("admin"); user.setPassword("admin"); List<User> users = mapper.findUsersByUsernamePassword3(user); System.out.println(users); sqlSession.close(); }
Mapper.xml文件:
1 2 3
<selectid="findUsersByUsernamePassword3"resultType="user"parameterType="user"> select * from tb_user where username = #{username} and password = #{password} </select>
List<User> findUsersByUsernamePassword4(@Param("user") User user);
对应Mapper.xml文件:
1 2 3 4
<selectid="findUsersByUsernamePassword4"resultType="user"> select * from tb_user where username = #{user.username} and password = #{user.password} </select>
<selectid="findUsersByUsernamePassword5"resultType="user"> select * from tb_user where username = #{map.username} and password = #{map.password} </select>
<!--select * from tb_user WHERE id in ( ? , ? , ? ) --> <selectid="findUsersByIds"resultType="user"> select * from tb_user <where> id in <foreachcollection="list"item="ite"open="("separator=","close=")"> #{ite} </foreach> </where> </select>
注意:List集合可以使用list,也可以使用collection。
使用Set
Mapper接口的方法:
1
List<User> findUsersByIds1(Set<Integer> ids);
对应的Mapper.xml文件
1 2 3 4 5 6
<selectid="findUsersByIds2"resultType="user"> select * from tb_user where id in <foreachcollection="collection"item="item"open="("separator=","close=")"> #{item} </foreach> </select>
注意:Set集合没有像List那样,Set只能使用collection。
传递数组
Mapper接口的方法:
1
List<User> findUsersByIds2(Integer[] ids);
对应的Mapper.xml文件
1 2 3 4 5 6 7
<selectid="findUsersByIds1"resultType="user"> select * from tb_user where id in <foreachcollection="array"item="item"open="("separator=","close=")"> #{item} </foreach> </select>
传递Map
使用Map来做保存操作:
1
voidsaveUserByMap1(Map<String,Object> map);
对应的Mapper.xml文件:
1 2 3 4
<selectid="saveUserByMap1"> insert into tb_user (user_id,username,password) values (#{user_id},#{username},#{password}); </select>