스프링부트 JPA CF서비스 자동연결

1 개요[ | ]

Binding to Data Services with Spring Boot JPA
스프링부트 JPA CF서비스 연결
  • 스프링부트 애플리케이션에서 JPA로 CF서비스(MySQL, PostgreSQL)에 연결 가능
  • 프로젝트 생성시 dependencies에서 JPA 선택
spring-boot-starter-data-jpa

2 방법[ | ]

  • Movie 클래스
@Entity
public class Movie {
	@Id
	private Integer id;
	private String title;
	private String image;
	private Integer year;
	private String plot;

	public Movie() {}
	public Movie(Integer id, String title, String image, Integer year, String plot) {
		this.id = id;
		this.title = title;
		this.image = image;
		this.year = year;
		this.plot = plot;
	}
	
	public String toString() {
		return "Movie [id=" + id + ", title=" + title + ", image=" + image + ", year=" + year + ", plot=" + plot + "]";
	}
}
  • MovieRepository 인터페이스
import org.springframework.data.jpa.repository.JpaRepository;

public interface MovieRepository extends JpaRepository<Movie, Integer> {}
  • MyController 클래스
@RestController
public class MyController {
	@Autowired MovieRepository movieRepository;

	@RequestMapping("/")
	public String home() {
		return "Welcome, home!<hr>" + movieRepository.findAll().toString();
	}
}

3 같이 보기[ | ]

4 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}