자바 스프링을 배운지 이틀째 날이 되었다. 간략하게 오늘 배운 내용을 정리해 본다.
public class Course {
public String title;
public String tutor;
public int days;
// 이렇게 아무런 파라미터가 없는 생성자를 기본생성자 라고 부릅니다.
public Course() {
}
public Course(String title, String tutor, int days) {
// this 는 "이것" 이죠? 클래스 변수를 가리킵니다.
this.title = title;
this.tutor = tutor;
this.days = days;
}
}
private String title;
// Getter
public String getTitle() {
return this.title;
}
// Setter
public void setTitle(String title) {
this.title = title;
}
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:testdb
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
spring.jpa.show-sql=true
@Entity // 테이블임을 나타냅니다.
public class Lecture {
public Lecture() {}
@Id // ID 값, Primary Key로 사용하겠다는 뜻입니다.
@GeneratedValue(strategy = GenerationType.AUTO) // 자동 증가 명령입니다.
private Long id;
@Column(nullable = false) // 컬럼 값이고 반드시 값이 존재해야 함을 나타냅니다.
private String title;
@Column(nullable = false)
private String tutor;
public Long getId() {
return id;
}
public String getTitle() {
return this.title;
}
public String getTutor() {
return this.tutor;
}
public Lecture(String title, String tutor) {
this.title = title;
this.tutor = tutor;
}
}
public interface LectureRepository extends JpaRepository<Lecture, Long> {
}
@Bean
public CommandLineRunner demo(LectureRepository lectureRepository) {
return (args) -> {
Lecture course = new Lecture("과목 01", "이름 01");
lectureRepository.save(course);
lectureRepository.findAll();
};
}