Spring CRUD

  • 친구 생성 (POST)
  • 친구 목록 조회 (GET)
  • 친구 정보 변경 (PUT)
  • 친구 정보 삭제 (DELETE)
  • ARC를 이용하여 정상적으로 동작하는지 확인하기.


폴더 및 파일 트리

list


PersonController.java

todaytest > src > main > java > com.test.todaytest > controller > PersonController

package com.test.todaytest.Cotroller;

import com.test.todaytest.domain.Person;
import com.test.todaytest.domain.PersonRepository;
import com.test.todaytest.domain.PersonRequestDto;
import com.test.todaytest.service.PersonService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequiredArgsConstructor
@RestController
public class PersonConroller {

    public final PersonRepository personRepository;
    public final PersonService personService;

    @PostMapping("api/persons")
    public Person createPerson(@RequestBody PersonRequestDto requestDto) {
        Person person = new Person(requestDto);
        return personRepository.save(person);
    }

    @GetMapping("api/persons")
    public List<Person> readPerson() {
        return personRepository.findAll();
    }

    @PutMapping("api/persons/{id}")
    public Long updatePerson(@PathVariable Long id, @RequestBody PersonRequestDto requestDto) {
        return personService.update(id, requestDto);
    }

    @DeleteMapping("api/persons/{id}")
    public Long deletePerson(@PathVariable Long id){
        personRepository.deleteById(id);
        return id;
    }

}




Person.java

todaytest > src > main > java > com.test.todaytest > domain > Person

package com.test.todaytest.domain;

import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.persistence.*;

@NoArgsConstructor
@Getter
@Entity
public class Person {
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Id
    private Long id;

    @Column(nullable = false)
    private String name;

    @Column(nullable = false)
    private String address;

    @Column(nullable = false)
    private int age;

    public Person(PersonRequestDto requestDto) {
        this.name = requestDto.getName();
        this.address = requestDto.getAddress();
        this.age = requestDto.getAge();
    }


    public void update(PersonRequestDto requestDto) {
        this.name = requestDto.getName();
        this.address = requestDto.getAddress();
        this.age = requestDto.getAge();
    }
}




PersonRepository.java

todaytest > src > main > java > com.test.todaytest > domain > PersonRepository

package com.test.todaytest.domain;

import org.springframework.data.jpa.repository.JpaRepository;

public interface PersonRepository extends JpaRepository<Person, Long> {
}




PersonRequestDto.java

todaytest > src > main > java > com.test.todaytest > domain > PersonRequestDto

package com.test.todaytest.domain;

import lombok.Getter;

@Getter
public class PersonRequestDto {

    private String name;
    private String address;
    private int age;
}




PersonService.java

todaytest > src > main > java > com.test.todaytest > domain > PersonService

package com.test.todaytest.service;

import com.test.todaytest.domain.Person;
import com.test.todaytest.domain.PersonRepository;
import com.test.todaytest.domain.PersonRequestDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

@RequiredArgsConstructor
@Service
public class PersonService {

    private final PersonRepository personRepository;

    @Transactional
    public Long update(Long id, PersonRequestDto requestDto) {
        Person person = personRepository.findById(id).orElseThrow(
                () -> new IllegalArgumentException("아이디가 존재하지 않습니다.")
        );
        person.update(requestDto);
        return id;
    }
}




TodaytestApplication.java

todaytest > src > main > java > com.test.todaytest > TodaytestApplication

package com.test.todaytest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@EnableJpaAuditing
@SpringBootApplication
public class TodaytestApplication {

	public static void main(String[] args) {
		SpringApplication.run(TodaytestApplication.class, args);
	}

}




결과




POST

post


GET

get


PUT

put

PUT 확인

putcheck


DELETE

delete

DELETE 확인

deletecheck

Leave a comment