2019-02-25

Spring Data JPA专注于使用JPA在关系数据库中存储数据。其最引人注目的功能是能够在运行时从存储库接口自动创建存储库实现
被注解@Entity,表明它是一个JPA实体
@GeneratedValue表示应自动生成ID
@Id,使JPA将其识别为对象的ID
Spring Data JPA如此强大的原因:您不必编写存储库接口的实现。Spring Data JPA在您运行应用程序时动态创建实现。

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity // 这告诉Hibernate从这个类中创建一个表
public class User {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;
    private String name;
    private String email;
import org.springframework.data.repository.CrudRepository;
import hello.User;
// 这将由Spring自动实现为名为userRepository的Bean
// CRUD refers Create, Read, Update, Delete
public interface UserRepository extends CrudRepository<User, Integer> {
}
package com.example.one;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;

@Controller  // 这意味着此类是一个Controller
@RequestMapping(path = "/demo") //这意味着URL以/demo开头(在应用程序路径之后)
public class UserController {

    @Autowired  // 这意味着获取名为userRepository的bean,它由Spring自动生成,我们将使用它来处理数据
    private UserRepository userRepository;

    @GetMapping(path="/add") // Map ONLY GET Requests
    public @ResponseBody String addNewUser(@RequestParam String name, @RequestParam String email){
        // @ResponseBody 表示返回的String是响应,而不是视图名称
        // @RequestParam 表示它是来自GET或POST请求的参数
        User user = new User();
        user.setName(name);
        user.setEmail(email);
        System.out.println(user.toString());

        userRepository.save(user);
        return "Saved";
    }

    @GetMapping(path = "/all")
    public @ResponseBody Iterable<User> getAllUsers(){
        // This returns a JSON or XML with the users
        return userRepository.findAll();

    }

}
<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
   
    <dependencies>

        <!-- JPA Data (We are going to use Repositories, Entities, Hibernate, etc...) -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <!-- Use MySQL Connector-Java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>


        <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>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>

"resources/application.properties"
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3306/spring_data
spring.datasource.username=root
spring.datasource.password=12345678
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 2019年2月是否买房?(个人记录) 老婆希望买一套房子,已有一套在住(可能拆迁,拆迁期间很可能就得租房住,居住环...
    普罗大神阅读 130评论 0 0
  • 如果你想在挤公交的时候,高峰期有座位坐,那么请你叫醒一个睡的特别香的人。告诉他到站了,他一定会飞奔下车,还会不断的...
    子非鱼_f292阅读 236评论 0 0
  • ——夏小芊 震耳欲聋的欢呼声环绕着那个舞台 微弱的灯光照在...
    QueenSunrise阅读 199评论 0 1
  • 本篇文章会就集成腾讯云IM碰到的部分问题进行汇总,问题如下: 问题1:方法数超过63500? 1、在build.g...
    AlexYoD阅读 2,584评论 1 3
  • 边月清光冷,拥衾不忍眠。思君千里外,夜梦可安然?
    胡三多阅读 206评论 0 0