Springboot+Spring Data JPA 实现分页查询

创建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.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.wxy97.shop</groupId>
    <artifactId>springboot_shop</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot_shop</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
      
        <!--开发阶段使用:springboot热启动(热部署):项目有更改,自动编译:需配置idea   true生效-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

进行相关配置application.yml

server:
  port: 1106
spring:
  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")
@Data
public 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
    Page<User> findAll(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;

@SpringBootTest
class SpringbootShopApplicationTests {

    @Resource
    UserRepository userRepository;

    @Test
    void contextLoads() {
        int pagenum=1; //页码
        int pagesize=3; //每页条数
        Pageable pageable = PageRequest.of(pagenum-1,pagesize);
        Page<User> users = userRepository.findAll(pageable);
        for (User user : users) {
            System.out.println(user.getName());
        }
    }

}

1711751405.png

最后修改:2020 年 01 月 05 日 03 : 59 PM
如果觉得我的文章对你有用,请随意赞赏