1、类的结构

2、repository层

package com.yuanfeng.repository;

import com.yuanfeng.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;

/**
 * @ClassName UserRepository
 * @Description T0D0
 * @Author yuanfeng
 * @Date 2019/7/25 9:27
 * @Version 1.0
 **/
public interface UserRepository extends JpaRepository<User,Long> {
}

 

3、Controller层 

package com.yuanfeng.controller;

import com.yuanfeng.entity.User;
import com.yuanfeng.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName UserController
 * @Description T0D0
 * @Author yuanfeng
 * @Date 2019/7/25 9:26
 * @Version 1.0
 **/

@RestController

@RequestMapping(value = "/user")
public class UserController {
    @Autowired
    private UserRepository userJPA;

    /**
     * 数据新增或更新,save方法可以执行添加也可以执行更新,如果需要执行持久化的实体存在主键值则更新数据,如果不存在则添加数据。
     */
    @RequestMapping(value = "/save", method = RequestMethod.GET)
    public User save(User user) {
        return userJPA.save(user);
    }
    @RequestMapping(value = "/find/{pageNumber}/{pageSize}", method = RequestMethod.GET)
    public Page<User> find(@PathVariable Integer pageNumber, @PathVariable Integer pageSize) {

        PageRequest request = new PageRequest(pageNumber, pageSize, Sort.Direction.DESC, "id");
        Page<User> users = userJPA.findAll(request);
        for (User u:
             users) {
            System.out.println(u);
        }

        return users;
    }


}

 

 

 4、maven依赖和数据库结构

<?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">
    <parent>
        <artifactId>easyshop</artifactId>
        <groupId>com.yuanfeng</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.yuanfeng</groupId>
    <artifactId>springdatadpa</artifactId>
    <packaging>war</packaging>

    <name>springdatadpa Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>
        <!--引入MySQL的依赖关系-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--引入JPA的依赖关系-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
    </dependencies>

    <build>
        <finalName>springdatadpa</finalName>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.2.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

5、运行结果