基于SSM框架的《超市订单管理系统》Web项目开发(第三天)用户管理,模糊查询,精准匹配,分页显示数据

2023-11-11

基于SSM框架的《超市订单管理系统》Web项目开发(第三天)用户管理,模糊查询,精准匹配,分页显示数据

昨天我们完善了登录的功能模块和退出的功能模块,今天我们将实现超市订单管理系统的用户管理功能模块中的用户列表和查询用户功能。


今天要完成的功能有:

对用户信息的显示和查询,其中重点有以下三点

(1) 查询:用户名称(模糊匹配)、用户角色(精确匹配)

(2) 分页显示数据列表。(后期会讲解如何用插件完成,现在要手写实现一下,虽然难,但是理解了就对我们的学习很有帮助)


在这里插入图片描述

在这里插入图片描述


一、使用SSM读取Role表中的所有数据

因为要想读取用户表user中的所有数据,需要用到账号角色信息,而账号角色信息存储在role表中,因此我们要想办法显示出来。所以要处理一下user表和role表的对应关系。

在这里插入图片描述

那么先读取角色表role的所有信息,尝试pojo->dao->Service的处理过程,跟昨天是一致的

①在pojo包下,添加用户角色表role的pojo类Role

这里就不说了,老办法idea链接databas自动生成pojo类

//Role.java
package com.steveDash.pojo;


public class Role {

  private Integer id;
  private String roleCode;
  private String roleName;
  private Integer createdBy;
  private java.util.Date creationDate;
  private Integer modifyBy;
  private java.util.Date modifyDate;


  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }


  public String getRoleCode() {
    return roleCode;
  }

  public void setRoleCode(String roleCode) {
    this.roleCode = roleCode;
  }


  public String getRoleName() {
    return roleName;
  }

  public void setRoleName(String roleName) {
    this.roleName = roleName;
  }


  public Integer getCreatedBy() {
    return createdBy;
  }

  public void setCreatedBy(Integer createdBy) {
    this.createdBy = createdBy;
  }


  public java.util.Date getCreationDate() {
    return creationDate;
  }

  public void setCreationDate(java.util.Date creationDate) {
    this.creationDate = creationDate;
  }


  public Integer getModifyBy() {
    return modifyBy;
  }

  public void setModifyBy(Integer modifyBy) {
    this.modifyBy = modifyBy;
  }


  public java.util.Date getModifyDate() {
    return modifyDate;
  }

  public void setModifyDate(java.util.Date modifyDate) {
    this.modifyDate = modifyDate;
  }

}

②在dao层下,新建RoleMapper.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="com.steveDash.dao.RoleMapper">
    <select id="getRoleList" resultType="Role">
        select * from role
    </select>
</mapper>

这里创建了一个查询,读取了role表中的所有数据

③同样在dao层下,创建RoleMapper接口,声明getRoleList()方法

这里要注意方法名必须与RoleMapper映射文件中sql语句的映射id一致

package com.steveDash.dao;

import com.steveDash.pojo.Role;

import java.util.List;

public interface RoleMapper {
    public List<Role> getRoleList();
}

这里的返回类型是List<Role>,因为是多条数据,且全都是Role类型的对象,因此如此声明返回值。

PS:这里是泛型,我的高级编程文章中也有讲解到泛型的学习


④Service层下,新建RoleService接口和上面的RoleMapper接口基本一致,也可以修改方法名(自己记得调用该方法名即可)
package com.steveDash.service;

import com.steveDash.pojo.Role;

import java.util.List;

public interface RoleService {
    public List<Role> getRoleList();
}

新建RoleService的实现类RoleServiceImpl,代码如下

package com.steveDash.service;

import com.steveDash.dao.RoleMapper;
import org.springframework.stereotype.Service;
import com.steveDash.pojo.Role;

import javax.annotation.Resource;
import java.util.List;

@Service("roleService")
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleMapper roleMapper;//引用前面的RoleMapper

    public RoleMapper getRoleMapper() {
        return roleMapper;
    }

    public void setRoleMapper(RoleMapper roleMapper) {
        this.roleMapper = roleMapper;
    }

    @Override
    public List<Role> getRoleList() {
       List<Role> roleList=null;
       try{
           roleList=roleMapper.getRoleList();
       }catch (RuntimeException e){
           e.printStackTrace();
           throw e;
       }
       return roleList;
    }
}

这里使用的注解的方式注册实体Bean**,有个常见的做法或者说是推荐做法就是,Mybatis使用XML方式,Spring和SpringMVC使用注解开发模式**。

还有我们这里引用了前面的RoleMapper,千万要记得生成getter和setter方法,因为依赖注入依靠的是构造器和setter方法


二、对用户表进行查询和显示的dao->Service层操作

在这里插入图片描述

可以看到,不做查询的时候就是默认查询全部的用户,并且还显示了用户角色这一信息,因为用户表User中的用户角色UserRole只是一个id,具体的角色名称RoleName在Role表中

那么要如何把俩个表关联起来呢?细心的读者,可能应该猜到了,多表关联查询,也就是动态SQL语句,一对一关联映射。(一个用户只对应一个身份)

从这个需求图也能看出,还有一个分页的功能,因为上线的系统,一般来说数据都很多,是没有办法一次性显示完的,每一页都只会显示一部分,因此这里我们设置每页显示的是五个用户。用户点击下一页再更新显示下一页的数据。

在这里插入图片描述

为了实现分页,我们就不能在select语句中一次性把全部数据读出来(这样会给传输和服务器带来巨大的负担),这时我们需要使用select语句中的limit用法

PS:

Select语句中limit的用法。

格式: select * from table where ... limit m,n 
(注意limit语句要放在select语句最后面)
用法:查询从第m+1条记录开始,读取n条记录。其中n如果省略,默认为0

注意只在MySQL中起作用。
在分页技术中经常使用。

下面通过两个例子看SQL语句中limit用法是怎样的:

举例:

1、select * from Customer LIMIT 10;--检索前10行数据,即显示1-10条数据;

2、select * from Customer LIMIT 1,10;--检索从第2行开始,一共10条记录,即显示第2....11记录;

3、select * from Customer limit 5,10;--检索从第6行开始,10条数据,即显示第6,7....15记录;

4、select * from Customer limit 6,10;--检索从第7行开始,10条数据,即显示第7,8...16记录。

那么就来进行功能的实现吧

1.添加显示用户表user的记录数量的查询

​ 用户可以输入用户名(username)或用户角色 (userRole) ,进行查询,但是返结果是显示有多少条记录,这是为了后面分页的时候使用。请注意我们使用的动态SQL语句 和多表关联:

​ 由于是对用户表进行操作,因此我们放到前面的user的操作中,不用另外创建文件。

①dao层下,UserMapper.xml新增getUserCount(),用来查询用户记录总数。
<!-- 查询用户计录数-->
<select id="getUserCount" resultType="int" >
    select count(1) as count from user u join role r on u.userRole = r.id
    <where>
        <if test="userName != null and userName != ''">
            u.userName like CONCAT ('%',#{userName},'%')
        </if>
        <if test="userRole != null and userRole!=0">
            and u.userRole = #{userRole}
        </if>
    </where>
</select>

②同样是在dao层下,UserMapper接口中,添加方法getUserCount
package com.steveDash.dao;

import com.steveDash.pojo.User;
import org.apache.ibatis.annotations.Param;

import java.util.List;


public interface UserMapper {
    public User getUserByUserCode(@Param("userCode") String userCode);
   	//获取用户记录总数
    public int getUserCount(@Param("userName")String userName, @Param("userRole")Integer userRole);
 
    


}

@Param 注解通常用于告诉框架或者持久层(如 MyBatis)方法接受的是哪个参数。主要目的是解决方法参数名称与 SQL 语句中的参数名称不匹配的问题,以确保框架能够正确地映射参数


③在service层中,往UserService中添加这个方法,以及在UserServiceImpl中实现这个方法

UserService接口添加该方法:

package com.steveDash.service;

import com.steveDash.pojo.User;

import java.util.List;

public interface UserService {
    public User getUserByUserCode(String userCode);
    //获取用户记录总数
    public int getUserCount(String queryUserName,int queryUserRole);

}

UserServiceImpl中实现该方法

package com.steveDash.service;

import com.steveDash.dao.UserMapper;
import com.steveDash.pojo.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("userServiceImpl")
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    public UserMapper getUserMapper() {
        return userMapper;
    }

    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public User getUserByUserCode(String userCode) {
        try{
        return userMapper.getUserByUserCode(userCode);
    }catch (RuntimeException e){
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public int getUserCount(String queryUserName, int queryUserRole) {
        int count = 0;
        try {
            count=userMapper.getUserCount(queryUserName,queryUserRole);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

   

}

到这里获取用户记录总数就已经完成了。那么先放着进行下一步操作


2.添加显示user用户表中的信息查询

用户可以输入用户名(username)或用户角色 (userRole) ,进行查询,但是和上面不同的是,我们这次返结果是显示用户表的信息,不是有多少条记录

在这里我们要使用多表关联查询,将User表和Role表关联起来,读取用户角色名称roleName

在这里插入图片描述


①确认好主表,返回的是User类型,所以我们在User类中添加一个Role类做为它的成员变量
package com.steveDash.pojo;

import org.springframework.stereotype.Component;

@Component
public class User {

  private Integer id;
  private String userCode;
  private String userName;
  private String userPassword;
  private Integer gender;
  private java.util.Date birthday;
  private String phone;
  private String address;
  private Integer userRole;
  private Integer createdBy;
  private java.util.Date creationDate;
  private Integer modifyBy;
  private java.util.Date modifyDate;
  private Role role;//一对一的多表联合查询,用于存储用户角色类


  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }


  public String getUserCode() {
    return userCode;
  }

  public void setUserCode(String userCode) {
    this.userCode = userCode;
  }


  public String getUserName() {
    return userName;
  }

  public void setUserName(String userName) {
    this.userName = userName;
  }


  public String getUserPassword() {
    return userPassword;
  }

  public void setUserPassword(String userPassword) {
    this.userPassword = userPassword;
  }


  public Integer getGender() {
    return gender;
  }

  public void setGender(Integer gender) {
    this.gender = gender;
  }


  public java.util.Date getBirthday() {
    return birthday;
  }

  public void setBirthday(java.util.Date birthday) {
    this.birthday = birthday;
  }


  public String getPhone() {
    return phone;
  }

  public void setPhone(String phone) {
    this.phone = phone;
  }


  public String getAddress() {
    return address;
  }

  public void setAddress(String address) {
    this.address = address;
  }


  public Integer getUserRole() {
    return userRole;
  }

  public void setUserRole(Integer userRole) {
    this.userRole = userRole;
  }


  public Integer getCreatedBy() {
    return createdBy;
  }

  public void setCreatedBy(Integer createdBy) {
    this.createdBy = createdBy;
  }


  public java.util.Date getCreationDate() {
    return creationDate;
  }

  public void setCreationDate(java.util.Date creationDate) {
    this.creationDate = creationDate;
  }


  public Integer getModifyBy() {
    return modifyBy;
  }

  public void setModifyBy(Integer modifyBy) {
    this.modifyBy = modifyBy;
  }


  public java.util.Date getModifyDate() {
    return modifyDate;
  }

  public void setModifyDate(java.util.Date modifyDate) {
    this.modifyDate = modifyDate;
  }

  public Role getRole() {
    return role;
  }

  public void setRole(Role role) {
    this.role = role;
  }

  @Override
  public String toString() {
    return "User{" +
            "id=" + id +
            ", userCode='" + userCode + '\'' +
            ", userName='" + userName + '\'' +
            ", userPassword='" + userPassword + '\'' +
            ", gender=" + gender +
            ", birthday=" + birthday +
            ", phone='" + phone + '\'' +
            ", address='" + address + '\'' +
            ", userRole=" + userRole +
            ", createdBy=" + createdBy +
            ", creationDate=" + creationDate +
            ", modifyBy=" + modifyBy +
            ", modifyDate=" + modifyDate +
            '}';
  }
}

记得添加了Role类作为变量,要记得设置getter和setter方法


②在dao层下,往UserMapper.xml中添加查询

​ 该查询,根据用户的名称userName,用户角色userRole,当前记录currentPageNo,每页数量pageSize进行查询,返回查到的所有记录:(这里就用到我们上面讲的limit方法

<!-- 创建一个对用户表分页的查询 -->
<select id="getUserListByPage" resultMap="UserWithRoleName">
    select u.*,r.* from user u join role r on u.userRole = r.id
    <where>
        <if test="userName != null and userName != ''">
            and u.userName like CONCAT ('%',#{userName},'%')
        </if>
        <if test="userRole != null  and userRole!=0">
            and u.userRole = #{userRole}
        </if>
    </where>
    order by u.id  limit #{currentPageNo},#{pageSize}
</select>
<resultMap id="UserWithRoleName" type="User">
    <id property="id" column="id" />
    <association property="role" javaType="Role">
        <id property="id" column="id" />
    </association>
</resultMap>

​ 根据我们前面学的,association用于一对一的情况,若是对多表关联查询的动态SQL这些知识不太熟悉的话,可以看看我的第三天文章动态SQL的学习第四天文章多表关联映射和缓存机制

PS:请注意,上面我们使用了临时表,同时还使用了limit,显示从第currentPageNo+1条记录开始,pageSize条记录的内容


③往resources->mybatis-config.xml,添加自动映射和Mybatis的二级缓存设置

ps:我们在关联映射和缓存机制的文章中,已经讲过了,添加的setting需要在typeAliases上,不然会报错,有遗忘的可以返回过去查看一下。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
    <settings>
        <!-- 开启自动映射,多表关联查询时,后面的resultMap使用自动映射,即不需要列出所有字段名-->
        <setting name="autoMappingBehavior" value="FULL" />
        <!-- 开启二级缓存机制,一级缓存是默认开启的-->
        <setting name="cacheEnabled" value="true"/>
    </settings>
    <typeAliases>
        <package name="com.steveDash.pojo" />
    </typeAliases>
</configuration>

④在dao层下,往UserMapper接口中,给上面的映射添加一个方法
package com.steveDash.dao;

import com.steveDash.pojo.User;
import org.apache.ibatis.annotations.Param;

import java.util.List;


public interface UserMapper {
    public User getUserByUserCode(@Param("userCode") String userCode);
    //获取用户记录总数
    public int getUserCount(@Param("userName")String userName, @Param("userRole")Integer userRole);
    //根据条件查询用户列表
    public List<User> getUserListByPage(@Param("userName")String userName,
                                        @Param("userRole")Integer userRole,
                                        @Param("currentPageNo")Integer currentPageNo,
                                        @Param("pageSize")Integer pageSize);



}

⑤在service层中,往UserService中添加该方法和上面的UserMapper接口一样,往UserServiceImpl中实现该方法的具体细节

UserService接口下:

package com.steveDash.service;

import com.steveDash.pojo.User;

import java.util.List;

public interface UserService {
    public User getUserByUserCode(String userCode);
    //获取用户记录总数
    public int getUserCount(String queryUserName,int queryUserRole);
    //根据条件查询用户列表
    public List<User> getUserListByPage (String queryUserName,int queryUserRole,int currentPageNo, int pageSize);
   
}

实现类UserServiceImpl中实现上面的查询操作

package com.steveDash.service;

import com.steveDash.dao.UserMapper;
import com.steveDash.pojo.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("userServiceImpl")
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    public UserMapper getUserMapper() {
        return userMapper;
    }

    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public User getUserByUserCode(String userCode) {
        try{
        return userMapper.getUserByUserCode(userCode);
    }catch (RuntimeException e){
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public int getUserCount(String queryUserName, int queryUserRole) {
        int count = 0;
        try {
            count=userMapper.getUserCount(queryUserName,queryUserRole);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public List<User> getUserListByPage(String queryUserName, int queryUserRole, int currentPageNo, int pageSize) {
        List<User> userList = null;

        try {
            userList=userMapper.getUserListByPage(queryUserName,queryUserRole,currentPageNo,pageSize);
        }
        catch (Exception e) {

            e.printStackTrace();
        }
        return userList;
    }


   

}

导入该引入的包,比如import java.utils.List

到这里我们已经完成了数据查询的分页数据处理,接下来我们需要根据后台传过来的值进行显示在VIEW端,并且在View端选择第几页,把currentPageNo和pageSize值传给service端决定读取哪一页的数据


3.整合SSM实现数据显示和查询分页
①新建tools包,往其中添加分页支持类PageSupport.java

在这里插入图片描述

package com.steveDash.tools;

public class PageSupport {
    //当前页码-来自于用户输入
    private int currentPageNo = 1;

    //总数量(表)
    private int totalCount = 0;

    //页面容量
    private int pageSize = 0;

    //总页数-totalCount/pageSize(+1)
    private int totalPageCount = 1;

    public int getCurrentPageNo() {
        return currentPageNo;
    }

    public void setCurrentPageNo(int currentPageNo) {
        if(currentPageNo > 0){
            this.currentPageNo = currentPageNo;
        }
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        if(totalCount > 0){
            this.totalCount = totalCount;
            //设置总页数
            this.setTotalPageCountByRs();
        }
    }
    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        if(pageSize > 0){
            this.pageSize = pageSize;
        }
    }

    public int getTotalPageCount() {
        return totalPageCount;
    }

    public void setTotalPageCount(int totalPageCount) {
        this.totalPageCount = totalPageCount;
    }

    public void setTotalPageCountByRs(){
        if(this.totalCount % this.pageSize == 0){//如果总行数除以每页行数可以除尽
            this.totalPageCount = this.totalCount / this.pageSize; //就有总行/每页多少行 页
        }else if(this.totalCount % this.pageSize > 0){ //如果总行数除以每页行数可以除不尽
            this.totalPageCount = this.totalCount / this.pageSize + 1;//就有总行/每页多少行+1 页
        }else{
            this.totalPageCount = 0;
        }
    }
}

在这个类里面,我们实现分页所用到的方法

稍微解释一下代码:

说明:上面的类主要有三个成员变量:

  • currentPageNo:当前显示第几条记录
  • totalCount:一共有多少条记录
  • totalPageCount:一共有多少页

其中currentPageNototalCount将由前端传过来。这个类主要功能是用setTotalPageCountByRs()方法计算出一共有多少页,给totalPageCount赋值,方法是:如果totalCount能整除pageSize,那就一共有totalCount/pageSize页 ,如果不能整除,就有totalCount/pageSize+1

例如:如果一共12行,每页6行,那么就有12/6=2页

如果一共12行,每页5行,就有12/5+1=3页 (取整)

PS:这一段可能有点难,涉及到了一部分算法,需要认真阅读并且消化代码才能理解


②在controller包中,找到UserController类,往其中添加查询用户方法getUserList(),并跳转至userlist.jsp页面

先要添加用户对象

@Resource
	private RoleService roleService;
package com.steveDash.controller;


import com.steveDash.pojo.User;
import com.steveDash.service.RoleService;
import com.steveDash.service.UserService;

import com.steveDash.tools.PageSupport;
import org.apache.log4j.Logger;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import com.steveDash.pojo.Role;

@Controller
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private RoleService roleService;

    private Logger logger= Logger.getLogger(UserController.class);

    @RequestMapping("/default")
    public String hello(){
        System.out.println("访问到Controller");
        return "default";
    }

    @RequestMapping(value="/main")
    public String welcome(HttpSession session)  {
        if(session.getAttribute("user") == null){ //如果用户没有登录就直接来到main.html就回到login
            return "redirect:/syserror";
        }
        else
            return "welcome";
    }

    @RequestMapping("/searchUser")
    public String search(){
        System.out.println("欢迎访问搜索页");
        return "searchUser";
    }

    @RequestMapping("/dosearchUser")
    public String searchUser(@RequestParam("username") String username, Model model){
        System.out.println("用户想要查询的用户名是:"+username);
        model.addAttribute("username",username);//把值放到model中传到前端
        return "result";
    }

    @RequestMapping(value="/login")
    public String showLoginPage(){
        System.out.println("进入登录页面");
        return "login";
    }

    @RequestMapping(value="/dologin")
    public String doLogin(@RequestParam("userCode") String userCode, @RequestParam("userPassword") String userPassword, Model model, HttpSession session){
        //读取用户和密码
        System.out.println("帐号和密码是"+userCode+"-"+userPassword);
        User user=userService.getUserByUserCode(userCode);
        if(user!=null){
        if(userPassword.equals(user.getUserPassword())){
           session.setAttribute("user",user);//添加session值
            return "redirect:/main"; //密码正确就去welcome.jsp
        }else{
            //登录失败就回到login.jsp
            model.addAttribute("error", "用户名或密码不正确");
            return "login";
        }
    }else{
        //登录失败就返回login.jsp
            model.addAttribute("error", "用户名不正确");
            return "login";
        }
    }

    @RequestMapping(value = "/logout")
    public String logout(HttpSession session){
        session.removeAttribute("user");//清除掉Session 中的user值
        return "redirect:/login";//返回login.jsp
    }

    @RequestMapping("/syserror")//出错页面
    public String sysError(){
        return "syserror";
    }




    @RequestMapping("/useradd")
    public String showRegisterPage(){
        logger.info("欢迎来到新增用户页面");
        return "useradd";
    }

    @RequestMapping(value = "/useraddsave",method = RequestMethod.POST)
    public String doRegister(User user,HttpSession session){
        //添加用户表的createBy值
        user.setCreatedBy(((User)session.getAttribute("user")).getId());
        //添加用户表的createdDte值
        user.setCreationDate((new Date()));
        if(userService.addUser(user)==true){//如果添加成功就返回
            return "redirect:/userlist";
        }
         return "useradd";//添加不成功则返回注册界面
    }


    //获取用户列表
    @RequestMapping(value="/userlist")
    public String getUserList(Model model,HttpSession session,
                              @RequestParam(value="queryname",required=false) String queryUserName,
                              @RequestParam(value="queryUserRole",required=false) String queryUserRole,
                              @RequestParam(value="pageIndex",required=false) String pageIndex) {
        if(session.getAttribute("user") == null){ //如果用户没有登录就直接来到userlist就回到syserror
            return "redirect:/syserror";
        }
        int _queryUserRole = 0;
        List<User> userList = null;
        //设置页面容量
        int pageSize = 5;
        //当前页码
        int currentPageNo = 1;

        if(queryUserName == null){
            queryUserName = "";
        }
        if(queryUserRole != null && !queryUserRole.equals("")){
            _queryUserRole = Integer.parseInt(queryUserRole);
        }

        if(pageIndex != null){
            try{
                currentPageNo = Integer.valueOf(pageIndex);
            }catch(NumberFormatException e){
                return "redirect:/syserror";
            }
        }
        //总数量(表)
        int totalCount = userService.getUserCount(queryUserName,_queryUserRole);
        //总页数
        PageSupport pages=new PageSupport();
        pages.setCurrentPageNo(currentPageNo);
        pages.setPageSize(pageSize);
        pages.setTotalCount(totalCount);
        int totalPageCount = pages.getTotalPageCount();
        //控制首页和尾页

        //设置分页的每一页的显示从哪里开始
        int start = ((currentPageNo-1) * pageSize);

        if(currentPageNo < 1){
            currentPageNo = 1;
        }else if(currentPageNo > totalPageCount){
            currentPageNo = totalPageCount;
        }

        //若是想要展示出其他的信息,就需要在这部分,把对应的数据或者变量添加到model中,然后去前端设置接受参数即可。
        userList = userService.getUserListByPage(queryUserName,_queryUserRole,start,pageSize);
        model.addAttribute("userList", userList);
        List<Role> roleList = null;
        roleList = roleService.getRoleList();
        model.addAttribute("roleList", roleList);
        model.addAttribute("queryUserName", queryUserName);
        model.addAttribute("queryUserRole", queryUserRole);
        model.addAttribute("totalPageCount", totalPageCount);
        model.addAttribute("totalCount", totalCount);
        model.addAttribute("currentPageNo", currentPageNo);
        return "userlist";
    }

}

代码说明:在上面代码中,注意我们把哪些值放到Model里传给了前端

在这里插入图片描述


4.view层的实现

往WEB-INF的pages文件夹中创建以下几个页面

①.在WEB-INF的pages中创建userlist.jsp页面:

(若是有修改想要展示的列,那么在这里也得进行对应的参数设置,否则无法获取到model中的参数)

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2023/9/18
  Time: 18:52
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@include file="/WEB-INF/pages/common/head.jsp"%>
<div class="right">
    <div class="location">
        <strong>你现在所在的位置是:</strong>
        <span>用户管理页面</span>
    </div>
    <div class="search">
        <form method="post" action="${pageContext.request.contextPath }/userlist">
            <input name="method" value="query" class="input-text" type="hidden">
            <span>用户名:</span>
            <input name="queryname" class="input-text" type="text" value="${queryUserName }">

            <span>用户角色:</span>
            <select name="queryUserRole">
                <c:if test="${roleList != null }">
                    <option value="0">--请选择--</option>
                    <c:forEach var="role" items="${roleList}">
                        <option <c:if test="${role.id == queryUserRole }">selected="selected"</c:if>
                                value="${role.id}">${role.roleName}</option>
                    </c:forEach>
                </c:if>
            </select>

            <input type="hidden" name="pageIndex" value="1"/>
            <input value="查 询" type="submit" id="searchbutton">
            <a href="${pageContext.request.contextPath}/jsp/useradd.jsp" >添加用户</a>
        </form>
    </div>
    <!--用户-->
    <table class="providerTable" cellpadding="0" cellspacing="0">
        <tr class="firstTr">
            <th width="10%">用户编码</th>
            <th width="20%">用户名称</th>
            <th width="10%">性别</th>
            <th width="20%">电话</th>
            <th width="10%">用户角色</th>
            <th width="30%">操作</th>
        </tr>
        <c:forEach var="user" items="${userList }" varStatus="status">
            <tr>
                <td>
                    <span>${user.userCode }</span>
                </td>
                <td>
                    <span>${user.userName }</span>
                </td>
                <td>
                     <span>
                        <c:if test="${user.gender==1}">男</c:if>
                        <c:if test="${user.gender==2}">女</c:if>
                     </span>
                </td>
                <td>
                    <span>${user.phone}</span>
                </td>
                <td>
                    <span>${user.role.roleName}</span>
                </td>
                <td>
                    <span><a class="viewUser" href="javascript:;" userid=${user.id } username=${user.userName }><img src="${pageContext.request.contextPath }/statics/images/read.png" alt="查看" title="查看"/></a></span>
                    <span><a class="modifyUser" href="javascript:;" userid=${user.id } username=${user.userName }><img src="${pageContext.request.contextPath }/statics/images/xiugai.png" alt="修改" title="修改"/></a></span>
                    <span><a class="deleteUser" href="javascript:;" userid=${user.id } username=${user.userName }><img src="${pageContext.request.contextPath }/statics/images/schu.png" alt="删除" title="删除"/></a></span>
                </td>
            </tr>
        </c:forEach>
    </table>
    <input type="hidden" id="totalPageCount" value="${totalPageCount}"/>
    <c:import url="rollpage.jsp">
        <c:param name="totalCount" value="${totalCount}"/>
        <c:param name="currentPageNo" value="${currentPageNo}"/>
        <c:param name="totalPageCount" value="${totalPageCount}"/>
    </c:import>
</div>
</section>

<!--点击删除按钮后弹出的页面-->
<div class="zhezhao"></div>
<div class="remove" id="removeUse">
    <div class="removerChid">
        <h2>提示</h2>
        <div class="removeMain">
            <p>你确定要删除该用户吗?</p>
            <a href="#" id="yes">确定</a>
            <a href="#" id="no">取消</a>
        </div>
    </div>
</div>

<%@include file="/WEB-INF/pages/common/foot.jsp" %>
<script type="text/javascript" src="${pageContext.request.contextPath }/statics/js/userlist.js"></script>

在WEB-INF的jsp中创建rollpage.jsp页面,用来翻页

rollpage.jsp:

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2023/9/18
  Time: 18:53
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    <script type="text/javascript">

    </script>
</head>
<body>
<div class="page-bar">
    <ul class="page-num-ul clearfix">
        <li>共${param.totalCount }条记录&nbsp;&nbsp; ${param.currentPageNo }/${param.totalPageCount }页</li>
        <c:if test="${param.currentPageNo > 1}">
            <a href="javascript:page_nav(document.forms[0],1);">首页</a>
            <a href="javascript:page_nav(document.forms[0],${param.currentPageNo-1});">上一页</a>
        </c:if>
        <c:if test="${param.currentPageNo < param.totalPageCount }">
            <a href="javascript:page_nav(document.forms[0],${param.currentPageNo+1 });">下一页</a>
            <a href="javascript:page_nav(document.forms[0],${param.totalPageCount });">最后一页</a>
        </c:if>
        &nbsp;&nbsp;
    </ul>
    <span class="page-go-form"><label>跳转至</label>
        <input type="text" name="inputPage" id="inputPage" class="page-key" />页
        <button type="button" class="page-btn" onClick='jump_to(document.forms[0],document.getElementById("inputPage").value)'>GO</button>
      </span>
</div>
</body>
<script type="text/javascript" src="${pageContext.request.contextPath }/statics/js/rollpage.js"></script>
</html>

③.打开common包中的head.jsp,修改“用户管理”跳转页面的指向,修改为指向userlist(与controller层定义的映射地址一致)

<li><a href=“${pageContext.request.contextPath }/userlist”>用户管理</a></li>

完整代码如下:

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2023/9/13
  Time: 23:03
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>超市订单管理系统</title>
    <link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath }/statics/css/style.css" />
    <link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath }/statics/css/public.css" />
</head>
<!--头部-->
<header class="publicHeader">
    <h1>超市订单管理系统</h1>
    <div class="publicHeaderR">
        <p><span>下午好!</span><span style="color: #fff21b"> ${sessionScope.user.userCode}</span> , 欢迎你!</p>
        <a href="${pageContext.request.contextPath }/logout">退出</a>
    </div>
</header>
<!--时间-->
<section class="publicTime">
    <span id="time">202391323:11  星期三</span>
    <a href="#">温馨提示:为了能正常浏览,请使用高版本浏览器!(Google Or Firefox)</a>
</section>
<!--主体内容-->
<section class="publicMian ">
    <div class="left">
        <h2 class="leftH2"><span class="span1"></span>功能列表 <span></span></h2>
        <nav>
            <ul class="list">
                <li ><a href="${pageContext.request.contextPath }/pages/bill.do?method=query">订单管理</a></li>
                <li><a href="${pageContext.request.contextPath }/pages/provider.do?method=query">供应商管理</a></li>
                <li><a href="${pageContext.request.contextPath }/userlist">用户管理</a></li>
                <li><a href="${pageContext.request.contextPath }/pages/pwdmodify.jsp">密码修改</a></li>
                <li><a href="${pageContext.request.contextPath }/pages/logout.do">退出系统</a></li>
            </ul>
        </nav>
    </div>
    <input type="hidden" id="path" name="path" value="${pageContext.request.contextPath }"/>
    <input type="hidden" id="referer" name="referer" value="<%=request.getHeader("Referer")%>"/>

④运行服务,可以部署本地tomcat服务,也可以使用mave插件方式运行tomcat服务(这里我用的是这个)

运行服务后,发现报错编码UTF-8的不可映射字符,(若无该bug,可直接去网页车市我们添加的效果)

在这里插入图片描述

可以看到是我们controller包下的UserController.java出现错误

在这里插入图片描述

点击jump source,就可以看到报错的位置

在这里插入图片描述

发现,报错的原因是中文注释部分。因为我们在前几天,就把项目的文件编码设置了(这是前几天的截图)

在这里插入图片描述

但是呢,现在查看一下设置,发现把java包整个都设置成了gbk编码因此会报错

在这里插入图片描述

解决办法:更改到controller包,并且点击apply即可

在这里插入图片描述

重新运行服务,发现就没有问题了

在这里插入图片描述


⑤去网页测试查询效果,已经分页效果

1.默认情况下,查询所有用户

在这里插入图片描述

2.指定用户名,采用模糊查询

在这里插入图片描述

3.精准查询

在这里插入图片描述

4.分页效果

在这里插入图片描述

代码说明:

(1)在userlist.jsp中使用了c:forEach进行迭代(类似于循环)操作

(2)进行翻页的是由rollpage.jsp实现的:

(3)当用户点开"用户角色"右边的下拉菜单时,调用roleList传过来的值显示.

在这里插入图片描述


总结

​ 今天是综合项目超市订单管理系统开发的第三天,我们完成对用户信息的显示和查询,动态SQL查询:用户名称(模糊匹配)、用户角色(精确匹配),还使用了分页显示数据列表把之前学习过的知识,都会应用在这个项目中涉及到多表关联查询、动态SQL、二级缓存机制、分页显示数据,通过今天的学习,希望各位读者可以对整体的分页显示数据流程有个大致的了解,为框架开发打下坚实基础。

​ 想要跟着学习的可以去我的资源里面找对应的文件下载,我的md文件也会发上去,项目文件会上传可以自己跟着学习一下。(ps:前俩天有事,所以今天补上)

作者:Stevedash

发表于:2023年9月19日 12点57分

注:本文内容基于个人学习理解,如有错误或疏漏,欢迎指正。感谢阅读!如果觉得有帮助,请点赞和分享。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

基于SSM框架的《超市订单管理系统》Web项目开发(第三天)用户管理,模糊查询,精准匹配,分页显示数据 的相关文章

  • 如何将本机库链接到 IntelliJ 中的 jar?

    我正在尝试在 IntelliJ 中设置 OpenCV 但是我一直在弄清楚如何告诉 IntelliJ 在哪里可以找到本机库位置 在 Eclipse 中 添加 jar 后 您可以在 Build Config 屏幕中设置 Native 库的位置
  • 如何让 BlazeDS 忽略属性?

    我有一个 java 类 它有一个带有 getter 和 setter 的字段 以及第二对 getter 和 setter 它们以另一种方式访问 该字段 public class NullAbleId private static final
  • 序列的排列?

    我有具体数量的数字 现在我想以某种方式显示这个序列的所有可能的排列 例如 如果数字数量为3 我想显示 0 0 0 0 0 1 0 0 2 0 1 0 0 1 1 0 1 2 0 2 0 0 2 1 0 2 2 1 0 0 1 0 1 1 0
  • 在内存中使用 byte[] 创建 zip 文件。 Zip 文件总是损坏

    我创建的 zip 文件有问题 我正在使用 Java 7 我尝试从字节数组创建一个 zip 文件 其中包含两个或多个 Excel 文件 应用程序始终完成 没有任何异常 所以 我以为一切都好 当我尝试打开 zip 文件后 Windows 7 出
  • 如何使用assertEquals 和 Epsilon 在 JUnit 中断言两个双精度数?

    不推荐使用双打的assertEquals 我发现应该使用带有Epsilon的形式 这是因为双打不可能100 严格 但无论如何我需要比较两个双打 预期结果和实际结果 但我不知道该怎么做 目前我的测试如下 Test public void te
  • 如何获取之前的URL?

    我需要调用我的网络应用程序的 URL 例如 如果有一个从 stackoverflow com 到我的网站 foo com 的链接 我需要 Web 应用程序 托管 bean 中的 stackoverflow 链接 感谢所有帮助 谢谢 并不总是
  • jQuery AJAX 调用 Java 方法

    使用 jQuery AJAX 我们可以调用特定的 JAVA 方法 例如从 Action 类 该 Java 方法返回的数据将用于填充一些 HTML 代码 请告诉我是否可以使用 jQuery 轻松完成此操作 就像在 DWR 中一样 此外 对于
  • 谷歌应用程序引擎会话

    什么是java应用程序引擎 默认会话超时 如果我们将会话超时设置为非常非常长的时间 会不会产生不良影响 因为谷歌应用程序引擎会话默认情况下仅存储在数据存储中 就像facebook一样 每次访问该页面时 会话仍然永远存在 默认会话超时设置为
  • java.lang.IllegalStateException:应用程序 PagerAdapter 更改了适配器的内容,而没有调用 PagerAdapter#notifyDataSetChanged android

    我正在尝试使用静态类将值传递给视图 而不是使用意图 因为我必须传递大量数据 有时我会收到此错误 但无法找出主要原因是什么 Error java lang IllegalStateException The application s Pag
  • 没有 Spring 的自定义 Prometheus 指标

    我需要为 Web 应用程序提供自定义指标 问题是我不能使用 Spring 但我必须使用 jax rs 端点 要求非常简单 想象一下 您有一个包含键值对的映射 其中键是指标名称 值是一个简单的整数 它是一个计数器 代码会是这样的 public
  • java.lang.IllegalStateException:提交响应后无法调用 sendRedirect()

    这两天我一直在尝试找出问题所在 我在这里读到我应该在代码中添加一个返回 我做到了 但我仍然得到 java lang IllegalStateException Cannot call sendRedirect after the respo
  • 无法创建请求的服务[org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]-MySQL

    我是 Hibernate 的新手 我目前正在使用 Spring boot 框架并尝试通过 hibernate 创建数据库表 我知道以前也问过同样的问题 但我似乎无法根据我的环境找出如何修复错误 休眠配置文件
  • 在 junit 测试中获取 javax.lang.model.element.Element 类

    我想测试我的实用程序类 ElementUtils 但我不知道如何将类作为元素获取 在 AnnotationProcessors 中 我使用以下代码获取元素 Set
  • Eclipse Maven Spring 项目 - 错误

    I need help with an error which make me crazy I started to study Java EE and I am going through tutorial on youtube Ever
  • 像 Java 这样的静态类型语言中动态方法解析背后的原因是什么

    我对 Java 中引用变量的动态 静态类型和动态方法解析的概念有点困惑 考虑 public class Types Override public boolean equals Object obj System out println i
  • 当 OnFocusChangeListener 应用于包装的 EditText 时,TextInputLayout 没有动画

    不能比标题说得更清楚了 我有一个由文本输入布局包裹的 EditText 我试图在 EditText 失去焦点时触发一个事件 但是 一旦应用了事件侦听器 TextInputLayout 就不再对文本进行动画处理 它只是位于 editText
  • 使用 AsyncTask 传递值

    我一直在努力解决这个问题 但我已经到了不知道该怎么办的地步 我想做的是使用一个类下载文件并将其解析为字符串 然后将该字符串发送到另一个类来解析 JSON 内容 所有部件都可以单独工作 并且我已经单独测试了所有部件 我只是不知道如何将值发送到
  • 专门针对 JSP 的测试驱动开发

    在理解 TDD 到底是什么之前 我就已经开始编写测试驱动的代码了 在没有实现的情况下调用函数和类可以帮助我以更快 更有效的方式理解和构建我的应用程序 所以我非常习惯编写代码 gt 编译它 gt 看到它失败 gt 通过构建其实现来修复它的过程
  • 我如何在java中读取二进制数据文件

    因此 我正在为学校做一个项目 我需要读取二进制数据文件并使用它来生成角色的统计数据 例如力量和智慧 它的设置是让前 8 位组成一个统计数据 我想知道执行此操作的实际语法是什么 是不是就像读文本文件一样 这样 File file new Fi
  • Spring Rest 和 Jsonp

    我正在尝试让我的 Spring Rest 控制器返回jsonp但我没有快乐 如果我想返回 json 但我有返回的要求 完全相同的代码可以正常工作jsonp我添加了一个转换器 我在网上找到了用于执行 jsonp 转换的源代码 我正在使用 Sp

随机推荐

  • 个人理财 第七章 理财师的工作流程和方法 8.33%

    第七章 理财师的工作流程和方法
  • 堆排序与新元素插入(对应王道考研数据结构)

    文章目录 代码 主要针对王道的代码增加了新元素的插入 上浮 操作 测试1 对大 小 根堆插入 上浮 一个新元素 时间复杂度与树的高度h有关 根据完全二叉树的性质 O l o g 2
  • 16_C#正则表达式之06将邮箱中的名字替换成星号

    using System using System Collections Generic using System Linq using System Text using System Threading Tasks using Sys
  • gscale

    一 处理可变数量的输入和输出 利用nargin和nargout进行检测 T testhv 4 5 该函数体中使用nargin返回2 使用nargout返回1 函数nargchk可用于一个M函数体中 以检测传递函数的参量数目是否正确 msg
  • Linux脚本调试

    输出脚本运行每行的命令及状态 bash x 脚本名 或在脚本开头加上 bin bash x 仅对脚本中部分内容调试 set x set x bash 脚本名 运行脚本前先打印脚本 bash v 脚本名 或在脚本开头加上 bin bash v
  • String类

    String类 String 类的特点 字符串一旦初始化就不会被改变 1 获取 1 1 获取字符串中字符的个数 长度 int length 1 2 根据位置获取字符 char charAt int index 1 3 根据字符 串 获取在字
  • django返回html标签

    记录一下 使用from django utils html import format html return format html 例子 from django contrib import admin from models impo
  • python 多版本虚拟环境的安装以及常见报错的处理(解决python2.7 - pip wheel failed with error code 2 问题)

    安装 虚拟环境 注意虚拟环境的 包安装 不要加sudo sudo会装在全局 注意虚拟环境的 包安装 不要加sudo sudo会装在全局 注意虚拟环境的 包安装 不要加sudo sudo会装在全局 1 虚拟环境的创建和使用 项目1 gt 依赖
  • 【vscode】快捷键一键生成vue模板

    vscode 快捷键一键生成vue模板 点击File gt Preferences gt User Snippets 搜索框搜索vue 打开vue json 会有如下代码 可自行配置 可默认 Place your snippets for
  • Webkit for Android分析

    转自 http mogoweb net archives 182 网上有许多webkit的分析文章 其中针对android porting的一篇文章WebKit WebKit For Android 写的非常好 分析得非常深入 不过这篇文章
  • 二、svg文字之排版

    1 transform rotate 90 80 80 的使用
  • 【Segment Anything Model】一:SAM分割任何事物模型官网使用介绍

    点击订阅专栏 查看专栏列表和对应知识点 本文为seg SAM系列文章 在持续更新 文章目录 1 简明扼要 2 分割效果 3 开始探索 Segment Anything Model的核心愿景 Segment Anything Model已经实
  • [TPAMI‘21] Heatmap Regression via Randomized Rounding

    paper https arxiv org pdf 2009 00225 pdf code https github com baoshengyu H3R 总结 本文提出一套编解码方法 编码 random round整数化 激活点响应值表征
  • AI行业快报:人工智能最新最全资讯!

    大家好 我是写作机器人小智 这是我自己写的文章哦 所有AI行业快报1秒生成 AI行业动态 近日 阿联酋驻华大使与搜狗公司IoT事业部产品总监 就中阿人工智能发展 未来合作等话题展开了交流 期间 搜狗旅行翻译宝直接承担了同传的角色 搜狗旅行翻
  • Python3.8.1的安装和运行

    01Python 3 8 1的安装和运行 在Windows10系统下安装python 1 1访问www python org网站 在浏览器地址栏中输入www python org访问网站 1 2下载python版本 根据自己的需要或者喜好下
  • 无压力轻松使用Obsidian写newsletter

    quail平台特点 拥有四大特点 开源 人工智能增强 web3和加密货币集成 超越电子邮件 开源 每个人都应该可以访问驱动其工具的代码 通过我们的服务 您可以放心地了解背后发生的事情 人工智能增强 为了创建一个真正智能的工具 我们知道需要在
  • Android Fragment

    Android实习札记 4 Fragment 碎片 基本概念解析 转载请注明出处 coder pig Fragment相信大家都不会陌生吧 侧滑啦 抽屉效果啦 DialogFragment啊等等 一堆地方都会 用到Fragment 对于Fr
  • “GetInputName“: 不是 “Ort::Session“ 的成员

    项目场景 使用C 和ONNXruntime部署深度学习模型 问题描述 作者在尝试使用onnxruntime和C 部署深度学习模型推理的时候 按照官网的文档对于Ort Session Run的定义 如下 需要获得模型输入层与输出层的名字 st
  • 如何根据SF6气体压力温度曲线,决定不同温度下断路器的充气压力?

    如何根据SF6气体压力温度曲线 决定不同温度下断路器的充气压力 答 SF6断路器的额定压力般为0 4 0 6MPa 表压 通常这时指环境温度为20 时的压力值 温度不同时 SF6气体的压力也不同 充气或检查时必须查对SF6气体温度压力曲线
  • 基于SSM框架的《超市订单管理系统》Web项目开发(第三天)用户管理,模糊查询,精准匹配,分页显示数据

    基于SSM框架的 超市订单管理系统 Web项目开发 第三天 用户管理 模糊查询 精准匹配 分页显示数据 昨天我们完善了登录的功能模块和退出的功能模块 今天我们将实现超市订单管理系统的用户管理功能模块中的用户列表和查询用户功能 今天要完成的功