赞
踩
用idea来演示,新建一个springboot项目
配置信息好之后,单击next
选择依赖,简单选择一下就行
命名项目和位置,之后点击finish
2.导入依赖page-helper
我们需要引入项目的更多依赖,打开项目的pom.xml文件引入依赖,具体代码如下:
- <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.2.5.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>com.example</groupId>
- <artifactId>demo</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>demo</name>
- <description>Demo project for Spring Boot</description>
- <properties>
- <java.version>1.8</java.version>
- </properties>
- <dependencies>
- <!-- thymeleaf -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-thymeleaf</artifactId>
- </dependency>
- <!-- web -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <!-- 整合mybatis所需要的依赖-->
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- <version>2.1.3</version>
- </dependency>
- <!-- pagehelper分页 -->
- <dependency>
- <groupId>com.github.pagehelper</groupId>
- <artifactId>pagehelper-spring-boot-starter</artifactId>
- <version>1.4.1</version>
- </dependency>
- <!-- 数据库驱动 -->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- </dependency>
- <!-- 数据库连接池 -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid</artifactId>
- <version>1.2.4</version>
- </dependency>
- <!-- 热部署devtools -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-devtools</artifactId>
- <scope>runtime</scope>
- <optional>true</optional>
- </dependency>
- <!-- lombok -->
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <optional>true</optional>
- </dependency>
- <!-- 单元测试 -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- </dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- <configuration>
- <excludes>
- <exclude>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- </exclude>
- </excludes>
- </configuration>
- </plugin>
- </plugins>
- </build>
-
- </project>
我这里导入的是1.4.1版本,如果想要更换其他版本在maven仓库中查找
pagehelper的maven仓库网址:https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter
3.新建数据库
我们数据库名称为bills,里面有一张表,名为 tb_customer,如图:
4.新建实体类
我们新建包pojo,在其包下新建 User.java实体类,以及查询类tbCustomerQuery.java ,由于我们在pom.xml中引入了lombok依赖,可以省略getter和setter等方法。
Lombok插件安装
之后在Settings面板下点击 Plugins
安装好lombok插件后,我们的具体代码如下:
- package com.pojo;
-
- import lombok.Data;
-
- /*
- *
- * 加了@Data注解的类,编译后会自动给我们加上下列方法:
- 所有属性的get和set方法
- toString 方法
- hashCode方法
- equals方法
- *
- * @AllArgsConstructor :有参构造方法
- * @NoArgsConstructor :无参构造方法
- * */
-
- @Data
- @AllArgsConstructor
- @NoArgsConstructor
- public class tbCustomer {
- private Integer id;
- private String name;
- private String remark;
- private String telephone;
- private String address;
- private Integer typeId;
- // private String typeName;
- }
5.新建Dao层
我们新建dao包,在其包下新建接口并命名为 tbCustomerDao.java ,具体代码如下:
- package com.mapper;
-
- import com.pojo.tbCustomer;
- import org.apache.ibatis.annotations.Mapper;
- import org.apache.ibatis.annotations.Param;
- import org.springframework.web.bind.annotation.RequestParam;
-
- import java.util.List;
-
-
- @Mapper
- public interface tbCustomerMapper {
-
- /**
- * 带条件分页
- * @param tbCustomer
- * @return
- */
- List<tbCustomer> findPages(tbCustomer tbCustomer);
-
- /**
- * 分页查询
- * @return
- */
- List<tbCustomer> findPage();
-
- /**
- * 查询所有信息
- * @return
- */
- List<tbCustomer> getAll(@Param("name") String name);
-
- /**
- * 新增信息
- * @param tbCustomer
- * @return
- */
- int addTbCustomer(tbCustomer tbCustomer);
-
- /**
- * 删除信息
- */
- int deleteTbCustomer(Integer id);
-
- /**
- * 修改信息
- */
- int updateTbCustomer(tbCustomer tbCustomer);
-
- tbCustomer getById(Integer id);
- }
扩展
如果我们有多个dao接口,我们可以不用在每个接口上都添加 @Mapper 注解,直接在项目启动类上添加
6.新建Mapper
我们在项目resources资源目录下新建一个 mapper 包,并在其包下新建 tbCustomerMapper.xml 文件,编写tbCustomerrDao.java接口对应的sql语句,具体代码如下:
- <?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.mapper.tbCustomerMapper">
- <select id="findPages" resultType="tbCustomer">
- select * from tb_customer
- <trim prefix="where" prefixOverrides="and|or">
- <if test="name!=null and name!='' and name.length!=0">
- and name like concat("%",#{name},"%")
- </if>
- <if test="remark!=null and remark!='' and remark.length!=0">
- and remark like concat("%",#{remark},"%")
- </if>
- </trim>
- </select>
-
- <select id="findPage" resultType="tbCustomer">
- select * from tb_customer
- </select>
- <select id="getAll" resultType="tbCustomer">
- select t.id,t.`name`,t.remark,t.telephone,t.address,t.typeId,b.typeName from tb_customer t,tb_customer_type b
- <where>
- and t.id=b.id
- <if test="name!=null and name!=''">and t.name like concat("%",#{name},"%")</if>
- </where>
- </select>
- <insert id="addTbCustomer">
- insert into tb_customer values(null,#{name},#{remark},#{telephone},#{address},#{typeId})
- </insert>
- <delete id="deleteTbCustomer">
- delete from tb_customer where id=#{id}
- </delete>
- <update id="updateTbCustomer">
- update tb_customer set name=#{name},remark=#{remark},telephone=#{telephone},address=#{address},typeId=#{typeId} where id=#{id}
- </update>
- <select id="getById" resultType="tbCustomer">
- select * from tb_customer where id=#{id}
- </select>
- </mapper>
注意:ideal创建的springboot项目的配置文件默认是properties
格式的,这里我们将其后缀更改为yml
,具体代码如下:
server: port: 8080 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/bills?characterEncoding=utf-8&serverTimezone=UTC username: root password: thymeleaf: mode: HTML5 prefix: classpath:/templates/ suffix: .html encoding: utf-8 cache: false mybatis: mapper-locations: classpath:/mapper/*.xml #mapper文件位置 type-aliases-package: com.pojo # 实体类位置 configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #输出SQL日志 pagehelper: helper-dialect: mysql # 指定分页插件使用哪种语言 reasonable: true # 分页合理化参数,默认为false,当该值为true,pageNum<=0默认查询第一页,pageNum>pages时会查询最后一页,false时直接根据参数进行查询 support-methods-arguments: true # 默认为false, 为true时允许在运行时根据多数据源自动识别对应的方言进行分页 params: countSql # 为了支持startPage(Object params)方法,增加该参数来配置参数映射,用于从对象中根据属性名取值,可以配置pageNum,pageSize,pageSizeZero, reasonable, 不配置映射是使用默认值, 默认值为pageNum=pageNum;pageSize=pageSize;count=countSql;reasonable=reasonable;pageSizeZero=pageSizeZero
新建接口 tbCustomerService.java 以及他的实现类 tbCustomerService.java ,具体代码如下:
- package com.service;
-
- import com.github.pagehelper.PageInfo;
- import com.pojo.tbCustomer;
-
- import java.util.List;
-
-
- public interface tbCustomerService {
-
- PageInfo findPages(tbCustomer tbCustomer,int pageIndex,int pageSize);
-
- PageInfo findPage(int pageIndex,int pageSize);
-
- List<tbCustomer> getAll(String name);
-
- int addTbCustomer(tbCustomer tbCustomer);
-
- int deleteTbCustomer(Integer id);
-
- int updateTbCustomer(tbCustomer tbCustomer);
-
- tbCustomer getById(Integer id);
- }
tbCustomerServiceImpl.java
- package com.service;
-
- import com.github.pagehelper.PageHelper;
- import com.github.pagehelper.PageInfo;
- import com.mapper.tbCustomerMapper;
- import com.pojo.tbCustomer;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- import java.util.List;
-
-
- @Service
- public class tbCustomerServiceImpl implements tbCustomerService{
-
- @Autowired
- private tbCustomerMapper tbCustomerMapper;
-
-
- @Override
- public PageInfo findPages(tbCustomer tbCustomer, int pageIndex, int pageSize) {
- PageHelper.startPage(pageIndex,pageSize);
- List<tbCustomer> pages = tbCustomerMapper.findPages(tbCustomer);
- PageInfo pageInfo=new PageInfo(pages);
- return pageInfo;
- }
-
- @Override
- public PageInfo findPage(int pageIndex, int pageSize) {
- PageHelper.startPage(pageIndex,pageSize);
- List<tbCustomer> page = tbCustomerMapper.findPage();
- PageInfo pageInfo=new PageInfo(page);
- return pageInfo;
- }
-
- @Override
- public List<tbCustomer> getAll(String name){
- return tbCustomerMapper.getAll(name);
- }
-
- @Override
- public int addTbCustomer(tbCustomer tbCustomer) {
- return tbCustomerMapper.addTbCustomer(tbCustomer);
- }
-
- @Override
- public int deleteTbCustomer(Integer id) {
- return tbCustomerMapper.deleteTbCustomer(id);
- }
-
- @Override
- public int updateTbCustomer(tbCustomer tbCustomer) {
- return tbCustomerMapper.updateTbCustomer(tbCustomer);
- }
-
- @Override
- public tbCustomer getById(Integer id) {
- return tbCustomerMapper.getById(id);
- }
- }
我们在resources的templates下新建 index.html 和 page.html 文件,具体代码如下:
index.html
- <!DOCTYPE html>
- <!--suppress ALL-->
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- </head>
- <body>
- <form th:action="@{/getPages}">
- 商品信息<input type="text" name="name" placeholder="请输入你要查询的信息"/>
- 商品描述<input type="text" name="remark"/>
- <input value="查询" type="submit">
- </form>
- <a href="add.html" th:href="@{/add.html}">新增</a>
- <table border="1">
- <tr>
- <td>商品编号</td>
- <td>商品名称</td>
- <td>商品信息</td>
- <td>联系方式</td>
- <td>家庭住址</td>
- <td>会员等级</td>
- <td>操作</td>
- </tr>
- <tr th:each="put:${page.list}">
- <td th:text="${put.id}"></td>
- <td th:text="${put.name}"></td>
- <td th:text="${put.remark}"></td>
- <td th:text="${put.telephone}"></td>
- <td th:text="${put.address}"></td>
- <td th:text="${put.typeId}"></td>
- <td>
- <a th:href="@{'/deleteCustomer/'+${put.id}}">删除</a>
- <a th:href="@{'/toUpdate/'+${put.id}}">修改</a>
- </td>
- </tr>
- </table>
- <div th:insert="page :: page"></div>
- </div>
- </body>
- </html>
page.html
- <!DOCTYPE html>
- <!--suppress ALL-->
- <div th:fragment="page">
- <style>
- .page{
- border: 1px solid blue;
- margin-right: 5px;
- padding: 3px 5px;
- }
- .page.active{
- color: red;
- border: 1px solid red;
- }
- #div1{
- position: absolute;
- left:420px;
- top:140px;
- }
- </style>
-
- <div style="float: left">
- 当前第<span th:text="${page.pageNum}"></span>页
- 总记录数<span th:text="${page.total}"></span>
- </div>
- <div id="div1">
- <a th:text="首页" th:if="${page.pageNum>1}" th:href="@{${path}}"></a>
- <a th:text="上一页" th:if="${page.pageNum>1}" th:href="@{${path}+${page.pageNum-1}+'&name='+${tbCustomer.name}+'&remark'+${tbCustomer.remark}}"></a>
- <a th:href="@{${path}+${i}+'&name='+${tbCustomer.name}+'&remark'+${tbCustomer.remark}}" th:each="i:${#numbers.sequence(1,page.pages)}" th:text="${i}"
-
- th:class="${page.pageNum==i}? 'page active':'page'"></a>
- <a th:text="下一页" th:if="${page.pageNum<page.pages}" th:href="@{${path}+${page.pageNum+1}+'&name='+${tbCustomer.name}+'&remark'+${tbCustomer.remark}}"></a>
- <a th:text="尾页" th:if="${page.pageNum<page.pages}" th:href="@{${path}+${page.pages}+'&name='+${tbCustomer.name}+'&remark'+${tbCustomer.remark}}"></a>
- </div>
-
- </div>
- </html>
我们新建 UserController.java ,具体代码如下:
- package com.controller;
-
- import com.github.pagehelper.PageInfo;
- import com.pojo.tbCustomer;
- import com.service.tbCustomerServiceImpl;
- import com.util.Constants;
- import com.util.PageSupport;
- import org.apache.ibatis.annotations.Param;
- import org.springframework.beans.factory.annotation.Autowired;
- 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.RequestParam;
-
- import javax.servlet.http.HttpSession;
- import java.util.List;
-
- /**
- * @Author:李润成
- * @Date:2022-09-21
- */
- @Controller
- public class tbCustomerController {
-
- @Autowired
- private tbCustomerServiceImpl tbCustomerService;
-
- /**
- * 带条件分页加模糊查询
- * @param model
- * @param tbCustomer
- * @param pageIndex
- * @param pageSize
- * @return
- */
- @RequestMapping("/getPages")
- public String getPages(Model model,tbCustomer tbCustomer,
- @RequestParam(value = "pageIndex",defaultValue = "1") int pageIndex,
- @RequestParam(value = "pageSize",defaultValue = "2",required = false) int pageSize){
-
- PageInfo page = tbCustomerService.findPages(tbCustomer,pageIndex, pageSize);
- model.addAttribute("tbCustomer",tbCustomer);
- model.addAttribute("path","getPages?pageIndex=");
- model.addAttribute("page",page);
- return "index";
- }
-
- /**
- * 普通分页
- * @param model
- * @param pageIndex
- * @param pageSize
- * @return
- */
- @RequestMapping("/getPage")
- public String getPage(Model model,
- @RequestParam(value = "pageIndex",defaultValue = "1") int pageIndex,
- @RequestParam(value = "pageSize",defaultValue = "2",required = false) int pageSize){
- PageInfo page = tbCustomerService.findPage(pageIndex, pageSize);
- model.addAttribute("path","getPage?pageIndex=");
- model.addAttribute("page",page);
- return "index";
- }
-
- /**
- * 模糊查询
- * @param name
- * @param model
- * @return
- * @throws Exception
- */
- @RequestMapping("/getAll")
- public String getAll(@RequestParam(value = "name",required = false)String name, Model model) throws Exception {
- List<tbCustomer> list = tbCustomerService.getAll(name);
- model.addAttribute("name",name);
- model.addAttribute("list", list);
- return "index";
- }
-
- /**
- * 新增信息
- * @param tbCustomer
- * @return
- */
- @RequestMapping("/addCustomer")
- public String addCustomer(tbCustomer tbCustomer) {
- int i = tbCustomerService.addTbCustomer(tbCustomer);
- if (i > 0) {
- return "redirect:/getPages";
- }
- return "redirect:error";
- }
-
- /**
- * 删除信息
- * @param id
- * @return
- */
- @RequestMapping("/deleteCustomer/{id}")
- public String deleteCustomer(@PathVariable("id") Integer id) {
- int i = tbCustomerService.deleteTbCustomer(id);
- if (i > 0) {
- return "redirect:/getPages";
- }
- return "redirect:error";
- }
-
- /**
- * 修改信息
- * @param id
- * @param model
- * @return
- */
- @RequestMapping("/toUpdate/{id}")
- public String toUpdate(@PathVariable("id")Integer id, Model model) {
- tbCustomer byId = tbCustomerService.getById(id);
- model.addAttribute("updateList", byId);
- return "update";
- }
- @RequestMapping("/updateCustomer")
- public String updateCustomer(tbCustomer tbCustomer) {
- int i = tbCustomerService.updateTbCustomer(tbCustomer);
- if (i > 0) {
- return "redirect:/getPages";
- }
- return "redirect:/error";
- }
- /**
- * 统一返回页面代码
- * @param page
- * @return
- */
- @RequestMapping("/{page}.html")
- public String toPage(@PathVariable("page")String page){
- return page;
- }
- }
11.启动项目
我们输入 http://localhost:8080/getPages
进入主界面,
显示当前页数、总页数和总条数,当前是第一页时不显示“上一页”,当前是最后一页时不显示“下一页”,可以输入具体的页面进行跳转
- <!DOCTYPE html>
- <!--suppress ALL-->
- <div th:fragment="page">
- <style>
- .page{
- border: 1px solid blue;
- margin-right: 5px;
- padding: 3px 5px;
- }
- .page.active{
- color: red;
- border: 1px solid red;
- }
- #div1{
- position: absolute;
- left:420px;
- top:140px;
- }
- </style>
-
- <div style="float: left">
- 当前第<span th:text="${page.pageNum}"></span>页
- 总记录数<span th:text="${page.total}"></span>
- </div>
- <div id="div1">
- <a th:text="首页" th:if="${page.pageNum>1}" th:href="@{${path}}"></a>
- <a th:text="上一页" th:if="${page.pageNum>1}" th:href="@{${path}+${page.pageNum-1}+'&name='+${tbCustomer.name}+'&remark'+${tbCustomer.remark}}"></a>
- <a th:href="@{${path}+${i}+'&name='+${tbCustomer.name}+'&remark'+${tbCustomer.remark}}" th:each="i:${#numbers.sequence(1,page.pages)}" th:text="${i}"
-
- th:class="${page.pageNum==i}? 'page active':'page'"></a>
- <a th:text="下一页" th:if="${page.pageNum<page.pages}" th:href="@{${path}+${page.pageNum+1}+'&name='+${tbCustomer.name}+'&remark'+${tbCustomer.remark}}"></a>
- <a th:text="尾页" th:if="${page.pageNum<page.pages}" th:href="@{${path}+${page.pages}+'&name='+${tbCustomer.name}+'&remark'+${tbCustomer.remark}}"></a>
- </div>
-
- </div>
- </html>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。