今天在做分页查询数据的小demo的时候,
一开始采用的是直接传入int startIndex,int pageSize
public interface UserMapper {
public List getAllUserList();
public List getUserListByPage(int startIndex,int pageSize);
}
在xml里,如果这样写的话它必然拿不到这两个字段
select * from user;
于是我在测试的时候
报了这个错,其意思就是xml文件的时候找不到第一个字段
其原因特别简单,因为在xml里你没有给startIndex,pageSize
这两个字段声明,但是你要是在xml里赋值声明了就变成了定值了,这样肯定不能实现动态查询
所以我采用了map
public interface UserMapper {
public List getAllUserList();
public List getUserListByPage(HashMap map);
}
在测试的时候,通过map,给它传递值,这个问题迎刃而解了。
@org.junit.Test
public void indexQueryTest(){
SqlSession sqlSession = MyUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
HashMap map = new HashMap<>();
map.put("startIndex",1);
map.put("pageSize",10);
List list = mapper.getUserListByPage(map);
for (User user : list) {
System.out.println(user);
}
sqlSession.close();
}
最后是查询的结果,没有问题哦!(表是自己建的!很多重复的勿要介意!)
如果本博客帮助到了你或者你学到了东西,欢迎加入我的公众号:(暂无) 和学习交流群435805702



