(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%;” />
Learning makes people happy, learning makes people feel ignorant! After more than a month of learning "High Salary Training Camp for Java Engineers", I found that my understanding of each technical point was very superficial and could not go deep. I set a Flag: keep learning for one hour every day, answer three technical questions on the Internet every week, and share what I know.Copy the code