(1) Add Spring Data JPA dependent initiator. Add the Spring Data JPA dependent initiator to the project’s POM.xml file as shown in the following example code


<dependency>

       <groupId>org.springframework.boot</groupId>

       <artifactId>spring-boot-starter-data-jpa</artifactId>

</dependency>

Copy the code

(2) Write ORM entity classes.


@Entity(name = "t_comment")  // Set the ORM entity class and specify the table name for the mapping

public class Comment {

 

    @Id   // Indicates the primary key ID of the mapping

    @GeneratedValue(strategy = GenerationType.IDENTITY) // Set the policy for increasing the primary key

    private Integer id;

    private String content;

    private String author;

 

    @Column(name = "a_id")  // Specify the table field name for the mapping

    private Integer aId;

       // Omit the property getXX() and setXX() methods

         // omit the toString() method

       }

Copy the code

(3) Write Repository interface: CommentRepository


public interface CommentRepository extends JpaRepository<Comment.Integer> {}Copy the code

(4) Test


    @Autowired

    private CommentRepository repository;

 

    @Test

    public void selectComment(a) {

        Optional<Comment> optional = repository.findById(1);

              if(optional.isPresent()){

System.out.println(optional.get());

                }

System.out.println();

 

    }

Copy the code

Print:

<imgsrc=”./images/image-20191227175837216.png” alt=”image-20191227175837216″ style=”zoom:67%;” />

Just learned pull hook education "Java engineers high salary boot camp", see just learned point on the answer. I hope the pull check can push me to the company I want to go to, target: byte!!Copy the code