【尚筹网】五、管理员维护

任务清单

  • 分页显示 Admin 数据
    • 不带关键词分页
    • 带关键词分页
  • 新增 Admin
  • 更新 Admin
  • 单条删除 Admin

分页管理管理员信息

目标

将数据库中的 Admin 数据在页面上以分页形式显示。在后端将“带关键词”和“不带关键词”的分页合并为同一套代码

思路

image-20241126223330409

代码

引入 PageHelper
  • 确认是否加入了依赖

    <!-- MyBatis 分页插件 -->
    <dependency>
    	<groupId>com.github.pagehelper</groupId>
    	<artifactId>pagehelper</artifactId>
    </dependency>
    
  • SqlSessionFactoryBean 配置 MyBatis 插件

    image-20241126223941497

<!-- 配置 SqlSessionFactoryBean 整合 MyBatis -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean" >
    <!-- 指定 MyBatis 全局配置文件位置 -->
    <property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
    <!-- 指定 Mapper.xml 配置文件位置 -->
    <property name="mapperLocations" value="classpath:mybatis/mapper/*Mapper.xml" />
    <!-- 配置数据源  -->
    <property name="dataSource" ref="dataSource" />
    <!-- 配置插件 -->
    <property name="plugins" >
       <array>
          <!-- 配置 PageHelper -->
          <bean class="com.github.pagehelper.PageHelper">
             <property name="properties">
                <props>
                   <!-- 配置数据库方言,告诉 PageHelper 当前使用的数据库 -->
                   <prop key="dialect">mysql</prop>
                   <!-- 配置页码的合理化修正,在1~总页数之间修正页码 -->
                   <prop key="reasonable">true</prop>
                </props>
             </property>
          </bean>
       </array>
    </property>
</bean>
AdminMapper 中编写 SQL 语句

image-20241126224731297

<select id="selectAdminByKeyword" resultMap="BaseResultMap">
    select id, login_acct, user_pswd, user_name, email, create_time
    from t_admin
    where login_acct like concat('%',#{keyword},'%')
    or user_name like concat('%',#{keyword},'%')
    or email like concat('%',#{keyword},'%')
</select>
AdminMapper 接口生成方法

image-20241126225604859

List<Admin> selectAdminByKeyword(String keyword);
AdminService

image-20241126230359513

@Override
public PageInfo<Admin> getPageInfo(String keyword, Integer pageNum, Integer pageSize) {
   
    // 1.调用 PageHelper 的静态方法开启分页功能
    // 这里充分体现了 PageHelper 的“非侵入式”设计:原本要做的查询不必有任何修改
    PageHelper.startPage(pageNum, pageSize);
    // 2.执行查询
    List<Admin> list = adminMapper.selectAdminByKeyword(keyword);
    // 3.封装为 PageInfo 对象返回
    return new PageInfo<>(list);
}
AdminHandler

image-20241126230824624

@RequestMapping("/admin/get/page.html")
public String getPageInfo(
       // 使用 @RequestParam 注解的 defaultValue 属性,指定默认值,在请求中没有携带对应参数时使用默认值
       // keyword 默认值使用空字符串,和 SQL 语句配合实现两种情况的适配
       @RequestParam(value = "keyword", defaultValue = "") String keyword,
       // pageNum 默认值使用 1
       @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
       // pageSize 默认值使用 5
       @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
       ModelMap modelMap
){
   
    // 调用 Service 方法获取 PageInfo 对象
    PageInfo<Admin> pageInfo = adminService.getPageInfo(keyword, pageNum, pageSize);
    // 将 PageInfo 对象存入模型
    modelMap.addAttribute(CrowdConstant.ATTR_NAME_PAGE_INFO, pageInfo);
    return "admin-page";
}
页面显示主体

image-20241126234009493

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
    <%@include file="/WEB-INF/include-head.jsp" %>
    <body>

       <%@include file="/WEB-INF/include-nav.jsp" %>
       <div class="container-fluid">
          <div class="row">
             <%@include file="/WEB-INF/include-sidebar.jsp" %>
             <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
                <div class="panel panel-default">
                   <div class="panel-heading">
                      <h3 class="panel-title"><i class="glyphicon glyphicon-th"></i> 数据列表</h3>
                   </div>
                   <div class="panel-body">
                      <form class="form-inline" role="form" style="float:left;">
                         <div class="form-group has-feedback">
                            <div class="input-group">
                               <div class="input-group-addon">查询条件</div>
                               <input class="form-control has-success" type="text"
                                      placeholder="请输入查询条件">
                            </div>
                         </div>
                         <button type="button" class="btn btn-warning"><i class="glyphicon glyphicon-search"></i>
                            查询
                         </button>
                      </form>
                      <button type="button" class="btn btn-danger" style="float:right;margin-left:10px;"><i
                            class=" glyphicon glyphicon-remove"></i> 删除
                      </button>
                      <button type="button" class="btn btn-primary" style="float:right;"
                              onclick="window.location.href='add.html'"><i class="glyphicon glyphicon-plus"></i>
                         新增
                      </button>
                      <br>
                      <hr style="clear:both;">
                      <div class="table-responsive">
                         <table class="table  table-bordered">
                            <thead>
                               <tr>
                                  <th width="30">#</th>
                                  <th width="30"><input type="checkbox"></th>
                                  <th>账号</th>
                                  <th>名称</th>
                                  <th>邮箱地址</th>
                                  <th width="100">操作</th>
                               </tr>
                            </thead>
                            <tbody>
                               <c:if test
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jasonakeke

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值