按钮功能的实现
@WebServlet(
name = "PersonnelController",
urlPatterns = "/personnel.do",
loadOnStartup = 0
)
public class PersonnelController extends HttpServlet {
private PersonnelService personnelService = PersonnelFactory.getPersonnelService();
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int current = 1;
String currentStr = request.getParameter("current");
if (Validator.isNotEmpty(currentStr) && Validator.isInteger(currentStr)) {
current = Integer.parseInt(currentStr);
}
int size = 10;
int start = (current - 1) * size;
Map<String, Object> params = new HashMap<>();
params.put("start", start);
params.put("size", size);
long count = personnelService.queryByCount(params);
int max = (int) Math.ceil(count * 1.0 / size);
List<Personnel> list = personnelService.queryByPage(params);
request.setAttribute("list", list);
request.setAttribute("current", current);
request.setAttribute("max", max);
request.setAttribute("count", count);
request.setAttribute("start", start);
request.setAttribute("size", size);
request.getRequestDispatcher("/view/personnel/list.jsp").forward(request, response);
}
public interface PersonnelService {
long queryByCount(Map<String, Object> params);
}
public class PersonnelServiceImpl implements PersonnelService {
private PersonnelDao personnelDao = PersonnelFactory.getPersonnelDao();
@Override
public long queryByCount(Map<String, Object> params) {
return personnelDao.queryByCount(params);
}
}
public interface PersonnelDao {
long queryByCount(Map<String, Object> params);
}
public class PersonnelDaoImpl implements PersonnelDao {
public PersonnelDaoImpl() {
}
private static PersonnelDaoImpl instance = null;
public static PersonnelDaoImpl getInstance() {
if (instance == null) {
synchronized (PersonnelDaoImpl.class) {
if (instance == null) {
instance = new PersonnelDaoImpl();
}
}
}
return instance;
}
@Override
public long queryByCount(Map<String, Object> params) {
SqlSession session = MybatisUtils.openSession();
long count = session.selectOne("personnel.queryByCount", params);
session.close();
return count;
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="personnel">
<select id="queryByCount" resultType="Long">
select count(1)
from rms_personnel
</select>
</mapper>