JSP页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<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 rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
function aa(pageNum){
// alert(pageNum);
var form1=document.getElementById("form1");
form1.action="EmpAction.do?flag=findAll&pageNum="+pageNum;
form1.submit();
}
</script>
</head>
<body>
欢迎:${ee.empName }
<a href="EmpAction.do?flag=findAll">查询全部</a>
<c:if test="${list!=null }">
<form action="EmpAction.do?flag=findAll" method="post" id="form1">
姓名:<input type="text" name="empName" value="${e.empName }">
部门:<input type="text" name="deptName" value="${e.deptName }">
生日:<input type="text" name="birthday" value="${e.birthday }">
<input type="submit" value=" submit ">
</form>
<table>
<tr>
<td>id</td>
<td>姓名</td>
<td>部门</td>
<td>生日</td>
</tr>
<c:forEach items="${list }" var="e">
<tr>
<td>${e.empId }</td>
<td>${e.empName }</td>
<td>${e.deptId }</td>
<td>${e.dept.deptName }</td>
<td>${e.birthday }</td>
</tr>
</c:forEach>
<tr>
<td>共${p.totalSize }条共${p.totalPage }页 第${p.pageNum }页
<a href="javascript:;" onclick="aa(${p.pageNum-1 })">上一页</a>
<a href="javascript:;" onclick="aa(${p.pageNum+1 })">下一页</a>
</td>
</tr>
</table>
</c:if>
</body>
</html>
Service层
import java.util.List;
public interface EmpService {
public Emp findEmpById(int id);
public List<Emp> findAll(Emp e);
public Emp login(Emp e);
public int count(Emp e);
}
bean层
import java.util.Date;
public class Emp {
private int empId;
private String empName;
private int sex;
private int age;
private int deptId;
private Date birthday;
private String username;
private String password;
private Dept dept;
private String deptName;
private int begin;
private int end;
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public int getBegin() {
return begin;
}
public void setBegin(int begin) {
this.begin = begin;
}
public int getEnd() {
return end;
}
public void setEnd(int end) {
this.end = end;
}
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getDeptId() {
return deptId;
}
public void setDeptId(int deptId) {
this.deptId = deptId;
}
}
public class Page {
private int pageNum;
private int pageSize;
private long totalSize;
private int totalPage;
public int getBegin(){
return (pageNum-1)*pageSize+1;
}
public int getEnd(){
return pageNum*pageSize;
}
public int getPageNum() {
return pageNum;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public long getTotalSize() {
return totalSize;
}
public void setTotalSize(long totalSize) {
this.totalSize = totalSize;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public Page(int pageNum, int pageSize, int totalSize) {
this.pageSize = pageSize;
this.totalSize = totalSize;
this.totalPage = totalSize%pageSize==0?totalSize/pageSize:totalSize/pageSize+1;
if(pageNum<=0){
this.pageNum=1;
}else if(pageNum>totalPage&&totalPage!=0){
this.pageNum=totalPage;
}else{
this.pageNum=pageNum;
}
}
}
<?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="com.dao.EmpDao">
<resultMap type="emp" id="rm">
<id property="empId" column="emp_id"/>
<result property="empName" column="emp_name"/>
<result property="deptId" column="dept_id"/>
<result property="sex" column="sex"/>
<result property="age" column="age"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
<result property="birthday" column="birthday"/>
<association property="dept" javaType="Dept">
<id property="deptId" column="dept_id"/>
<result property="deptName" column="dept_name"/>
</association>
</resultMap>
<select id="findEmpById" parameterType="int" resultMap="rm">
select * from emp_tb where emp_id=#{id}
</select>
<select id="findAll" parameterType="emp" resultMap="rm">
select * from(
select ee.*,rownum rn from(
select e.emp_id,e.emp_name,e.birthday,d.dept_id,d.dept_name
from emp_tb e inner join dept_tb d on e.dept_id=d.dept_id
<where>
<if test="empName!=null and empName!=''">
and e.emp_name like '%'||#{empName}||'%'
</if>
<if test="deptName!=null and deptName!=''">
and d.dept_name like '%'||#{deptName}||'%'
</if>
<if test="birthday!=null">
and e.birthday>#{birthday}
</if>
</where>
) ee )
eee where eee.rn between #{begin} and #{end}
</select>
<select id="login" parameterType="emp" resultMap="rm">
select * from emp_tb where username=#{username} and password=#{password}
</select>
<select id="count" resultType="int">
select count(*) from emp_tb e inner join dept_tb d on e.dept_id=d.dept_id
<where>
<if test="empName!=null and empName!=''">
and e.emp_name like '%'||#{empName}||'%'
</if>
<if test="deptName!=null and deptName!=''">
and d.dept_name like '%'||#{deptName}||'%'
</if>
<if test="birthday!=null">
and e.birthday>#{birthday}
</if>
</where>
</select>
</mapper>
Action层
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
@Component
@SessionAttributes({"ee"})
@RequestMapping("EmpAction.do")
public class EmpAction {
@Autowired
@Qualifier("empServiceImpl")
private EmpService es;
public EmpService getEs() {
return es;
}
public void setEs(EmpService es) {
this.es = es;
}
@RequestMapping(params="flag=login")
public String login(@ModelAttribute Emp e,ModelMap map){
System.out.println(e.getUsername());
Emp ee=es.login(e);
String str=null;
if(ee!=null){
map.put("ee", ee);
str="admin/index.jsp";
}else{
str="login.jsp";
}
return str;
}
@RequestMapping(params="flag=findAll")
public String findAll(ModelMap map,HttpServletRequest request){
String pageNum1=request.getParameter("pageNum");
int pageNum=0;
if(pageNum1!=null&&pageNum1!=""){
pageNum=Integer.parseInt(pageNum1);
}
String empName=request.getParameter("empName");
String deptName=request.getParameter("deptName");
String birthday=request.getParameter("birthday");
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Date birth=null;
if(birthday!=null&&birthday!=""){
try {
birth=sdf.parse(birthday);
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
Emp e=new Emp();
e.setEmpName(empName);
e.setDeptName(deptName);
e.setBirthday(birth);
int totalSize=es.count(e);
Page p=new Page(pageNum, 3, totalSize);
e.setBegin(p.getBegin());
e.setEnd(p.getEnd());
List<Emp> list=es.findAll(e);
map.put("e", e);
map.put("p", p);
map.put("list", list);
return "admin/index.jsp";
}
}