Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

No Format
# application.properties
spring.jpa.hibernate.ddl-auto=createupdate
spring.datasource.url=jdbc:mysql://localhost:3306/spring
spring.datasource.username=test
spring.datasource.password=test1234

...

  • update JPA에서 정의한 데이터모델과, 실제 데이터베이스의 모델에 변경이 있을때 반영됩니다. 

  • create 매번 데이터베이스를 생성하지만, 어플리케이션이 닫힐때 드롭하지 않습니다.

  • create-drop 매번 데이터베이스를 생성하고, 세션이 닫힐때 자동으로 데이터베이스를 드롭합니다.

...

Code Block
languagejava
themeEmacs
collapsetrue
package com.psmon.springdb;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity // This tells Hibernate to make a table out of this class
public class User {
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private Integer id;
	
	private String name;
	
	private String email;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

}


Image Added

실제 DataBase에서는 위와같은 테이블이, 어플리케이션 시작시 자동 생성됩니다.


CRUD 저장소생성

Code Block
languagejava
themeEmacs
collapsetrue
package com.psmon.springdb;

import org.springframework.data.repository.CrudRepository;

//This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository
//CRUD refers Create, Read, Update, Delete

public interface UserRepository extends CrudRepository<User, Long> {

}