概要说明:在这个员工实现分页功能上耗费了相当大的精力,首先需要晓得一对一是怎么配置的,然后就是sql语句是怎么写的,这两个缺一不可。
Page类参考:http://blog.csdn.net/su1573/article/details/76889339
1.创建Employee.java和Employee.xml映射文件
Employee.java
public class Employee {
private Integer employeeId; //员工id
private Integer deptId;
private Dept dept; //部门
private Integer jobId;
private Job job; //职业
private String employeeName; //员工名字
private String employeeCardId; //身份证
private String employeeAddress; //地址
private String employeePostCode; //邮政编码
private String employeePhone; //手机号
private String employeeQQ; //QQ号
private String employeeEmail; //E-mail
private String employeeSex; //性别
private String employeeParty; //政治面貌
/**
* 使用@ModelAttribute接收参数时
* form表单中有日期,Spring不知道该如何转换,
* 要在实体类的日期属性上加@DateTimeFormat(pattern="yyyy-MM-dd")注解
*/
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date employeeBirthday; //出生日期
private String employeeRace; //民族
private String employeeEducation; //学历
private String employeeHobby; //爱好
private Date employeeCreateDate; //建档日期
//getter()和setter()省略
}
Employee.xml
<?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="employee">
<!-- 《员工与部门相关联》 -->
<resultMap type="com.su.domain.Employee" id="EmployeeWithDept">
<id column="employeeId" property="employeeId"/>
<result column="deptId" property="deptId"/>
<result column="jobId" property="jobId"/>
<result column="employeeName" property="employeeName"/>
<result column="employeeCardId" property="employeeCardId"/>
<result column="employeeAddress" property="employeeAddress"/>
<result column="employeePostCode" property="employeePostCode"/>
<result column="employeePhone" property="employeePhone"/>
<result column="employeeQQ" property="employeeQQ"/>
<result column="employeeEmail" property="employeeEmail"/>
<result column="employeeSex" property="employeeSex"/>
<result column="employeeParty" property="employeeParty"/>
<result column="employeeBirthday" property="employeeBirthday"/>
<result column="employeeRace" property="employeeRace"/>
<result column="employeeEducation" property="employeeEducation"/>
<result column="employeeHobby" property="employeeHobby"/>
<result column="employeeCreateDate" property="employeeCreateDate"/>
<association property="dept" javaType="com.su.domain.Dept">
<id column="deptId" property="deptId"/>
<result column="deptName" property="deptName"/>
<result column="deptDesc" property="deptDesc"/>
</association>
</resultMap>
<!-- 《员工与职业相关联 》“继承”《员工与部门相关联》 -->
<resultMap type="com.su.domain.Employee" id="EmployeeWithJobExtendDept" extends="EmployeeWithDept">
<collection property="job" ofType="com.su.domain.Job">
<id column="jobId" property="jobId"/>
<result column="jobName" property="jobName"/>
<result column="jobDesc" property="jobDesc"/>
</collection>
</resultMap>
<!-- 根据条件分页查询 -->
<select id="findEmployeeByPageName" parameterType="employee" resultMap="EmployeeWithJobExtendDept">
select employeeTable.*,jobTable.jobName,deptTable.deptName
from employeeTable,jobTable,deptTable
where jobTable.jobId = #{jobId} and deptTable.deptId = #{deptId}
and employeeTable.employeeName like '%${employeeName}%' and employeeTable.employeeCardId = #{employeeCardId}
and employeeTable.employeePhone = #{employeePhone} and employeeTable.jobId = #{jobId}
and employeeTable.deptId = #{deptId} and employeeTable.employeeSex = #{employeeSex}
</select>
<!-- 点击查询全部员工信息 -->
<select id="findAllCount" resultType="java.lang.Integer">
select count(*) from employeeTable
</select>
<!-- 查询记录数 -->
<select id="findCount" parameterType="employee" resultType="java.lang.Integer">
select count(*) from employeeTable,jobTable,deptTable
where jobTable.jobId = #{jobId} and deptTable.deptId = #{deptId}
and employeeTable.employeeName like '%${employeeName}%' and employeeTable.employeeCardId = #{employeeCardId}
and employeeTable.employeePhone = #{employeePhone} and employeeTable.jobId = #{jobId}
and employeeTable.deptId = #{deptId} and employeeTable.employeeSex = #{employeeSex}
</select>
<!-- 员工、部门、职位 -->
<select id="findEmployeeByPage" resultMap="EmployeeWithJobExtendDept" parameterType="employee">
select employeeTable.*,jobTable.jobName,deptTable.deptName
from employeeTable,jobTable,deptTable
where employeeTable.jobId = jobTable.jobId and employeeTable.deptId = deptTable.deptId
</select>
</mapper>
2. 创建EmployeeDao,和EmployeeDaoImpl
EmployeeDao.java
public interface EmployeeDao {
public Page findEmployeeByPageName(int currentPage,Employee employee); //按条件分页查询
}
EmployeeDaoImpl.java
public class EmployeeDaoImpl extends SqlSessionDaoSupport implements EmployeeDao {
@Override
public Page findEmployeeByPageName(int currentPage, Employee employee) {
SqlSession sqlSession = this.getSqlSession();
Page pageIndex = new Page(); //实例化Page对象
int countResult = 0;
if(employee.getEmployeeName() == null || employee.getEmployeeName().equals("")){
//查询employee中数据总数,或者符合条件的数据总数
countResult = sqlSession.selectOne("employee.findAllCount");
}else if(employee.getEmployeeName() != null){
//查询employee中数据总数,或者符合条件的数据总数
countResult = sqlSession.selectOne("employee.findCount",employee);
}
System.out.println("******总记录数:"+countResult);
pageIndex.setTatalNums(countResult);
int totalPages = (pageIndex.getTatalNums() % pageIndex.PAGESIZE) == 0?
pageIndex.getTatalNums() / pageIndex.PAGESIZE : (pageIndex.getTatalNums() / pageIndex.PAGESIZE)+1;
pageIndex.setTotalPages(totalPages); //总页数
System.out.println("******总页数:"+totalPages);
if(currentPage < 1 ||currentPage == 0){
currentPage = 1;
}else if(currentPage > totalPages){
currentPage = totalPages;
}
System.out.println("当前页:"+currentPage);
pageIndex.setCurrentPage(currentPage);
int firstResult = (currentPage - 1) * pageIndex.PAGESIZE;
System.out.println("起始行:"+firstResult);
pageIndex.setFirstResult(firstResult);
List deptList = null;
// offset起始行 // limit是当前页显示多少条数据
RowBounds rowBounds = new RowBounds(pageIndex.getFirstResult(), pageIndex.PAGESIZE);
if(employee.getEmployeeName() == null || employee.getEmployeeName().equals("")){
deptList = sqlSession.selectList("employee.findEmployeeByPage", employee, rowBounds);
}else if(employee.getEmployeeName() != null){
deptList = sqlSession.selectList("employee.findEmployeeByPageName", employee, rowBounds);
}
pageIndex.setList(deptList);
System.out.println("数据长度:"+deptList.size());
return pageIndex;
}
}
3.创建EmployeeService和EmployeeServiceImpl
EmployeeService.java
public interface EmployeeService {
public Page findEmployeeByPageName(int currentPage,Employee employee); //按条件分页查询
}
EmployeeServiceImpl.java
public class EmployeeServiceImpl implements EmployeeService {
@Autowired
private EmployeeDao employeeDao;
@Override
public Page findEmployeeByPageName(int currentPage, Employee employee) {
return employeeDao.findEmployeeByPageName(currentPage, employee);
}
}
4.EmployeeController控制器
具体实现员工分页查询,检索条件分页查询
@Controller
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@Autowired
private JobService jobService;
@Autowired
private DeptService deptService;
@RequestMapping("findEmployeeByPageName.action")
public ModelAndView findEmployeeByPageName(int currentPage,Employee employee,ModelAndView mav,HttpServletRequest request){
System.out.println("in EmployeeController method findEmployeeByPageName()");
if(employee.getEmployeeName() != null){
System.out.println("检索的员工姓名:"+employee.getEmployeeName());
}
request.setAttribute("empBack", employee);
Page page = employeeService.findEmployeeByPageName(currentPage, employee);
List<Dept> deptList = deptService.findAllDept();
List<Job> jobList = jobService.findAllJob();
request.setAttribute("page", page);
request.setAttribute("deptList", deptList);
request.setAttribute("jobList", jobList);
mav.setViewName("jsp/employee/employeeList.jsp");
return mav;
}
5. jsp页面展示结果
<%@ page language="java" contentType="text/html; charset=UTF-8" import="com.su.util.*"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="f" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>人事管理系统 ——员工管理</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3" />
<meta http-equiv="description" content="This is my page" />
<link href="${pageContext.request.contextPath}/css/css.css" type="text/css" rel="stylesheet" />
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/js/ligerUI/skins/Aqua/css/ligerui-dialog.css"/>
<link href="${pageContext.request.contextPath}/js/ligerUI/skins/ligerui-icons.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.11.0.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-migrate-1.2.1.js"></script>
<script src="${pageContext.request.contextPath}/js/ligerUI/js/core/base.js" type="text/javascript"></script>
<script src="${pageContext.request.contextPath}/js/ligerUI/js/plugins/ligerDrag.js" type="text/javascript"></script>
<script src="${pageContext.request.contextPath}/js/ligerUI/js/plugins/ligerDialog.js" type="text/javascript"></script>
<script src="${pageContext.request.contextPath}/js/ligerUI/js/plugins/ligerResizable.jss" type="text/javascript"></script>
<link href="${pageContext.request.contextPath}/css/pager.css" type="text/css" rel="stylesheet" />
<script type="text/javascript">
function one(frm,num){
frm.currentPage.value=num;
frm.submit();
}
function jump_to(frm,pageNo){
var reg=/^\d+$/;
if(!reg.test(pageNo)){
alert("请输入正确数字");
}else{
one(frm,pageNo);
}
}
$(function(){
/** 获取上一次选中的部门数据 */
var boxs = $("input[type='checkbox'][id^='box_']");
/** 给全选按钮绑定点击事件 */
$("#checkAll").click(function(){
// this是checkAll this.checked是true
// 所有数据行的选中状态与全选的状态一致
boxs.attr("checked",this.checked);
})
/** 给数据行绑定鼠标覆盖以及鼠标移开事件 */
$("tr[id^='data_']").hover(function(){
$(this).css("backgroundColor","#eeccff");
},function(){
$(this).css("backgroundColor","#ffffff");
})
/** 删除员工绑定点击事件 */
$("#delete").click(function(){
/** 获取到用户选中的复选框 */
var checkedBoxs = boxs.filter(":checked");
if(checkedBoxs.length < 1){
$.ligerDialog.error("请选择一个需要删除的员工!");
}else{
/** 得到用户选中的所有的需要删除的ids */
var ids = checkedBoxs.map(function(){
return this.value;
})
$.ligerDialog.confirm("确认要删除吗?","删除员工",function(r){
if(r){
// alert("删除:"+ids.get());
// 发送请求
window.location = "${pageContext.request.contextPath}/removeEmployee.action?ids=" + ids.get();
}
});
}
})
})
</script>
</head>
<body>
<!-- 导航 -->
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr><td height="10"></td></tr>
<tr>
<td width="15" height="32"><img src="${pageContext.request.contextPath}/images/main_locleft.gif" width="15" height="32"></td>
<td class="main_locbg font2"><img src="${pageContext.request.contextPath}/images/pointer.gif"> 当前位置:员工管理 > 员工查询</td>
<td width="15" height="32"><img src="${pageContext.request.contextPath}/images/main_locright.gif" width="15" height="32"></td>
</tr>
</table>
<form name="empform0" method="post" id="empform0" action="${pageContext.request.contextPath}/findEmployeeByPageName.action">
<table width="100%" height="90%" border="0" cellpadding="5" cellspacing="0" class="main_tabbor">
<!-- 查询区 -->
<tr valign="top">
<td height="30">
<table width="100%" border="0" cellpadding="0" cellspacing="10" class="main_tab">
<tr>
<td class="fftd">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td class="font3">
职位:
<select name="jobId" style="width:143px;">
<option value="${requestScope.empBack.jobId}">(默认值)</option>
<c:forEach items="${requestScope.jobList }" var="job">
<option value="${job.jobId }">${job.jobName }</option>
</c:forEach>
</select>
姓名:<input type="text" name="employeeName" value="${requestScope.empBack.employeeName}">
身份证号码:<input type="text" name="employeeCardId" maxlength="18" value="${requestScope.empBack.employeeCardId}">
</td>
</tr>
<tr>
<td class="font3">
性别:
<select name="employeeSex" style="width:143px;">
<option value="${requestScope.empBack.employeeSex}">(默认值)</option>
<option value="男">男</option>
<option value="女">女</option>
</select>
手机:<input type="text" name="employeePhone" value="${requestScope.empBack.employeePhone}">
所属部门:<select name="deptId" style="width:100px;">
<option value="${requestScope.empBack.deptId}">(默认值)</option>
<c:forEach items="${requestScope.deptList }" var="dept">
<option value="${dept.deptId }">${dept.deptName }</option>
</c:forEach>
</select>
<input type="hidden" name="currentPage" value="1">
<input type="submit" value="搜索"/>
<input id="delete" type="button" value="删除"/>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
<!-- 数据展示区 -->
<tr valign="top">
<td height="20">
<table width="100%" border="1" cellpadding="5" cellspacing="0" style="border:#c2c6cc 1px solid; border-collapse:collapse;">
<tr class="main_trbg_tit" align="center">
<td><input type="checkbox" name="checkAll" id="checkAll"></td>
<td>姓名</td>
<td>性别</td>
<td>手机号码</td>
<td>邮箱</td>
<td>职位</td>
<td>学历</td>
<td>身份证号码</td>
<td>部门</td>
<td>联系地址</td>
<td>建档日期</td>
<td align="center">操作</td>
</tr>
<c:forEach items="${requestScope.page.list}" var="employee" varStatus="stat">
<tr id="data_${stat.index}" class="main_trbg" align="center">
<td><input type="checkbox" id="box_${stat.index}" value="${employee.employeeId}"></td>
<td>${employee.employeeName }</td>
<td>${employee.employeeSex} </td>
<td>${employee.employeePhone }</td>
<td>${employee.employeeEmail }</td>
<td>${employee.job.jobName }</td>
<td>${employee.employeeEducation }</td>
<td>${employee.employeeCardId }</td>
<td>${employee.dept.deptName }</td>
<td>${employee.employeeAddress }</td>
<td>
<f:formatDate value="${employee.employeeCreateDate}"
type="date" dateStyle="long"/>
</td>
<td align="center" width="40px;"><a href="${pageContext.request.contextPath}/updateEmployee.action?flag=1&employeeId=${employee.employeeId}">
<img title="修改" src="${pageContext.request.contextPath}/images/update.gif"/></a>
</td>
</tr>
</c:forEach>
</table>
</td>
</tr>
<!-- 分页标签 -->
<tr valign="top"><td align="center" class="font3">
<% Page pageOne = (Page)request.getAttribute("page"); %>
<% if(pageOne!=null) {for(int i=1;i<=pageOne.getTotalPages();i++){ %>
<a href="javascript:one(document.forms[0],<%=i%>)"><font size="3px"><%=i%> </font></a>
<% }} %>
<br>
搜索结果共<font style="color:red">${page.tatalNums }</font>条 当前页${page.currentPage}/共${page.totalPages}页
<span>跳转至</span><input type="text" name="inputPage" id="inputPage" value="${page.currentPage}" style="width:30px;text-align:center;"/>页
<input type="button" onclick="javascript:jump_to(document.forms[0],document.getElementById('inputPage').value)" value="GO"/>
</td></tr>
</table>
</form>
<div style="height:10px;"></div>
</body>
</html>
至此员工与部门、职位,关联分页查询已完成,供以后复习
Author:su1573