일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- tomcat
- Spring Batch
- spring batch 코드
- spring boot
- spring jpa
- IntelliJ
- ApplicationPidFileWriter
- Spring Data JPA
- 톰캣
- spring camp
- spring-webmvc
- SuperTypeToken
- spring pid
- Data REST
- spring-mvc
- Spring
- static inner class
- spring boot tomcat
- Spring Data REST
- 코드 리뷰
- spring-webmvc #코드읽기
- JPA mapping
- JUnit
- REST API
- ORM
- JPA
- 세미나
- batch
- spring bean
- docker
- Today
- 1
- Total
- 916,386
woniper
spring boot-3(JPA 설정 및 사용) 본문
[spring] spring boot-1(특징과 기본 설정)
[spring] spring boot-2(프로젝트 구조와 Tomcat 연동 및 proerties사용)
[spring] spring boot-4(Velocity 설정과 사용)
Spring Boot Data JPA 설정
JPA를 따로 설명하지 않겠다. 나도 사용한지 얼마되지 않았고 사실 잘 모른다. 간단하게 사용해본 정도이고 설정하고 간단하게 사용하는 방법만 포스팅 할 것이다.
1. pom.xml
pom.xml에 spring-boot-stater-data-jpa를 추가한다. 예제를 mysql로 사용하기 위해 mysql-connector도 추가한다.
org.springframework.boot spring-boot-starter-data-jpa mysql mysql-connector-java 5.1.31
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를 참고하면된다.
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
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
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 ListgetUserList() { return userRepository.findAll(); } }
5. 프로젝트 실행
User 도메인과 UserRepository, MainController를 작성했다면 프로젝트를 실행하고 http://localhost:8080/users로 접속해보자. 아래와 같이 데이터가 나오면 성공!!
'Spring' 카테고리의 다른 글
spring boot embedded tomcat CORS 적용 (0) | 2015.04.09 |
---|---|
spring boot-4(Velocity 설정과 사용) (0) | 2014.10.25 |
spring boot-2(프로젝트 구조와 Tomcat 연동 및 proerties사용) (7) | 2014.10.25 |
spring boot-1(특징과 기본 설정) (0) | 2014.10.25 |
HATEOAS 사용하기 (1) | 2014.07.27 |