SpringBoot分页插件的使用---小案例(厂安防盗门)

SpringBoot分页插件

业务逻辑:我想要实现对必备材料信息列表的分页展示,包括查询后的信息也要分页展示

效果图
在这里插入图片描述
解决思路:
首先实体类用来接收数据,有一个Dao类用来进行sql查询,有一个servcie(需要传pageNum,PageSiz),serviceImpl继承(里面有关于pagehelper的实现方法),Controller调用给前端传参,大致如此,看代码~

实体类

package com.example.pojo;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Cargo {
    
    
//    cargo_id	int
    private Integer cargoId;
//    cargo_name	varchar
    private String cargoName;
//    cargo_unit	varchar
    private String cargoUnit;
//    cargo_num	varchar
    private Integer cargoNum;
//    cargo_price	double
    private Double cargoPrice;
//    cargo_manufacturer	varchar
    private String cargoManufacturer;
//    c_id	int
    private Integer cId;
//    cargo_date	datetime
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date cargoDate;
//    cargo_operator	varchar
    private String cargoOperator;

    private Category category;
}

分页工具实体类

package com.example.util;

@Data
public class PageInfo<T> {
    
    

    /* 当前页 */
    private int pageNum;

    /* 上一页 */
    private int prePage;

    /* 下一页 */
    private int nextPage;

    /* 总页数 */
    private int pages;

    /* 总条数 */
    private int total;

    /* 每页多少条 */
    private int pageSize;

    /* 查询的起始索引 */
    private int startIndex;

    private List<T> list;


    public PageInfo(int pageNum, int total, int pageSize) {
    
    

        this.pageNum = pageNum;
        this.total = total;
        this.pageSize = pageSize;

        this.prePage = this.pageNum  > 1 ? this.pageNum - 1 : 1;

        /* 总条数 / 每页多少条   */
        this.pages = (this.total / this.pageSize) + (this.total % this.pageSize != 0?1:0);

        /* 下一页 */
        this.nextPage = this.pageNum >= this.pages ? this.pages : this.pageNum + 1;

        /* 起始索引 (当前页 -1 ) * pageSize  */
        this.startIndex = (this.pageNum - 1) * pageSize;
    }
}

DAO层

package com.example.dao;

import com.example.pojo.Cargo;
import com.example.pojo.Category;
import com.example.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public class CargoDao {
    
    

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<Cargo> queryList(PageInfo<Cargo> pageInfo) {
    
    
        List<Cargo> list = jdbcTemplate.query("select * from Cargo limit ?,?",
                new BeanPropertyRowMapper<>(Cargo.class), pageInfo.getStartIndex(), pageInfo.getPageSize()
        );
        return list;
    }

    public int insert(Cargo cargo) {
    
    
        String sql = "insert into cargo (cargo_name,cargo_unit,cargo_num,cargo_price,cargo_manufacturer,c_id,cargo_date,cargo_operator) " +
                "value " +
                "(?,?,?,?,?,?,?,?)";
        int i = jdbcTemplate.update(sql, cargo.getCargoName(), cargo.getCargoUnit(), cargo.getCargoNum(), cargo.getCargoPrice(), cargo.getCargoManufacturer(), cargo.getCId(), cargo.getCargoDate(), cargo.getCargoOperator());
        return i;

    }

    public int delete(int id) {
    
    
        String sql = "delete from cargo where cargo_id = ?";
        int i = jdbcTemplate.update(sql, id);
        return i;
    }

    public int update(Cargo cargo) {
    
    
        String sql = "update cargo set cargo_name = ?,cargo_unit = ?,cargo_num = ?,cargo_price = ?,cargo_manufacturer = ?,c_id = ?,cargo_date = ?,cargo_operator = ? where cargo_id = ?";
        int i = jdbcTemplate.update(sql, cargo.getCargoName(),cargo.getCargoUnit(),cargo.getCargoNum(),cargo.getCargoPrice(),cargo.getCargoManufacturer(),cargo.getCId(),cargo.getCargoDate(),cargo.getCargoOperator(),cargo.getCargoId());
        return i;
    }

    public List<Cargo> queryListByName(String name) {
    
    
        String sql = "select * from cargo where cargo_name like '%" + name + "%'";
        List<Cargo> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Cargo.class));
        return list;
    }

    public List<Cargo> queryListByNameAndTime(String name, Date date) {
    
    
        String sql = "select * from cargo WHERE cargo_name = ? and cargo_date = ?";
        List<Cargo> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Cargo.class), name, date);
        return list;
    }

    public Cargo queryListById(int id) {
    
    
        String sql = "select * from cargo where cargo_id  = ?";
        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Cargo.class), id);
    }

    public int queryRows() {
    
    
        String sql = "select count(*) from cargo";
        int i = jdbcTemplate.queryForObject(sql, int.class);
        return i;
    }
}

Service层–业务逻辑

package com.example.service;

import com.example.pojo.Cargo;
import com.example.util.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.util.List;


public interface CargoService {
    
    
    List<Cargo> queryList(PageInfo<Cargo> pageInfo);

    int insert(Cargo cargo);

    int delete(int i);

    int update(Cargo cargo);

    List<Cargo> queryListByName(String name);

    List<Cargo> queryListByNameAndTime(String name, String time) throws ParseException;

    Cargo queryListById(int id);

    int queryRows();
}

serviceImpl

package com.example.service.impl;

import com.example.dao.CargoDao;
import com.example.pojo.Cargo;
import com.example.service.CargoService;
import com.example.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class CargoServiceImpl implements CargoService {
    
    

    @Autowired
    private CargoDao cargoDao;
    @Override
    public List<Cargo> queryList(PageInfo<Cargo> pageInfo) {
    
    
        return cargoDao.queryList(pageInfo);
    }

    @Override
    public int insert(Cargo cargo) {
    
    
        return cargoDao.insert(cargo);
    }

    @Override
    public int delete(int id) {
    
    
        return cargoDao.delete(id);
    }

    @Override
    public int update(Cargo cargo) {
    
    
        return cargoDao.update(cargo);
    }

    @Override
    public List<Cargo> queryListByName(String name) {
    
    
        return cargoDao.queryListByName(name);
    }

    @Override
    public List<Cargo> queryListByNameAndTime(String name, String time) throws ParseException {
    
    
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse(time);
        return cargoDao.queryListByNameAndTime(name,date);
    }

    @Override
    public Cargo queryListById(int id) {
    
    
        return cargoDao.queryListById(id);
    }

    @Override
    public int queryRows() {
    
    
        return cargoDao.queryRows();
    }

}

Controller–控制层

package com.example.controller;

import com.example.pojo.Cargo;
import com.example.pojo.Category;
import com.example.service.CargoService;
import com.example.service.CategoryService;
import com.example.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/cargo")
public class CargoController {
    
    
    @Autowired
    private CargoService cargoService;

    @Autowired
    private CategoryService categoryService;
    /**
     * 查询全部
     *
     * @param model
     * @return
     */
    @RequestMapping("/queryList")
    public String queryList(Model model,
                            @RequestParam(value = "pageNum",defaultValue = "1",required = false) int pageNum,
                            @RequestParam(value = "pageSize",defaultValue = "3",required = false) int pageSize
    ) {
    
    

        int total = cargoService.queryRows();
        PageInfo<Cargo> pageInfo = new PageInfo<>(pageNum, total, pageSize);
        List<Cargo> list = cargoService.queryList(pageInfo);
        pageInfo.setList(list);
        model.addAttribute("pageInfo", pageInfo);
        list.forEach(System.out::println);
        return "cargo";
    }
    /**
     * 删除
     */
    @RequestMapping("/delete")
    public String delete(int id){
    
    
        int i = cargoService.delete(id);
        return i > 0?"redirect:/cargo/queryList":"error";
    }
    /**
     * 增加
     */
    @RequestMapping("/insert")
    public String insert(Cargo cargo){
    
    
        int i = cargoService.insert(cargo);
        return i > 0 ?"redirect:/cargo/queryList":"error";
    }
    /**
     * 修改
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(Model model,int id){
    
    
       Cargo cargo = cargoService.queryListById(id);
       model.addAttribute("cargo",cargo);
        List<Category> list = categoryService.queryList();
        model.addAttribute("list",list);
        return "updateCargo";
    }

    @RequestMapping("/updateCargo")
    public String update(Cargo cargo){
    
    
        int i = cargoService.update(cargo);
        return i > 0?"redirect:/cargo/queryList":"error";
    }
//保存
    @RequestMapping("/toSave")
    public String toSave(Model model){
    
    
        List<Category> list = categoryService.queryList();
        model.addAttribute("list",list);
        return "insertCargo";
    }
}

HTML—前端主页面
cargo.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>cargo_list</title>
    <link type="text/css" rel="stylesheet" th:href="@{/css/bootstrap.css}"/>
    <script type="text/javascript" th:src="@{/js/jquery-3.6.0.js}"></script>
    <style type="text/css">
        #cargo_div {
      
      
            width: 1500px;
            margin-left: 20px;
            margin-top: 20px;
        }

        #cargo_tb {
      
      
            text-align: center;
        }

        #cargo_tb thead tr th {
      
      
            text-align: center;
        }

        #cargo_tb tbody tr td {
      
      
            line-height: 2;
        }
    </style>
</head>
<body>
<div id="cargo_div">
    <table class="table table-bordered table-hover" id="cargo_tb">
        <thead>
        <tr>
            <th>货物编号8081</th>
            <th>货物名称</th>
            <th>货物计量单位</th>
            <th>货物数量</th>
            <th>货物价格</th>
            <th>生产厂家</th>
            <th>所属分类</th>
            <th>入库时间</th>
            <th>操作员</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <tr th:each="cargo,i : ${pageInfo.list}" th:object="${cargo}">
            <td th:text="${i.count}"></td>
            <td th:text="*{cargoName}"></td>
            <td th:text="*{cargoUnit}"></td>
            <td th:text="*{cargoNum}"></td>
            <td th:text="*{cargoPrice}"></td>
            <td th:text="*{cargoManufacturer}"></td>
            <td th:text="*{cId}"></td>
            <td th:text="*{#dates.format(cargoDate,'yyyy-MM-dd HH:mm:ss')}"></td>
            <td th:text="*{cargoOperator}"></td>
            <td>
                <button type="button" class="btn btn-danger btn-sm"
                        th:onclick="|toCategory(${cargo.cargoId})|">编辑
                </button>
                <button type="button" class="btn btn-danger btn-sm"
                        th:onclick="|deleteCategoryById(${cargo.cargoId})|">删除
                </button>
            </td>
        </tr>
        </tbody>
    </table>
    <button type="button" class="btn btn-success btn-sm" id="cargo_add_btn">添加</button>
    <br>
    <br>
    总条数:<span th:text="${pageInfo.total}"></span>,
    <span th:text="${pageInfo.pageNum}"></span>/<span th:text="${pageInfo.pages}"></span>
    <a href="/cargo/queryList?pageNum=1">首页</a>
    <a th:href="|/cargo/queryList?pageNum=${pageInfo.prePage}&pageSize=${pageInfo.pageSize}|">上一页</a>
    <a th:href="|/cargo/queryList?pageNum=${pageInfo.nextPage}&pageSize=${pageInfo.pageSize}|">下一页</a>
    <a th:href="|/cargo/queryList?pageNum=${pageInfo.pages}&pageSize=${pageInfo.pageSize}|">尾页</a>
</div>
<script type="text/javascript">
<!--    修改-->
    function toCategory(cargoId) {
      
      
        window.location.href = "/cargo/toUpdate?id=" + cargoId;
    }
    //删除
    function deleteCategoryById(cargoId) {
      
      
        confirm("你确定要删除广安防盗门分类吗?") ? window.location.href = "/cargo/delete?id="
            + cargoId : window.location.href = "/cargo/queryList";
    }

    $(function () {
      
      
        $("#cargo_add_btn").click(function () {
      
      
            window.location.href = "/cargo/toSave";
        });
    })
</script>
</body>
</html>

insertCargo.html—添加页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>cargo_add</title>
    <link type="text/css" rel="stylesheet" th:href="@{/css/bootstrap.css}"/>
    <script type="text/javascript" th:src="@{/js/jquery-3.6.0.js}"></script>
    <script language="javascript" type="text/javascript" th:src="@{/js/My97DatePicker/My97DatePicker/WdatePicker.js}"></script>
    <style type="text/css">
        #category_add_div {
      
      
            width: 400px;
            margin-left: 20px;
            margin-top: 20px;
        }

    </style>
</head>
<body>
<div id="category_add_div" class="form-inline">
    <form action="/cargo/insert" method="post" id="category_add_form">
        <div class="form-group">
            <label for="cargo_name">货物名称:</label>
            <input type="text" class="form-control" name="cargoName" id="cargo_name" placeholder="货物名称">
        </div><br><br>
        <div class="form-group">
            <label for="cargo_unit">货物计量单位:</label>
            <input type="text" class="form-control" name="cargoUnit" id="cargo_unit" placeholder="货物计量单位">
        </div><br><br>
        <div class="form-group">
            <label for="cargo_num">货物数量:</label>
            <input type="text" class="form-control" name="cargoNum" id="cargo_num" placeholder="货物数量">
        </div><br><br>
        <div class="form-group">
            <label for="cargo_price">货物价格:</label>
            <input type="text" class="form-control" name="cargoPrice" id="cargo_price" placeholder="货物价格">
        </div><br><br>
        <div class="form-group">
            <label for="cargo_manufacturer">生产厂家:</label>
            <input type="text" class="form-control" name="cargoManufacturer" id="cargo_manufacturer" placeholder="生产厂家">
        </div><br><br>
        <div class="form-group">
            <label for="c_id">所属分类:</label>
            <select name="cId" id="c_id">
                    <option th:each="cate:${list}" th:value="${cate.id}" th:text="${cate.name}"></option>
            </select>
        </div><br><br>
        <div class="form-group">
            <label for="cargo_date">入库时间:</label>
            <input type="text" onClick="WdatePicker({ 
        el:this,dateFmt:'yyyy-MM-dd HH:mm:ss'})" class="Wdate form-control" name="cargoDate" id="cargo_date" placeholder="入库时间">
        </div><br><br>
        <div class="form-group">
            <label for="cargo_operator">操作员:</label>
            <input type="text" class="form-control" name="cargoOperator" id="cargo_operator" placeholder="操作员">
        </div><br><br>
        <button type="submit" class="btn btn-success btn-sm">保存</button>
    </form>
</div>
</body>
</html>

updateCargo—修改页面

<!DOCTYPE html>

<!--suppress ALL-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>修改页面</title>
    <link type="text/css" rel="stylesheet" th:href="@{/css/bootstrap.css}"/>
    <script type="text/javascript" th:src="@{/js/jquery-3.6.0.js}"></script>
    <script language="javascript" type="text/javascript" th:src="@{/js/My97DatePicker/My97DatePicker/WdatePicker.js}"></script>
</head>
<body>
<form action="/cargo/updateCargo" method="post">
    <input type="hidden" name="cargoId" th:value="${cargo.cargoId}">
    名称:<input type="text" th:value="${cargo.cargoName}" name="cargoName"><br>
    单位:<input type="text" th:value="${cargo.cargoUnit}" name="cargoUnit"><br>
    数量:<input type="text" th:value="${cargo.cargoNum}" name="cargoNum"><br>
    价格:<input type="text" th:value="${cargo.cargoPrice}" name="cargoPrice"><br>
    厂家:<input type="text" th:value="${cargo.cargoManufacturer}" name="cargoManufacturer"><br>
    所属分类:
    <select name="cId">
        <option th:each="cate : ${list}" th:value="${cate.id}"
                th:text="${cate.name}" th:selected="${cate.id == cargo.cId}">
        </option>
    </select>
    <br>
    入库时间: <input th:value="${cargo.cargoDate}" class="Wdate"
                 onclick="WdatePicker({ 
        el:this,dateFmt:'yyyy-MM-dd HH:mm:ss'})"
                 type="text" name="cargoDate"><br>
    操作员:<input th:value="${cargo.cargoOperator}" type="text" name="cargoOperator">
    <button type="submit">提交</button>
</form>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/weixin_52859229/article/details/129765291