1 dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
2 实体类
import javax.persistence.*;
@Entity
@Table(name = "t_comment")
public class Comment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String content;
private String author;
@Column(name = "a_id")
private Integer aId;
@Override
public String toString() {
return "Comment{" +
"id=" + id +
", content='" + content + '\'' +
", author='" + author + '\'' +
", aId=" + aId +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Integer getaId() {
return aId;
}
public void setaId(Integer aId) {
this.aId = aId;
}
}
3 repository
public interface CommentRepository extends JpaRepository<Comment,Integer> {
}
4 测试类
@RunWith(SpringRunner.class)
@SpringBootTest
class SpringbootDemoApplicationTests {
@Autowired
private CommentRepository commentRepository;
@Test
public void selectComment(){
Optional<Comment> byId = commentRepository.findById(1);
if(byId.isPresent()){}
System.out.println(byId.get());
}
}
```