1. 将参数封装成一个域对象,底层 HashMap 还是一个:
1 User user=new User(); 2 user.setUserName("taiyo"); 3 user.setPassword("123"); 4 checkUser(user);//为返回值类型为User
<select id="selectByUsernameAndPassword" resultType="user" parameterType="user"> select * from user where username = #{username} and password = #{password}; </select>
2. 使用 HashMap 封装参数:
HashMap<String, Object> map = new HashMap<String, Object>(); map.put("username", "taiyo"); map.put("password", "123"); checkUser(map);
<select id="selectByUsernameAndPassword" resultType="user" parameterType="Map"> select * from user where username = #{username} and password = #{password}; </select>
3. 在 UserMapper.java 接口中定义方法时在方法入参前加上 @Param:
// interface UserMapper.java User selectByUsernameAndPassword(@Param("username") String username, @Param("password") String password);
1 checkUser("taiyo", "123");
<select id="selectByUsernameAndPassword" resultType="user"> select * from user where username = #{username} and password = #{password}; </select>
MyBatis - parameterType - 多个参数
原文:https://www.cnblogs.com/taiyo/p/13190129.html