用户分页多条件查询

因为自己对mybaties的查询语句还不熟悉,刚开始就没有想过把自己的一系列条件查询和分页显示联系在一起。在学长的点播下学会了灵活的应用sql语句

步骤

1.前端我为了美观去网上下载一个分页的jquary插件

<%@page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
    <title>人力资源管理系统title>
    <script type="text/javascript"src="${pageContext.request.contextPath}/Js/timeFormat.js">script>
    <style type="text/css">
        <select id="selectAllUser"  resultMap="users"  parameterType="map">     select id,username,name,password,sex,birthday,createtime,     CASE  WHEN (length(content) <=5) THEN content ELSE CONCAT(left(content,5),'……') END    as content from users where 1=1     <trim>     <if test="username != null" >       and username like concat(concat('%',#{username}),'%')     if>         <if test="sort ==1 and starTime != null">             and createtime > #{starTime}         if>         <if test="sort ==1 and endTime != null">             and createtime < #{endTime}         if>         <if test="sort ==2 and starTime != null">             and birthday > #{starTime}         if>         <if test="sort ==2 and endTime != null">             and birthday < #{endTime}         if>         <if test="pageSize != null and pageNum != null">             limit #{pageNum} ,#{pageSize}         if>     trim> select> <select id="selectAllCount"  resultType="int"  parameterType="map">     select count(*) from users where 1=1     <trim>         <if test="username != null" >             and username like concat(concat('%',#{username}),'%')         if>         <if test="sort ==1 and starTime != null">             and createtime > #{starTime}         if>         <if test="sort ==1 and endTime != null">             and createtime < #{endTime}         if>         <if test="sort ==2 and starTime != null">             and birthday > #{starTime}         if>         <if test="sort ==2 and endTime != null">             and birthday < #{endTime}         if>     trim> select>

注意:mybaties语句里面的大于小于符号是和sql里面不一样的,统计查询到的记录数量时,条件不能加上分页的查询条件。

3.controler里面的语句

@RequestMapping("/toListUser")
public String toListUser(ModelMap model, String sort, String startTime ,String endTime,String username,String pageNum,String pageSize ) {
    List list = userService.selectUsers(sort,startTime,endTime,username, pageNum, pageSize);
    int count=userService.selectAllCount(sort,startTime,endTime,username);
    model.addAttribute("username",username);
    model.addAttribute("sort",sort);
    model.addAttribute("startTime",startTime);
    model.addAttribute("endTime",endTime);
    model.addAttribute("pageNum",pageNum);
    model.addAttribute("pageSize",pageSize);
    model.addAttribute("user", list);
    model.addAttribute("tolcount", count);
    return "/listuser";
}

 

你可能感兴趣的:(用户分页多条件查询)