前言
前面实现SSM整合以及实现原始手动分页参考
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/85113289
使用插件首先要先加载jar包
jar包下载:
https://download.csdn.net/download/badao_liumang_qizhi/10863649
将两个jar包,放到项目下的lib目录下。
在sqlsession下增加
完整代码
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/ssmtest?characterEncoding=UTF-8
root
523627
Service中去掉total方法和原来手动分页的方法
package com.badao.service;
import java.util.List;
import com.badao.pojo.User;
import com.badao.util.Page;
public interface UserService {
List selectAllUser();
//List selectAllUser(Page page);
//int total();
}
注释掉原来的手动分页方法
package com.badao.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.badao.mapper.UserMapper;
import com.badao.pojo.User;
import com.badao.service.UserService;
import com.badao.util.Page;
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserMapper userMapper;
public List selectAllUser() {
// TODO Auto-generated method stub
return userMapper.selectAllUser();
}
/* @Override
public List selectAllUser(Page page) {
// TODO Auto-generated method stub
return userMapper.selectAllUser(page);
}
@Override
public int total() {
// TODO Auto-generated method stub
return userMapper.total();
}*/
}
userMapper.java中注释掉total()方法以及原来的手动分页的方法。
package com.badao.mapper;
import java.util.List;
import com.badao.pojo.User;
import com.badao.util.Page;
public interface UserMapper {
public int addUser(User user);
public User selectUser(int id);
public int updateUser(User user);
public void deleteUser(int id);
public List selectAllUser();
//public List selectAllUser(Page page);
//public int total();
}
userMapper.xml中去掉total的select语句以及原来的limit语句
insert into user ( name,age ) values (#{name},#{age})
delete from user where id= #{id}
< BR> updateusersetname=#{name},age=#{age} where id=#{id}
通过
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
导入类
通过
PageHelper.offsetPage(page.getStart(),5);
实现分页按需查询
通过
int total = (int) new PageInfo<>(cs).getTotal();
获取查询的总数
完整代码
package com.badao.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.badao.pojo.User;
import com.badao.service.UserService;
import com.badao.util.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
// 告诉spring mvc这是一个控制器类
@Controller
@RequestMapping("")
public class UserController {
@Autowired
UserService userService;
@RequestMapping("listUser")
public ModelAndView listUser(Page page){
ModelAndView mav = new ModelAndView();
//根据分页对象,进行查询获取查询对象集合
//List cs= userService.selectAllUser(page);
//int total = userService.total();
//根据总数,计算最后一页的信息
PageHelper.offsetPage(page.getStart(),5);
List cs= userService.selectAllUser();
int total = (int) new PageInfo<>(cs).getTotal();
page.caculateLast(total);
// 放入转发参数
mav.addObject("userList", cs);
// 放入jsp路径
mav.setViewName("listUser");
return mav;
}
}
注释掉原来的的手动分页实现时的单元测试代码
package com.badao.test;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import com.badao.mapper.UserMapper;
import com.badao.pojo.User;
import com.badao.util.Page;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;//使用junit4时报错导这个包
@RunWith(SpringJUnit4ClassRunner.class)//使用junit4进行测试
@ContextConfiguration("classpath:applicationContext.xml")//加载配置文件
public class InsertTest {
@Autowired//自动注入
private UserMapper userMapper;
@Test//标明是测试方法
@Rollback(false) //标明使用完此方法后事务不回滚,true时为回滚
public void testAdd() {
for (int i = 0; i < 100; i++) {
User user = new User();
user.setName("user"+i);
userMapper.addUser(user);
}
}
/* @Test
public void testTotal() {
int total = userMapper.total();
System.out.println(total);
}
@Test
public void testList() {
Page p = new Page();
p.setStart(2);
p.setCount(3);
List cs=userMapper.selectAllUser(p);
for (User c : cs) {
System.out.println(c.getName());
}
}*/
}
保持不变
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
pageContext.setAttribute("APP_PATH", request.getContextPath());
%>
查询所有用户
id
name
age
${u.id}
${u.name}
${u.age}
https://download.csdn.net/download/badao_liumang_qizhi/10863821