mybatis分页的实现

1.实体类.mapper.xml

<select id="selectAll" parameterType="map" resultType="cn.sxt.entity.User">
    SELECT * FROM user limit #{startIndex} , #{pageSize}
</select>

2.Userdao

    public List<User> getAll(int currentPage,int pageSize) throws IOException {
        SqlSession session  = MyBatisUtil.getSession();
        Map<String,Integer> map = new HashMap<String, Integer>();
        map.put("startIndex", (currentPage-1)*pageSize);
        map.put("pageSize", pageSize);
        List<User> list = session.selectList("cn.sxt.entity.UserMapper.selectAll",map);
        //System.out.println("id="+user.getId()+",name="+user.getName()+",psw="+user.getPwd());
        session.close();
        return list;
    }

3.Test

    public static void main(String[] args) throws IOException {
        UserDao userDao = new UserDao();
        
        List<User> list = userDao.getAll(1,1);
        for(User i:list) {
            System.out.println(i);
        }
    }
Last modification:February 15th, 2020 at 03:30 pm
如果觉得我的文章对你有用,请随意赞赏