public interface EmpDao {
/**
* 获取雇员总数量
* @return
*/
public int searchAll(Map map);
public List<Emp> search(Map map);
}
mapper映射文件
<select id="searchAll" parameterType="java.util.Map" resultType="int">
select count(*) from emp <where>
<if test="minSal>0">
and sal > #{minSal}
</if>
</where>
</select>
<select id="search" parameterType="java.util.Map" resultType="com.entity.Emp">
select empno,ename,sal from
(select rownum r ,e.* from emp e
<if test="minSal>0">
where e.sal > #{minSal}
</if>
) a where a.r > #{start}
and a.r < #{end}
</select>
业务层接口
//业务层的接口
public interface IEmpService {
//分页;
public PageData getList(Map map);
}
业务层接口的实现类
@Service("service")
public class EmpService implements IEmpService {
//注入,byName类型;
@Resource
private EmpDao empDao;//数据层;
public PageData getList(Map map) {
PageData pageData=new PageData(empDao.searchAll(map),empDao.search(map));
return pageData;
}
}
控制器代码
@Controller
@RequestMapping("/emp") //窄化处理
public class EmpController {
//自动注入
@Resource
private IEmpService service;
@RequestMapping("/list")
public String getList(Model model){//Model:模型,只存数据
List<Emp>list=service.getList();
model.addAttribute("size",list.size());
model.addAttribute("list",list);//存放集合
return "test2";//返回到list页面
}
//分页
@RequestMapping("/list2")
public String list(Model model,Integer pageNo,Integer minSal,HttpServletRequest request){
System.out.println("URI地址:"+request.getRequestURI());
// Enumeration pNames=request.getParameterNames();
// while(pNames.hasMoreElements()){
// String name=(String)pNames.nextElement();
// System.out.println("name:"+name+"="+request.getParameter(name));
// }
int pageSize=3;
if(pageNo==null){
pageNo=1;
}
Map map =new HashMap();
map.put("start", (pageNo-1)*pageSize);
map.put("end", pageNo*pageSize+1);
map.put("minSal",minSal);
PageData pageData = service.getList(map);
int count = pageData.getCount();//总数量
String pageString = new PageUtil(pageSize, pageNo, count, request).getPageString();
model.addAttribute("list", pageData.getList());
model.addAttribute("pageString", pageString);
return "list";
}
}
前端页面list.jsp页面
<form method="post" action="emp/list2">
最低工资:<input type="text" name="minSal" value="${param.minSal}"/>
最高工资:<input type="text" name="maxSal">
<input type="submit" value="查询"/>
</form>
<table border="1" align="center" width="100%">
<tr align="center">
<td>工号</td><td>姓名</td><td>工资</td><td>操作</td>
</tr>
<c:forEach items="${list }" var="emp">
<tr>
<td>${emp.empno }</td>
<td>${emp.ename }</td>
<td>${emp.sal }</td>
<td><!--view->controller->view -->
<a href='emp/toAddEmp'>增加</a>
<a href='emp/getEmp/${emp.empno}'>编辑</a>
<a href='emp/${emp.empno}/deleteEmp'>删除</a>
<a href='emp/deleteEmp2?empno=${emp.empno}'>删除2</a>
</td>
</tr>
</c:forEach>
<tr align="center"><td colspan="4">${pageString}</td></tr>
</table>
正常带查询的坟茔应该是如下状态
按时间查询,在入职日期输入一个时间,点击datetimebox,选择相应的时间
mapper文件代码:
<select id="getpage" parameterType="java.util.Map" resultType="net.bean.emp">
select * from (select rownum r ,e.empno,e.ename,e.age,e.sex,e.email,e.ephone,p.pname,dt.dname,d.dromroom,e.eaddress,e.hiredate,e.leavedate from T_emp e left join
T_drom d on e.dromno=d.dromno left join T_dept dt on e.deptno=dt.deptno left join T_position p on e.pno=p.pno
<where>
<if test="empno>0">
and e.empno=#{empno}
</if>
<if test="ename!=null">
and e.ename like '%${ename}%'
</if>
<if test="hiredate!=null">
and e.hiredate=to_date('${hiredate}','yyyy-mm-dd')
</if>
</where>
)a
where a.r > #{start}
and a.r < #{end}
</select>
控制器代码:
@RequestMapping("/getemps")
public String list(Model model,Integer pageNo,HttpServletRequest request,Integer empno,String ename,Date hiredate){
System.out.println("URI地址:"+request.getRequestURI());
int pageSize=5;//每页条数
if(pageNo==null){
pageNo=1;
}
Map map =new HashMap();
map.put("start", (pageNo-1)*pageSize);
map.put("end", pageNo*pageSize+1);
map.put("empno",empno);
map.put("ename", ename);
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
if(hiredate!=null){
String s=sdf.format(hiredate);
System.out.println(s);
map.put("hiredate", s);
}
PageData pageData =empservice.getList(map);
int count = pageData.getCount();//总数量
String pageString = new PageUtil(pageSize, pageNo, count, request).getPageString();
model.addAttribute("emps", pageData.getList());
model.addAttribute("pageString", pageString);
return "emp/getemps";
}
注意实体类型是java.sql.date,可以直接查询。