一、账单
1.导入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ts</groupId>
<artifactId>billmappers</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.0.0.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
</project>
2.spring boot 启动器
package com.ts;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
/**
* @author 森哥
*/
@SpringBootApplication
@MapperScan("com.ts.dao")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
3.application.properties:springboot全局配置文件
#连接池
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: root
url: jdbc:mysql://localhost:3306/bill-mapper
thymeleaf:
cache: false #关闭thymeleaf的缓存
#整合mybatis
mybatis:
type-aliases-package: com.ts.entity #别名
mapper-locations: classpath:/mappers/*.xml
4.实体类
4.1账单类型
package com.ts.entity;
/**
* @Author: 森哥
* @Description:
* @Date Created in 2021-09-15 11:20
* @Modified By:
*/
import javax.persistence.*;
@Table(name = "bill_type_")
public class BillType {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_")
private Long id;
@Column(name = "name_")
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
4.2账单
package com.ts.entity;
import javax.persistence.*;
import java.util.Date;
/**
* @Author: 森哥
* @Description:
* @Date Created in 2021-09-15 11:28
* @Modified By:
*/
@Table(name = "bill_")
public class Bill {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_")
private long id;
@Column(name = "title_")
private String title;
@Column(name = "bill_time_")
private Date billTime;
@Column(name = "type_id_")
private long typeId;
@Column(name = "price_")
private Double price;
@Column(name = "explain_")
private String explain;
@Transient
private String date1;
/**
* 开始时间
*/
@Transient
private Date date2;
/**
* 结束时间
*/
@Transient
private Date endTime;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Date getBillTime() {
return billTime;
}
public void setBillTime(Date billTime) {
this.billTime = billTime;
}
public long getTypeId() {
return typeId;
}
public void setTypeId(long typeId) {
this.typeId = typeId;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String getExplain() {
return explain;
}
public void setExplain(String explain) {
this.explain = explain;
}
public String getDate1() {
return date1;
}
public void setDate1(String date1) {
this.date1 = date1;
}
public Date getDate2() {
return date2;
}
public void setDate2(Date date2) {
this.date2 = date2;
}
public Date getEndTime() {
return endTime;
}
public void setEndTime(Date endTime) {
this.endTime = endTime;
}
@Override
public String toString() {
return "Bill{" +
"id=" + id +
", title='" + title + '\'' +
", billTime=" + billTime +
", typeId=" + typeId +
", price=" + price +
", explain='" + explain + '\'' +
", date1='" + date1 + '\'' +
", date2=" + date2 +
", endTime=" + endTime +
'}';
}
}
注意 @Transient注解表示当前属性为瞬时属性,跟字段没有映射,上面实体类中date1和date2只作为查询条件存
在,跟表中的字段没有关联
5.dao
5.1BillMapeer和TypeMapper
package com.ts.dao;
import com.ts.entity.Bill;
import tk.mybatis.mapper.common.Mapper;
import java.util.List;
/**
* @Author: 森哥
* @Description:
* @Date Created in 2021-09-15 20:14
* @Modified By:
*/
public interface BillMapper extends Mapper<Bill> {
/**
* 查询
*
* @param bill
* @return java.util.List<com.ts.entity.Bill>
* @author ts
* @date 2021/9/15 21:05
*/
List<Bill> queryBill(Bill bill);
}
package com.ts.dao;
import com.ts.entity.BillType;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.common.Mapper;
/**
* @Author: 森哥
* @Description:
* @Date Created in 2021-09-15 20:14
* @Modified By:
*/
public interface TypeMapper extends Mapper<BillType> {
}
5.2查询修改的映射文件
<?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.ts.dao.BillMapper">
<sql id="selectSql">
SELECT
b.id_ as id,
b.title_ as title,
b.bill_time_ as billTime,
b.type_id_ as typeId,
b.price_ as price,
b.explain_ as `explain`,
t.name_ as typeName
FROM
bill_ as b
left join bill_type_ as t
on b.type_id_ = t.id_
</sql>
<select id="queryBill" resultType="com.ts.entity.Bill">
<include refid="selectSql"/>
<where>
<if test="typeId !=null">b.type_id_ = #{typeId}</if>
<if test="title !=null">and b.title_ like '%${title}%'</if>
<if test="date1 !=null">and b.bill_time_ >= #{date1}</if>
<if test="date2 !=null">and b.bill_time_ <= #{date2}</if>
</where>
</select>
</mapper>
注意:explain字段名在mysql 中是关键词必须使用``包裹
6.service
package com.ts.service;
import com.ts.dao.BillMapper;
import com.ts.entity.Bill;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* @Author: 森哥
* @Description:
* @Date Created in 2021-09-15 21:10
* @Modified By:
*/
@Service
public class BillService {
@Resource
private BillMapper billMapper;
/**
* 根据开始时间和结束时间查询
*
* @param bill
* @return java.util.List<com.ts.entity.Bill>
* @author ts
* @date 2021/9/15 21:18
*/
public List<Bill> queryBill(Bill bill) {
return billMapper.queryBill(bill);
}
/**
* 添加
*
* @param bill
* @return int
* @author ts
* @date 2021/9/15 21:23
*/
public int add(Bill bill) {
return billMapper.insert(bill);
}
/**
* 根据id查询
*
* @param id
* @return com.ts.entity.Bill
* @author ts
* @date 2021/9/15 21:25
*/
public Bill selectBillById(Long id) {
return billMapper.selectByPrimaryKey(id);
}
/**
* @param bill
* @return int
* @author ts
* @date 2021/9/15 21:29
*/
public int updateBillById(Bill bill) {
return billMapper.updateByPrimaryKey(bill);
}
/**
* @param id
* @return int
* @author ts
* @date 2021/9/15 21:30
*/
public int deleteBillById(Long id) {
return billMapper.deleteByPrimaryKey(id);
}
}
package com.ts.service;
import com.ts.dao.TypeMapper;
import com.ts.entity.BillType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* @Author: 森哥
* @Description:
* @Date Created in 2021-09-15 21:11
* @Modified By:
*/
@Service
public class TypeService {
@Resource
private TypeMapper typeMapper;
/**
* @return java.util.List<com.ts.entity.BillType>
* @author ts
* @date 2021/9/15 21:44
*/
public List<BillType> getAll() {
return typeMapper.selectAll();
}
}
7.controller
package com.ts.controller;
import com.ts.entity.Bill;
import com.ts.entity.BillType;
import com.ts.service.BillService;
import com.ts.service.TypeService;
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 javax.annotation.Resource;
import java.util.List;
/**
* @Author: 森哥
* @Description:
* @Date Created in 2021-09-16 21:10
* @Modified By:
*/
@Controller
@RequestMapping("/bill")
public class BillController {
@Resource
private BillService billService;
@Resource
private TypeService typeService;
/**
* 查询类别
*
* @return java.util.List<com.ts.entity.BillType>
* @author ts
* @date 2021/9/16 21:14
*/
public List<BillType> queryTypes() {
return typeService.getAll();
}
/**
* 查询
*
* @param bill
* @param model
* @return java.lang.String
* @author ts
* @date 2021/9/16 21:24
*/
@RequestMapping("/list")
public String query(Bill bill, Model model) {
List<BillType> types = queryTypes();
model.addAttribute("types", types);
List<Bill> list = billService.queryBill(bill);
model.addAttribute("lsit", list);
model.addAttribute("b", bill);
return "/bill/list";
}
/**
* 调到添加页面
*
* @param model
* @return java.lang.String
* @author ts
* @date 2021/9/16 21:26
*/
@RequestMapping("/toAdd")
public String toAdd(Model model) {
List<BillType> types = queryTypes();
model.addAttribute("types", types);
return "/bill/add";
}
/**
* 添加
*
* @param bill
* @return java.lang.String
* @author ts
* @date 2021/9/16 21:27
*/
@RequestMapping("/add")
public String add(Bill bill) {
billService.add(bill);
return "redirect:/bill/list";
}
/**
* 删除操作
*
* @param id
* @return java.lang.String
* @author ts
* @date 2021/9/16 21:31
*/
@RequestMapping("/delete/{id}")
public String deleteById(@PathVariable("id") Long id) {
billService.deleteBillById(id);
return "redirect:/bill/list";
}
/**
* 跳转到修改界面
*
* @param id
* @param model
* @return java.lang.String
* @author ts
* @date 2021/9/16 21:36
*/
@RequestMapping("/toUpdate/{id}")
public String toUpdate(@PathVariable("id") Long id, Model model) {
List<BillType> types = queryTypes();
model.addAttribute("types", types);
Bill bill = billService.selectBillById(id);
model.addAttribute("b", bill);
return "/bill/update";
}
/**
* @param bill
* @return java.lang.String
* @author ts
* @date 2021/9/16 21:38
*/
@RequestMapping("/update")
public String update(Bill bill) {
billService.updateBillById(bill);
return "redirect:/bill/list";
}
}
8.页面
8.1 list.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>userList</title>
<link rel="stylesheet" th:href="@{/css/bootstrap.css}"></link>
<script type="text/javascript" th:src="@{/js/My97DatePicker/WdatePicker.js}"></script>
<script type="text/javascript" src="/js/My97DatePicker/lang/zh-cn.js"></script>
<script type="text/javascript" th:src="@{/js/jquery/jquery-1.10.2.min.js}"></script>
</head>
<body class="container">
<br/>
<h1>账单列表</h1>
<br/><br/>
<div class="with:80%">
<!-- TODO 回显查询数据-->
<form class="form-inline" id="qf" method="post">
<div class="form-group">
<label for="typeId" class="control-label">类型</label>
<select name="typeId" id="typeId" class="form-control">
<option value="">全部</option>
<option th:each="t:${types}" th:value="${t.id}" th:text="${t.name}" th:selected="${t.id}+'' == ${#strings.trim(param.typeId)}"></option>
</select>
</div>
<div class="form-group">
<label for="date1" class="control-label">开始时间</label>
<input type="text" class="form-control" name="date1" id="date1" th:value="${param.date1}" placeholder="开始时间" onclick="WdatePicker()"/>
</div>
<div class="form-group">
<label for="date2" class="control-label">结束时间</label>
<input type="text" class="form-control" name="date2" id="date2" th:value="${param.date2}" placeholder="结束时间" onclick="WdatePicker()"/>
</div>
<div class="form-group">
<input type="submit" value="查询" class="btn btn-info"/>
<input type="reset" value="重置" class="btn btn-info"/>
<a href="/bill/toAdd" th:href="@{/bill/toAdd}" class="btn btn-info">添加</a>
</div>
</form>
</div>
<br/>
<div class="with:80%">
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>#</th>
<th>标题</th>
<th>时间</th>
<th>金额</th>
<th>类别</th>
<th>说明</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<!-- TODO 回显查询结果-->
<tr th:each="b, bstatus : ${bills}" th:style="${bstatus.odd} ? 'background-color: #A3C6C8'" th:object="${b}">
<th scope="row" th:text="${b.id}">id</th>
<td th:text="${b.title}">name</td>
<td th:text="${b.billTime} ? ${#dates.format(b.billTime, 'yyyy-MM-dd')}">time</td>
<td th:text="${b.price}">price</td>
<td th:text="${b.typeName}">typeName</td>
<td th:text="${b.explain}">explain</td>
<td>
<a th:href="|/bill/toUpdate/*{id}|">修改</a>
<a th:href="|/bill/delete/*{id}|">删除</a>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
todo:th:selected=”${t.id}+’’ == ${#strings.trim(param.typeId)}”
t.id要与param.typeId比较两边都有成一样的类型
所以${t.id}可以通过+’’来转换为字符串 param.typeId可以通过内置函数strings.trim()来转换为字符串
8.2 add.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>user</title>
<link rel="stylesheet" th:href="@{/css/bootstrap.css}"></link>
<script type="text/javascript" th:src="@{/js/My97DatePicker/WdatePicker.js}"></script>
<script type="text/javascript" src="/js/My97DatePicker/lang/zh-cn.js"></script>
</head>
<body class="container">
<br/>
<h1>添加账单</h1>
<br/><br/>
<div class="with:80%">
<form class="form-horizontal" action="/bill/add" method="post">
<div class="form-group">
<label for="typeId" class="col-sm-2 control-label">类型</label>
<div class="col-sm-10">
<select name="typeId" id="typeId" class="form-control">
<option th:each="t:${types}" th:text="${t.name}" th:value="${t.id}"></option>
<!-- TODO 回显账单类型 -->
</select>
</div>
</div>
<div class="form-group">
<label for="title" class="col-sm-2 control-label" >标题</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="title" id="title" placeholder="标题"/>
</div>
</div>
<div class="form-group">
<label for="billTime" class="col-sm-2 control-label">日期</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="billTime" id="billTime" placeholder="日期" onclick="WdatePicker()"/>
</div>
</div>
<div class="form-group">
<label for="price" class="col-sm-2 control-label">金额</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="price" id="price" placeholder="金额"/>
</div>
</div>
<div class="form-group">
<label for="explain" class="col-sm-2 control-label">说明</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="explain" id="explain" placeholder="说明"/>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" value="保存" class="btn btn-info" />
<input type="reset" value="重置" class="btn btn-info" />
</div>
</div>
</form>
</div>
</body>
</html>
8.3 update.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>user</title>
<link rel="stylesheet" th:href="@{/css/bootstrap.css}"></link>
<script type="text/javascript" th:src="@{/js/My97DatePicker/WdatePicker.js}"></script>
<script type="text/javascript" src="/js/My97DatePicker/lang/zh-cn.js"></script>
</head>
<body class="container">
<br/>
<h1>修改账单</h1>
<br/><br/>
<div class="with:80%">
<form class="form-horizontal" action="/bill/update" method="post" th:object="${bill}">
<input type="hidden" name="id" th:value="*{id}" >
<div class="form-group">
<label for="typeId" class="col-sm-2 control-label">类型</label>
<div class="col-sm-10">
<select name="typeId" id="typeId" class="form-control">
<option value="">全部</option>
<option th:each="t:${types}" th:value="${t.id}" th:text="${t.name}" th:selected="${t.id}+'' == ${typeId}"></option>
</select>
</div>
</div>
<div class="form-group">
<label for="title" class="col-sm-2 control-label" >标题</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="title" id="title" th:value="*{title}" placeholder="标题"/>
</div>
</div>
<div class="form-group">
<label for="billTime" class="col-sm-2 control-label">日期</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="billTime" id="billTime" th:value="*{#dates.format(billTime,'yyyy-MM-dd')}" placeholder="日期" onclick="WdatePicker()"/>
</div>
</div>
<div class="form-group">
<label for="price" class="col-sm-2 control-label">金额</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="price" th:value="*{price}" id="price" placeholder="金额"/>
</div>
</div>
<div class="form-group">
<label for="explain" class="col-sm-2 control-label">说明</label>
<div class="col-sm-10">
<input type="text" class="form-control" th:value="*{explain}" name="explain" id="explain" placeholder="说明"/>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" value="保存" class="btn btn-info" />
<input type="reset" value="重置" class="btn btn-info" />
</div>
</div>
</form>
</div>
</body>
</html>
8.4 分页
引入pageHelper分页插件的starter,注意:必须引入pagehelper的启动器,不能直接引入pagehelper
8.41 引入依赖
<!-- 分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
8.4.2分页方法的测试
@Test
public void findPage() {
Bill b = new Bill();
b.setTitle("a");
PageInfo<Bill> page = PageHelper.startPage(2, 10).doSelectPageInfo(() -> {
billService.list(b);
});
page.getList().forEach(bill -> {
System.out.println(bill.getId() + " " + bill.getTitle());
});
System.out.println("总行数=" + page.getTotal());
System.out.println("当前页=" + page.getPageNum());
System.out.println("每页行数=" + page.getPageSize());
System.out.println("总页数=" + page.getPages());
System.out.println("起始行数=" + page.getStartRow());
System.out.println("是第一页=" + page.isIsFirstPage());
System.out.println("是最后页=" + page.isIsLastPage());
System.out.println("还有下一页=" + page.isHasNextPage());
System.out.println("还有上一页=" + page.isHasPreviousPage());
System.out.println("页码列表" + Arrays.toString(page.getNavigatepageNums()));
}
8.4.3service中的分页方法
/**
* @param bill
* @param pageNum
* @param pageSize
* @return com.github.pagehelper.PageInfo<com.ts.entity.Bill>
* @author ts
* @date 2021/9/17 20:35
*/
public PageInfo<Bill> listPage(Bill bill, int pageNum, int pageSize) {
return PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> {
billMapper.queryBill(bill);
});
}
8.4.4controller中的方法
/**
* @param pageNum
* @param pageSize
* @param bill
* @param model
* @return java.lang.String
* @author ts
* @date 2021/9/17 20:41
*/
@RequestMapping("/list-page")
public String pageList(
@RequestParam(defaultValue = "1") int pageNum,
@RequestParam(defaultValue = "10") int pageSize,
Bill bill, Model model) {
List<BillType> types = queryTypes();
model.addAttribute("types", types);
PageInfo<Bill> page = billService.listPage(bill, pageNum, pageSize);
model.addAttribute("page", page);
model.addAttribute("bill", bill);
return "/bill/list-page";
}
8.4.5页面部分分页逻辑示意图
8.4.6 list-page.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>userList</title>
<link rel="stylesheet" th:href="@{/css/bootstrap.css}"></link>
<script type="text/javascript" th:src="@{/js/My97DatePicker/WdatePicker.js}"></script>
<script type="text/javascript" src="/js/My97DatePicker/lang/zh-cn.js"></script>
<script type="text/javascript" th:src="@{/js/jquery/jquery-1.10.2.min.js}"></script>
</head>
<body class="container">
<br/>
<h1>账单列表</h1>
<br/><br/>
<div class="with:80%">
<form class="form-inline" id="qf" th:action="@{/bill/list-page}" method="post">
<!-- TODO 分页相关参数-->
<input type="hidden" name="pageNum" id="pageNum" th:value="${page.pageNum}">
<input type="hidden" name="pageSize" id="pageSize" th:value="${page.pageSize}">
<div class="form-group">
<label for="typeId" class="control-label">类型</label>
<select name="typeId" id="typeId" class="form-control">
<option value="">全部</option>
<option th:each="t:${types}" th:value="${t.id}" th:text="${t.name}"
th:selected="${t.id}+'' == ${#strings.trim(param.typeId)}"></option>
</select>
</div>
<div class="form-group">
<label for="date1" class="control-label">开始时间</label>
<input type="text" class="form-control" name="date1" id="date1" placeholder="开始时间" onclick="WdatePicker()"/>
</div>
<div class="form-group">
<label for="date2" class="control-label">结束时间</label>
<input type="text" class="form-control" name="date2" id="date2" placeholder="结束时间" onclick="WdatePicker()"/>
</div>
<div class="form-group">
<input type="submit" value="查询" class="btn btn-info"/>
<input type="reset" value="重置" class="btn btn-info"/>
<a href="/bill/toAdd" th:href="@{/bill/toAdd}" class="btn btn-info">添加</a>
</div>
</form>
</div>
<br/>
<div class="with:80%">
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>#</th>
<th>标题</th>
<th>时间</th>
<th>金额</th>
<th>类别</th>
<th>说明</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<!-- TODO 页面循环-->
<tr th:each="b, bstatus : ${page.list}" th:style="${bstatus.odd} ? 'background-color: #A3C6C8'"
th:object="${b}">
<th scope="row" th:text="${b.id}">id</th>
<td th:text="${b.title}">name</td>
<td th:text="${b.billTime} ? ${#dates.format(b.billTime, 'yyyy-MM-dd')}">time</td>
<td th:text="${b.price}">price</td>
<td th:text="${b.typeName}">typeName</td>
<td th:text="${b.explain}">explain</td>
<td>
<a th:href="|/bill/toUpdate/*{id}|">修改</a>
<a th:href="|/bill/delete/*{id}|">删除</a>
</td>
</tr>
</tbody>
</table>
</div>
<!-- TODO 分页工具类-->
<ul class="pagination">
<li>
<button class="btn btn-default" id="first">第一页</button>
</li>
<li>
<button class="btn btn-default" id="prev">上一页</button>
</li>
<li th:each="p:${page.navigatepageNums}">
<button class="btn btn-default" name="pn" th:text="${p}" th:disabled="(${p} == ${page.pageNum})"></button>
</li>
<!-- <li th:each="p:${page.navigatepageNums}">-->
<!-- <button class="btn btn-default" name="pn" th:text="${p}" th:disabled="(${p} == ${page.pageNum})"></button>-->
<!-- </li>-->
<li>
<button class="btn btn-default" id="next">下一页</button>
</li>
<li>
<button class="btn btn-default" id="last">最后页</button>
</li>
</ul>
<!-- TODO 分页的js代码-->
<script>
$(function () {
//当前页
let pageNum = [[${page.pageNum}]];
//最后页
let pageCount = [[${page.pages}]]
//还有下一页
let hasNextPage = [[${page.hasNextPage}]]
//还有有上一页
let hasPreviousPage = [[${page.hasPreviousPage}]]
//按钮事件
$("#first").click(function () {
$("#pageNum").val(1);
$("#qf").submit();
})
$("#prev").click(function () {
$("#pageNum").val(pageNum - 1);
$("#qf").submit();
})
$("#next").click(function () {
$("#pageNum").val(pageNum + 1);
$("#qf").submit();
})
$("#last").click(function () {
$("#pageNum").val(pageCount);
$("#qf").submit();
})
//页码跳转
$("button[name='pn']").click(function () {
$("#pageNum").val($(this).html());
$("#qf").submit();
});
//没有下一页 下一页和最后一页不可用
if (!hasNextPage) {
$("#last").prop("disabled", true);
$("#next").prop("disabled", true);
}
if (!hasPreviousPage) {
$("#first").prop("disabled", true);
$("#prev").prop("disabled", true);
}
})
</script>
</body>
</html>