在上篇的基础上修改:
IUserOperation增加public List<User> selectUsers(String username);
user.xml增加:
<!-- 为了返回list 类型而定义的returnMap -->
<resultMap type="User" id="resultListUser">
<id column="id" property="id" />
<result column="username" property="username" />
<result column="password" property="password" />
</resultMap>
<select id="selectUsers" parameterType="string" resultMap="resultListUser">
select * from user where username like #{username}
</select>
测试代码:
public void testGetUserList() {
SqlSession session = sqlSessionFactory.openSession();
try {
IUserOperation userOperation = session.getMapper(IUserOperation.class);
List<User> users = userOperation.selectUsers("user%");
for (User user : users) {
System.out.println(user.getId() + ":" + user.getUsername() + ":" + user.getPassword());
}
} finally {
session.close();
}
}