Dowon Lee님의 Spring Boot를 이용한 RESTful Web Services 개발 강의를 학습한 내용입니다.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
spring:
datasource:
url: jdbc:h2:mem:testdb
jpa:
show-sql: true
defer-datasource-initialization: true
h2:
console:
enabled: true
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/h2-console/**").permitAll();
http.csrf().disable();
http.headers().frameOptions().disable();
}
//...
}
- localhost:8088/h2-console
insert into user values(1, sysdate(), 'User1', 'test1111', '701010-1111111');
insert into user values(2, sysdate(), 'User2', 'test2222', '801010-1111111');
insert into user values(3, sysdate(), 'User3', 'test3333', '901010-1111111');
@Entity
public class User {
@Id
@GeneratedValue
private Integer id;
//...
}
- @Entity : 해당 클래스 이름으로 DB에 테이블 생성, 클래스 필드 정보로 테이블 생성에 필요한 컬럼 정보로 사용
- @Id : 기본 키
- @GeneratedValue : 자동 생성되는 키 값
@Repository
public interface UserRepository extends JpaRepository<User, Integer> {
}
- JpaRepository<다룰 Entity, 기본 키 타입>
@RestController
@RequestMapping("/jpa")
public class UserJpaController {
private UserRepository userRepository;
public UserJpaController(UserRepository userRepository) {
this.userRepository = userRepository;
}
@GetMapping("/users")
public List<User> retrieveAllUsers(){
return userRepository.findAll();
}
}
@GetMapping("/users/{id}")
public User retrieveUser(@PathVariable int id){
Optional<User> user = userRepository.findById(id);
if(!user.isPresent()){
throw new UserNotFoundException(String.format("ID{%s} not found", id));
}
return user.get();
}
@GetMapping("/users/{id}")
public EntityModel<User> retrieveUser(@PathVariable int id){
Optional<User> user = userRepository.findById(id);
if(!user.isPresent()){
throw new UserNotFoundException(String.format("ID{%s} not found", id));
}
//HATEOAS
EntityModel<User> model = new EntityModel<>(user.get());
WebMvcLinkBuilder linkTo = WebMvcLinkBuilder.linkTo(
WebMvcLinkBuilder.methodOn(this.getClass()).retrieveAllUsers());
model.add(linkTo.withRel("all-users"));
return model;
}
}
@DeleteMapping("/users/{id}")
public void deleteUser(@PathVariable int id){
userRepository.deleteById(id);
}
insert into user values(90001, sysdate(), 'User1', 'test1111', '701010-1111111');
insert into user values(90002, sysdate(), 'User2', 'test2222', '801010-1111111');
insert into user values(90003, sysdate(), 'User3', 'test3333', '901010-1111111');
// 중복을 피하기 위한 임의값 설정
@PostMapping("/users")
public ResponseEntity<User> createUser(@Valid @RequestBody User user){
User savedUser = userRepository.save(user);
URI location = ServletUriComponentsBuilder.fromCurrentRequest()
.path("/{id}")
.buildAndExpand(savedUser.getId())
.toUri();
return ResponseEntity.created(location).build();
}
insert into post values(10001, 'My first post', 90001);
insert into post values(10002, 'My second post', 90001);
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Post {
@Id
@GeneratedValue
private Integer id;
private String description;
@ManyToOne(fetch = FetchType.LAZY)
@JsonIgnore
private User user;
}
- User : Post -> 1 : (0~N)
- Main(User) : Sub(Post) -> Parent : Child
- @ManyToOne : 다 대 일
- FetchType.LAZY : 지연 로딩 방식
- @JsonIgnore : 외부에 데이터 노출X
public class User {
//...
@OneToMany(mappedBy = "user")
private List<Post> posts;
public User(int id, String name, Date joinDate, String password, String ssn) {
this.id = id;
this.name = name;
this.password = password;
this.ssn = ssn;
}
}
- @OneToMany : 일 대 다
@GetMapping("/users/{id}/posts")
public List<Post> retrieveAllPostsByUser(@PathVariable int id){
Optional<User> user = userRepository.findById(id);
if(!user.isPresent()){
throw new UserNotFoundException(String.format("ID{%s} not found", id));
}
return user.get().getPosts();
}
@Repository
public interface PostRepository extends JpaRepository<Post, Integer> {
}
public class UserJpaController {
@Autowired
private PostRepository postRepository;
//...
@PostMapping("/users/{id}/posts")
public ResponseEntity<Post> createPost(@PathVariable int id, @RequestBody Post post){
Optional<User> user = userRepository.findById(id);
if(!user.isPresent()){
throw new UserNotFoundException(String.format("ID{%s} not found", id));
}
post.setUser(user.get());
Post savedPost = postRepository.save(post);
URI location = ServletUriComponentsBuilder.fromCurrentRequest()
.path("/{id}")
.buildAndExpand(savedPost.getId())
.toUri();
return ResponseEntity.created(location).build();
}
}