2021-11-5 Springboot+Thymeleaf+Bootstrap完成简单管理系统的练习

一、分页功能的实现

 代码结构

 第一种方式:在controller写的分页

controller包代码

package com.example.controller;

import com.example.dao.UserDao;
import com.example.pojo.User;
import com.github.pageHelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class TestController{
    @Resource
    private UserDao ud;

    /*用户管理界面*/
    @RequestMapping("/Mall/ManagementUserByName")
    public String managementUserByName(Model model,HttpServletRequest request,User user){
        String _pageNum = request.getParameter("pageNum");
        String _pageSize = request.getParameter("pageSize");
        int pageNum = 1;
        int pageSize = 10;

        if(_pageNum!=null && !"".equals(_pageNum)){
            pageNum = Integer.parseInt(_pageNum);
        }
        if(_pageSize!=null && !"".equals(_pageSize)){
            pageSize = Integer.parseInt(_pageSize);
        }

        int totalCount = ud.findCountUser(user);
        int totalPageNum = 0;
        if(totalCount%pageSize!=0){
            totalPageNum = totalCount/pageSize+1;
        }else{
            totalPageNum = totalCount/pageSize;
        }

        int offset = 0;
        if(pageNum<=1){
            offset = 0;
        }else {
            offset = (pageNum-1)*pageSize;
        }

        List<User> userList = ud.findAllUserPage(offset,pageSize,user);
        model.addAttribute("pageNum",pageNum);
        model.addAttribute("pageSize",pageSize);
        model.addAttribute("totalCount",totalCount);
        model.addAttribute("totalPageNum",totalPageNum);
        model.addAttribute("ulist",userList);
        return "management/managementUser";
       
    }
}

dao包代码

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

import java.util.List;

public interface UserDao {
    //查
    int findCountUser(User user);
    List<User> findAllUser();
    List<User> findAllUserPage(@Param("userOffset") Integer userOffset, @Param("userPageSize") Integer userPageSize,
                               @Param("user") User user);

}

pojo包代码

package com.example.pojo;

public class User {
    private Integer id;
    private String name;
    private String password;
    private String phone;
    private String address;
    private String photo;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getpassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    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 String getPhoto() {
        return photo;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
    }

    public User(){

    }

    public User(Integer id, String name, String password, String phone, String address, String photo) {
        this.id = id;
        this.name = name;
        this.password = password;
        this.phone = phone;
        this.address = address;
        this.photo = photo;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                ", phone='" + phone + '\'' +
                ", address='" + address + '\'' +
                ", photo='" + photo + '\'' +
                '}';
    }
}

mybatis包代码

<?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.example.dao.UserDao">
    
    <!--分页-->
    <select id="findCountUser" resultType="Integer">
        SELECT count(1) FROM user
    </select>

    <select id="findAllUserPage" resultType="com.example.pojo.User">
        SELECT * FROM user limit ${userOffset} ,${userPageSize}
    </select>

</mapper>

 managementUser.HTML代码

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <Meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <Meta name="viewport" content="width=device-width, initial-scale=1,shrink-to-fit=no">
    <title>用户管理</title>
    <link th:href="@{/css/bootstrap.css}" rel="stylesheet">
    <link th:href="@{/css/font-awesome.css}" rel="stylesheet">
    <script type="text/javascript" th:src="@{/js/jquery-3.5.1.js}"></script>
    <script type="text/javascript" th:src="@{/js/bootstrap.js}"></script>
    <style>
        .navbar2{
            background-color: #3c8dbc;
            border: none;
            border-radius: 0;
            margin-bottom: 0;
        }
        .navbar2 a{
            color: #FFF;
        }

        .nav>li{
            text-align: center;
        }
        .nav>li>a{
            color:#444;
            margin: 0 5px;
        }
        .nav>li>a:hover,.nav>li>a:focus,.nav>li>a:active{
            background-color: #367FA9;
        }
        body {
            width: 100%;
            height: 100%;
            margin: 0;
            overflow: hidden;
            background-color: #FFFFFF;
            font-family: "Microsoft YaHei", sans-serif;
        }
        .panel2{
            background-color: #222D32;
            color: #b8c7ce;
            border: none;
            border-radius: 0;
        }
        .panel2:hover{
            background-color: #1E282C;
            color: #FFF;
        }
        .panel2 a:hover,.panel2 a:active,.panel2 a:focus{
            text-decoration: none;
        }
    </style>
</head>
<body>
    <!--顶部导航栏部分-->
    <nav class="navbar navbar2">
        <div class="container-fluid">

            <div class="collapse navbar-collapse">
                <ul class="nav navbar-nav navbar-right">
                    <li role="presentation" style="padding-top: 25px;">
                        <a th:href="@{#}">当前用户:<span class="badge">管理员</span></a>
                    </li>
                    <li style="padding-top: 25px;">
                        <a th:href="@{#}"><span class="glyphicon glyphicon-lock"></span>退出登录</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <div class="pageContainer" style="bottom: 0;left:0; right: 0; top: 75px;overflow: auto;position: absolute;width: 100%;" >
        <!-- 左侧导航栏 -->
        <div class="pageSidebar" style="width: 240px;height:100%;padding-bottom: 30px;padding-top: 40px;margin-top:0;
        background-color: #222D34;float: left;">
            <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
                <!-- 商家管理 -->
                <div class="panel2">
                    <div class="panel-heading" role="tab" style="text-align: center;">
                        <h4 class="panel-title">
                            <a role="button" th:href="@{/Mall/ManagementMerchantByName}">
                                <i class="fa fa-group"></i>商家管理
                            </a>
                        </h4>
                    </div>
                </div>

                <!-- 用户管理 -->
                <div class="panel2">
                    <div class="panel-heading" role="tab" style="text-align: center;">
                        <h4 class="panel-title">
                            <i class="fa fa-user-circle-o" aria-hidden="true"></i>用户管理
                        </h4>
                    </div>
                </div>

                <!-- 数据检测统计 -->
                <div class="panel2">
                    <div class="panel-heading" role="tab" style="text-align: center;">
                        <h4 class="panel-title">
                            <a role="button" th:href="@{/Mall/CountMerchant}" >
                                <i class="fa fa-bar-chart" aria-hidden="true"></i>商家销售额
                            </a>
                        </h4>
                    </div>
                </div>

                <!-- 系统维护 -->
                <div class="panel2">
                    <div class="panel-heading" role="tab" style="text-align: center;">
                        <h4 class="panel-title">
                            <a role="button" th:href="@{/Mall/Maintenance}">
                                <i class="fa fa-bar-chart" aria-hidden="true"></i>系统维护
                            </a>
                        </h4>
                    </div>
                </div>
            </div>
        </div>

        <!-- 左侧导航和正文内容分隔线 -->
        <div class="splitter" style="width: 5px;height: 100%;bottom: 0;left: 240px;position: absolute;overflow: hidden;background-color: #fff;"></div>

        <!-- 正文内容部分 -->
        <div style="margin-left: 245px;width: 1200px;align-content: center;padding-top: 15px;">
            <table width="1000px" border="1" class="table table-hover table-striped" style="Box-shadow:2px 2px 2px #ddd">
                <thead>
                    <tr>
                        <th class="text-center">姓名</th>
                        <th class="text-center">密码</th>
                        <th class="text-center">手机号码</th>
                        <th class="text-center">地址</th>
                        <th class="text-center">头像</th>
                        <th class="text-center">操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr th:each="user:${ulist}">
                        <td align="center" th:text="${user.name}"></td>
                        <td align="center" th:text="${user.password}"></td>
                        <td align="center" th:text="${user.phone}"></td>
                        <td align="center" th:text="${user.address}"></td>
                        <td align="center" th:text="${user.photo}"></td>
                        <td align="center">
                            <button type="button" class="btn btn-primary">
                                <a th:href="@{'/Mall/goUpdateUser/'+${user.name}}" style="color:#FFFFFF">修改</a>
                            </button>
                            <button type="button" class="btn btn-primary">
                                <a th:href="@{'/Mall/goDeleteUser/'+${user.name}}" style="color:#FFFFFF">删除</a>
                            </button>
                        </td>
                    </tr>
                </tbody>
            </table>
            <!--分页部分-->
            <!--<span th:text="${page}"></span>-->
            <div class="text-center" style="margin-top: -10px;">
                <ul class="pagination">
                    <li><a th:if="${pageNum==1}" href="#">&laquo;</a></li>
                    <li><a th:if="${pageNum!=1}" th:href="${'/Mall/ManagementUserByName?pageNum='+(pageNum-1)+'&pageSize='+pageSize}">&laquo;</a></li>
                    <li th:each="i:${#numbers.sequence(1,totalPageNum)}" th:class="${pageNum==i?'active':''}">
                        <a th:href="${'/Mall/ManagementUserByName?pageNum='+i+'&pageSize='+pageSize}" th:text="${i}"></a></li>
                    <li><a th:if="${pageNum==totalPageNum}" href="#">&raquo;</a></li>
                    <li><a th:if="${pageNum!=totalPageNum}" th:href="${'/Mall/ManagementUserByName?pageNum='+(pageNum-1)+'&pageSize='+pageSize}">&raquo;</a></li>
                </ul>
            </div>


        </div>
    </div>
</body>
</html>

第二种方式:继承BaseController

*********在pom.xml引入依赖************

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.6</version>
        </dependency>
        <dependency>
            <groupId>com.github.pageHelper</groupId>
            <artifactId>pageHelper-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>

Controller包代码

package com.example.controller;

import com.example.dao.UserDao;
import com.example.pojo.User;
import com.github.pageHelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class TestController extends BaseController {
    @Resource
    private UserDao ud;

    /*用户管理界面*/
    @RequestMapping("/Mall/ManagementUserByName")
    public String managementUserByName(Model model,HttpServletRequest request,User user){
       
        buildPage();
        List<User> userList = ud.findAllUserPage2(user);
        PageInfo<User> page = new PageInfo<>(userList);

        model.addAttribute("pageNum",page.getPageNum());
        model.addAttribute("pageSize",page.getPageSize());
        model.addAttribute("totalPageNum",page.getPages());
        model.addAttribute("totalCount",page.getTotal());
        model.addAttribute("page",page);
        model.addAttribute("ulist",userList);
        model.addAttribute("user",user);
        return "management/managementUser";
    }

    @RequestMapping("/Mall/goUpdateUser/{name}")//去修改用户信息页面
    public String goUpdateUser(@PathVariable("name") String name, Model model){
        User user = ud.findByNameUser(name);
        model.addAttribute("user",user);
        return "management/updateUser";
    }
    @RequestMapping("/Mall/UpdateUser")//修改用户信息
    @ResponseBody
    public boolean updateUser(User user){
        int i = ud.updateUser(user);
        if (i>0){
            return true;
        }else {
            return false;
        }
    }
    @RequestMapping("/Mall/goDeleteUser/{name}")//删除用户信息
    public String goDeleteUser(@PathVariable("name") String name){
        ud.deleteUser(name);
        return "success";
    }


}
package com.example.controller;

import com.github.pageHelper.pageHelper;
import org.apache.tomcat.util.http.fileupload.RequestContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

public class BaseController {

    /**
     * 这里必须在调用select语句前调用
     */
    protected void buildPage() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        int pageNum = 1;
        int pageSize = 5;
        try {
            pageNum = Integer.parseInt(request.getParameter("pageNum"));
            pageSize = Integer.parseInt(request.getParameter("pageSize"));
        } catch (NumberFormatException e) {
            System.out.println("****************页面参数传入有误*****************");
        }

        pageHelper.startPage(pageNum,pageSize);
    }
}

dao包代码

package com.example.dao;

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

import java.util.List;

public interface UserDao {

    List<User> findAllUserPage2(User user);

}

pojo包一样

mybatis包下UserMapper.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.example.dao.UserDao">
    
    <select id="findAllUserPage2" resultType="com.example.pojo.User">
        SELECT * FROM user
    </select>
</mapper>

前端分页代码

<ul class="pagination">
    <li><a th:if="${pageNum==1}" href="#">&laquo;</a></li>
    <li><a th:if="${pageNum!=1}" th:href="${'/Mall/ManagementUserByName?pageNum='+(pageNum-1)+'&pageSize='+pageSize}">&laquo;</a></li>
    <li th:each="i:${#numbers.sequence(1,totalPageNum)}" th:class="${pageNum==i?'active':''}">
                        <a th:href="${'/Mall/ManagementUserByName?pageNum='+i+'&pageSize='+pageSize}" th:text="${i}"></a></li>
    <li><a th:if="${pageNum==totalPageNum}" href="#">&raquo;</a></li>
    <li><a th:if="${pageNum!=totalPageNum}" th:href="${'/Mall/ManagementUserByName?pageNum='+(pageNum-1)+'&pageSize='+pageSize}">&raquo;</a></li>
</ul>

代码效果

相关文章

Bootstrip HTML 查询搜索常用格式模版 &lt;form class=&...
如何在按钮上加红色数字 您可以使用Bootstrap的badge组件来在...
要让两个按钮左右排列,你可以使用 Bootstrap 的网格系统将它...
是的,可以将status设置为布尔类型,这样可以在前端使用复选...
前端工程师一般用的是Bootstrap的框架而不是样式,样式一般自...
起步导入:<linkrel="stylesheet"href="b...