美文网首页
2019-02-25

2019-02-25

作者: 简洁心飞 | 来源:发表于2019-02-25 14:48 被阅读0次
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-02-25

      本文链接:https://www.haomeiwen.com/subject/jqhgyqtx.html