spring boot-3(JPA 설정 및 사용)
Spring Boot Data JPA 설정
JPA를 따로 설명하지 않겠다. 나도 사용한지 얼마되지 않았고 사실 잘 모른다. 간단하게 사용해본 정도이고 설정하고 간단하게 사용하는 방법만 포스팅 할 것이다.
1. pom.xml
pom.xml에 spring-boot-stater-data-jpa를 추가한다. 예제를 mysql로 사용하기 위해 mysql-connector도 추가한다.
1 2 3 4 5 6 7 8 9 | < dependency > < groupid >org.springframework.boot</ groupid > < artifactid >spring-boot-starter-data-jpa</ artifactid > </ dependency > < dependency > < groupid >mysql</ groupid > < artifactid >mysql-connector-java</ artifactid > < version >5.1.31</ version > </ dependency > |
2. jdbc, jpa 설정
application.properties에 아래 설정을 추가한다. 물론 자신에 환경에 맞게 jdbc url과 username, password도 변경된다.
spring.jpa.hibernate.ddl-auto=create
spring.jpa.generate-ddl=false
spring.jpa.show-sql=true
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&charaterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.driverClassName=com.mysql.jdbc.Driver
Spring Boot Data JPA 사용
1. jpa를 사용하기 위한 domain
domain에서 사용된 어노테이션은 jpa에서 제공하는 어노테이션이다. 자세한 설명은 Spring Data JPA를 참고하면된다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | package net.woniper.springboot.domain; import javax.persistence.*; import java.io.Serializable; /** * Created by woniper on 14. 10. 25.. */ @Entity public class User implements Serializable { @Id @GeneratedValue (strategy = GenerationType.AUTO) private Long id; @Column (name = "name" , nullable = false ) private String name; @Column (name = "age" , nullable = false ) private Integer age; public Long getId() { return id; } public void setId(Long id) { this .id = id; } public String getName() { return name; } public void setName(String name) { this .name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this .age = age; } } |
2. import.sql
main > resources > import.sql 파일 생성 후 아래와 같이 입력하고 프로젝트를 실행하면 자동으로 user테이블이 생성되며 import.sql에 쿼리가 실행되어 데이터가 입력된다.
insert into user(name, age) values ('woniper1', 26);
insert into user(name, age) values ('woniper2', 27);
3. repository
repository는 구현체가 없다. interface로 Repository를 사용하는데 메서드명에 기반해 쿼리를 날려준다.
참고 : http://docs.spring.io/spring-data/jpa/docs/1.7.0.RELEASE/reference/html/#jpa.query-methods
1 2 3 4 5 6 7 8 | package net.woniper.springboot.repository; import net.woniper.springboot.domain.User; import org.springframework.data.jpa.repository.JpaRepository; public interface UserRepository extends JpaRepository<User, Long> { } |
4. controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | package net.woniper.springboot.controller; import net.woniper.springboot.domain.User; import net.woniper.springboot.repository.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.util.List; /** * Created by woniper on 14. 10. 25.. */ @Controller @RequestMapping ( "/" ) public class MainController { @Autowired private UserRepository userRepository; @RequestMapping public @ResponseBody String index() { return "Hello Woniper Spring Boot~" ; } @RequestMapping ( "/users" ) public @ResponseBody List<user> getUserList() { return userRepository.findAll(); } } </user> |
5. 프로젝트 실행
User 도메인과 UserRepository, MainController를 작성했다면 프로젝트를 실행하고 http://localhost:8080/users로 접속해보자. 아래와 같이 데이터가 나오면 성공!!
출처 : https://blog.woniper.net/232?category=699184
'Spring Framework > Spring boot' 카테고리의 다른 글
Spring Data JPA 사용하기 (0) | 2020.09.01 |
---|---|
Spring MVC (0) | 2020.09.01 |
spring boot embedded tomcat CORS 적용 (0) | 2020.09.01 |
spring boot-4(Velocity 설정과 사용) (0) | 2020.09.01 |
spring boot-2(프로젝트 구조와 Tomcat 연동 및 proerties사용) (0) | 2020.09.01 |
spring boot-1(특징과 기본 설정) (0) | 2020.09.01 |
스프링부트 애노테이션 정리, annotation 간단 요약 (자세한 내용은 검색해서 확인하고 용도 파악하기) (0) | 2019.01.10 |
Isolation level (정리) (0) | 2018.07.19 |