springboot分页查询_Springboot+Spring Data JPA 实现分页查询

5a8628db6790b1ceaec6c1a47f99b9e5.png

创建springboot项目在pom.xm 添加依赖

<?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.0modelVersion>    <parent>        <groupId>org.springframework.bootgroupId>        <artifactId>spring-boot-starter-parentartifactId>        <version>2.2.2.RELEASEversion>        <relativePath/>     parent>    <groupId>com.wxy97.shopgroupId>    <artifactId>springboot_shopartifactId>    <version>0.0.1-SNAPSHOTversion>    <name>springboot_shopname>    <description>Demo project for Spring Bootdescription>    <properties>        <java.version>1.8java.version>    properties>    <dependencies>        <dependency>            <groupId>org.springframework.bootgroupId>            <artifactId>spring-boot-starter-data-jpaartifactId>        dependency>        <dependency>            <groupId>org.springframework.bootgroupId>            <artifactId>spring-boot-starter-webartifactId>        dependency>        <dependency>            <groupId>org.projectlombokgroupId>            <artifactId>lombokartifactId>            <optional>trueoptional>        dependency>                <dependency>            <groupId>mysqlgroupId>            <artifactId>mysql-connector-javaartifactId>        dependency>                <dependency>            <groupId>org.springframework.bootgroupId>            <artifactId>spring-boot-devtoolsartifactId>            <optional>trueoptional>        dependency>        <dependency>            <groupId>org.springframework.bootgroupId>            <artifactId>spring-boot-starter-testartifactId>            <scope>testscope>            <exclusions>                <exclusion>                    <groupId>org.junit.vintagegroupId>                    <artifactId>junit-vintage-engineartifactId>                exclusion>            exclusions>        dependency>    dependencies>    <build>        <plugins>            <plugin>                <groupId>org.springframework.bootgroupId>                <artifactId>spring-boot-maven-pluginartifactId>            plugin>        plugins>    build>project>

进行相关配置application.yml

server:  port: 1106spring:  datasource:    type: com.zaxxer.hikari.HikariDataSource    driver-class-name: com.mysql.jdbc.Driver    url: jdbc:mysql://127.0.0.1:3306/shop?characterEncoding=utf-8    password: shop    username: shop    hikari:      auto-commit: true  jpa:    hibernate:    #启动项目时会自动根据bean在数据库中生成表      ddl-auto: update

bean包 User.java

package com.wxy97.shop.pojo;import lombok.Data;import javax.persistence.*;import java.io.Serializable;/** * @Author: wxySmile * @Date 20-1-2 下午12:19 */@Entity@Table(name = "s_user")@Datapublic class User implements Serializable {    @Id    @GeneratedValue(strategy = GenerationType.IDENTITY)    private Integer id;    private String username;    private String password;    private boolean mg_state;    private String name;    private String email;    private String tel;}

dao包 UserRepository.java

package com.wxy97.shop.dao;import com.wxy97.shop.pojo.User;import org.springframework.data.jpa.repository.JpaRepository;import java.util.List;/** * @Author: wxySmile * @Date 20-1-2 下午1:09 */public interface UserRepository  extends JpaRepository<User,Integer> {    @Override    PagefindAll(Pageable pageable);}

启动项目后数据库会自动生成表,在数据库中插入数据

INSERT INTO `s_user` (`id`, `email`, `mg_state`, `name`, `password`, `tel`, `username`) VALUES(1, 'wxyrrcj@163.com', b'1', '若如初见', '3579c5bed0f11113ebfbbcfb9371eeb5', '15666666666', 'wxy'),(2, '123@love.com', b'1', '李四', '456', '88888888888', NULL),(3, '123@love.com', b'1', '张三', '123', '123456789', '123'),(4, '123@qq.com', b'0', '王五', '55555', '666666666', '555555'),(5, '456@qq.com', b'0', '赵', '6666', '666666666', '6666'),(6, 'aaa@qq.com', b'1', '我是第二页', '222', '66666666666', '2'),(7, '啊啊啊啊啊啊啊', b'0', '啊啊啊啊啊啊啊啊啊啊', '啊啊啊啊啊啊啊啊啊', '啊啊啊啊啊啊啊啊', '啊啊啊啊啊啊啊啊啊啊');

测试类

package com.wxy97.shop;import com.wxy97.shop.dao.UserRepository;import com.wxy97.shop.pojo.User;import org.junit.jupiter.api.Test;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.data.domain.Page;import org.springframework.data.domain.PageRequest;import org.springframework.data.domain.Pageable;import javax.annotation.Resource;@SpringBootTestclass SpringbootShopApplicationTests {    @Resource    UserRepository userRepository;    @Test    void contextLoads() {        int pagenum=1; //页码        int pagesize=3; //每页条数        Pageable pageable = PageRequest.of(pagenum-1,pagesize);        Page users = userRepository.findAll(pageable);        for (User user : users) {            System.out.println(user.getName());        }    }}
e71105e36f81abe728820360bd39f5f4.png
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值