๐ GitHub ๋ฐ๋ก๊ฐ๊ธฐ
๊ธฐ๋ฅ | Method | URL | Return |
---|---|---|---|
๋ฉ์ธํ์ด์ง | GET | / | index.html |
๋ฉ๋ชจ ์์ฑ | POST | /api/memos | Memo |
๋ฉ๋ชจ ์กฐํ | GET | /api/memos | List<Memo> |
๋ฉ๋ชจ ๋ณ๊ฒฝ | PUT | /api/memos/{id} | Long |
๋ฉ๋ชจ ์ญ์ | DELETE | /api/memos/{id} | Long |
Spring Boot DevTools
: ์ฝ๋์์ โ ์ดํ๋ฆฌ์ผ์ด์
์ ์ฌ์คํํด์ผ ๋๋ ๋ถํธํ ์ ์ ํด๊ฒฐํ ์ ์๋๋ก ์ ๊ณต๋๋ ๋ชจ๋๐ main > entity > Timestamped.java
@Getter
@MappedSuperclass // ์กฐ์ํด๋์ค
// @EntityListeners -> Application์ @EnableJpaAuditing ์ถ๊ฐํ์
@EntityListeners(AuditingEntityListener.class)
public class Timestamped {
// ์์ฑ์๊ฐ
@CreatedDate
private LocalDateTime createdAt;
// ์์ ์๊ฐ
@LastModifiedDate
private LocalDateTime modifiedAt;
}
๐ main > entity > Memo.java
@Getter
@Entity
@NoArgsConstructor // ๊ธฐ๋ณธ์์ฑ์๋ฅผ ์๋์ผ๋ก ์ถ๊ฐ
public class Memo extends Timestamped {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(nullable = false)
private String username;
@Column(nullable = false)
private String contents;
}
๐ main > repository > MemoRepository.java
public interface MemoRepository extends JpaRepository<Memo, Long> {
}
๐ main > dto > MemoRequestDto
@Getter
public class MemoRequestDto {
private String username;
private String contents;
}
๐ main > controller > MemoController.java
@RestController // @Controller + @ResponseBody -> Jsonํํ์ ๋ฐ์ดํฐ๋ฐํ
@RequiredArgsConstructor // final, @Notnull -> ์์ฑ์ ์๋์์ฑ
public class MemoController {
// service๋จ ๊ฐ์ฒด ๋ถ๋ฌ์ด
private final MemoService memoService;
// ๋ฉ์ธํ์ด์ง - html ๋ฐํ
@GetMapping("/")
public ModelAndView home() {
return new ModelAndView("index");
}
// Memo ์์ฑํ๊ธฐ
@PostMapping("/api/memos")
public Memo createMemo(@RequestBody MemoRequestDto memoRequestDto) {
return memoService.createMemo(memoRequestDto);
}
}
๐ main > service > MemoService.java
@Service
@RequiredArgsConstructor // final, @Notnull -> ์์ฑ์ ์๋์์ฑ
public class MemoService {
private final MemoRepository memoRepository;
// DB ์์
์ฒ๋ฆฌ ์ค, ์ค๋ฅ๋ฐ์ -> ๋ชจ๋ ์์
์์ํ๋ก ๋๋ฆผ
@Transactional
public Memo createMemo(MemoRequestDto memoRequestDto) {
// Memo : ๋ฐํํ์
(Memo ํด๋์ค์ ๊ฐ์ฒด๋ฐํ)
Memo memo = new Memo(memoRequestDto);
memoRepository.save(memo);
return memo;
}
}
๐ main > entity > Memo.java
@Getter
@Entity
@NoArgsConstructor // ๊ธฐ๋ณธ์์ฑ์๋ฅผ ์๋์ผ๋ก ์ถ๊ฐ
public class Memo extends Timestamped {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(nullable = false)
private String username;
@Column(nullable = false)
private String contents;
// MemoRequestDto -> ๊ฐ์ด ๋ฎ์ด์์์ง
public Memo(MemoRequestDto memoRequestDto) {
this.username = memoRequestDto.getUsername();
this.contents = memoRequestDto.getContents();
}
}
@RestController // @Controller + @ResponseBody -> Jsonํํ์ ๋ฐ์ดํฐ๋ฐํ
@RequiredArgsConstructor // final, @Notnull -> ์์ฑ์ ์๋์์ฑ
public class MemoController {
// service๋จ ๊ฐ์ฒด ๋ถ๋ฌ์ด
private final MemoService memoService;
// ๋ฉ์ธํ์ด์ง - html ๋ฐํ
@GetMapping("/")
public ModelAndView home() {
return new ModelAndView("index");
}
// Memo ์์ฑํ๊ธฐ
@PostMapping("/api/memos")
public Memo createMemo(@RequestBody MemoRequestDto memoRequestDto) {
return memoService.createMemo(memoRequestDto);
}
// Memo ์กฐํํ๊ธฐ
@GetMapping("/api/memos")
public List<Memo> getMemos() {
return memoService.getMemos();
}
}
@Service
@RequiredArgsConstructor // final, @Notnull -> ์์ฑ์ ์๋์์ฑ
public class MemoService {
private final MemoRepository memoRepository;
// Memo ์์ฑํ๊ธฐ
// DB ์์
์ฒ๋ฆฌ ์ค, ์ค๋ฅ๋ฐ์ -> ๋ชจ๋ ์์
์์ํ๋ก ๋๋ฆผ
@Transactional
public Memo createMemo(MemoRequestDto memoRequestDto) {
// Memo : ๋ฐํํ์
(Memo ํด๋์ค์ ๊ฐ์ฒด๋ฐํ)
Memo memo = new Memo(memoRequestDto);
memoRepository.save(memo);
return memo;
}
// Memo ์กฐํํ๊ธฐ
@Transactional(readOnly = true)
public List<Memo> getMemos() {
// ์์ฑ์๊ฐ๊ธฐ์ค ๋ด๋ฆผ์ฐจ์์ผ๋ก ๋ฐ์ดํฐ ๊ฐ์ ธ์ด
// Repository๋ ์ฐ๊ฒฐํ์ (๋ฐํํ์
+ ์กฐํ์กฐ๊ฑด)
return memoRepository.findAllByOrderByModifiedAtDesc();
}
}
public interface MemoRepository extends JpaRepository<Memo, Long> {
// Memo ์กฐํํ๊ธฐ
List<Memo> findAllByOrderByModifiedAtDesc();
// ๋ฐํํ์
+ ์กฐํ์กฐ๊ฑด
}
@RestController // @Controller + @ResponseBody -> Jsonํํ์ ๋ฐ์ดํฐ๋ฐํ
@RequiredArgsConstructor // final, @Notnull -> ์์ฑ์ ์๋์์ฑ
public class MemoController {
// service๋จ ๊ฐ์ฒด ๋ถ๋ฌ์ด
private final MemoService memoService;
// ๋ฉ์ธํ์ด์ง - html ๋ฐํ
@GetMapping("/")
public ModelAndView home() {
return new ModelAndView("index");
}
// Memo ์์ฑํ๊ธฐ
@PostMapping("/api/memos")
public Memo createMemo(@RequestBody MemoRequestDto memoRequestDto) {
return memoService.createMemo(memoRequestDto);
}
// Memo ์กฐํํ๊ธฐ
@GetMapping("/api/memos")
public List<Memo> getMemos() {
return memoService.getMemos();
}
// Memo ์์ ํ๊ธฐ
@PutMapping("/api/memos/{id}")
public Long updateMemo(@PathVariable Long id, // RESTful ํต์ ๋ฐฉ๋ฒ
@RequestBody MemoRequestDto memoRequestDto) {
return memoService.updateMemo(id, memoRequestDto);
}
}
@Service
@RequiredArgsConstructor // final, @Notnull -> ์์ฑ์ ์๋์์ฑ
public class MemoService {
private final MemoRepository memoRepository;
// Memo ์์ฑํ๊ธฐ
// DB ์์
์ฒ๋ฆฌ ์ค, ์ค๋ฅ๋ฐ์ -> ๋ชจ๋ ์์
์์ํ๋ก ๋๋ฆผ
@Transactional
public Memo createMemo(MemoRequestDto memoRequestDto) {
// Memo : ๋ฐํํ์
(Memo ํด๋์ค์ ๊ฐ์ฒด๋ฐํ)
Memo memo = new Memo(memoRequestDto);
memoRepository.save(memo);
return memo;
}
// Memo ์กฐํํ๊ธฐ
@Transactional(readOnly = true)
public List<Memo> getMemos() {
// ์์ฑ์๊ฐ๊ธฐ์ค ๋ด๋ฆผ์ฐจ์์ผ๋ก ๋ฐ์ดํฐ ๊ฐ์ ธ์ด
// Repository๋ ์ฐ๊ฒฐํ์ (๋ฐํํ์
+ ์กฐํ์กฐ๊ฑด)
return memoRepository.findAllByOrderByModifiedAtDesc();
}
// Memo ์์ ํ๊ธฐ
@Transactional
public Long updateMemo(Long id, MemoRequestDto memoRequestDto) {
// ํ๋ผ๋ฏธํฐ๋ก ๋ฐ์์จ Id -> DB์ ์กด์ฌํ๋์ง ํ์ธ & ๊ฐ์ฒด์ ๋ด์์ด
// ํ๋ผ๋ฏธํฐ๋ก ๋ฐ์์จ Id -> DB์ ์กด์ฌํ์ง ์์ผ๋ฉด ์์ธ์ฒ๋ฆฌ
Memo memo = memoRepository.findById(id).orElseThrow(
() -> new IllegalArgumentException("์์ด๋๊ฐ ์กด์ฌํ์ง ์์ต๋๋ค.")
);
// Dto๊ฐ -> ๊ธฐ์กด ๊ฐ์ฒด(ํ๋ผ๋ฏธํฐ id๋ก ์ฐพ์)์ ๋ฎ์ด์์ฐ๊ธฐ
memo.update(memoRequestDto);
// Id๊ฐ ๋ฐํ
return memo.getId();
}
}
@Getter
@Entity
@NoArgsConstructor // ๊ธฐ๋ณธ์์ฑ์๋ฅผ ์๋์ผ๋ก ์ถ๊ฐ
public class Memo extends Timestamped {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(nullable = false)
private String username;
@Column(nullable = false)
private String contents;
// Memo ๊ฐ ๋ฎ์ด์์ฐ๋ ์ญํ (์์ฑ์)
public Memo(MemoRequestDto memoRequestDto) {
this.username = memoRequestDto.getUsername();
this.contents = memoRequestDto.getContents();
}
// Memo ์์ ํ๊ธฐ (MemoService -> update)
public void update(MemoRequestDto memoRequestDto) {
this.username = memoRequestDto.getUsername();
this.contents = memoRequestDto.getContents();
}
}
์ด๊ธฐ ์ ์ฒด Memo
Memo update
Memo ์ฌ์กฐํ
@RestController // @Controller + @ResponseBody -> Jsonํํ์ ๋ฐ์ดํฐ๋ฐํ
@RequiredArgsConstructor // final, @Notnull -> ์์ฑ์ ์๋์์ฑ
public class MemoController {
// service๋จ ๊ฐ์ฒด ๋ถ๋ฌ์ด
private final MemoService memoService;
// ๋ฉ์ธํ์ด์ง - html ๋ฐํ
@GetMapping("/")
public ModelAndView home() {
return new ModelAndView("index");
}
// Memo ์์ฑํ๊ธฐ
@PostMapping("/api/memos")
public Memo createMemo(@RequestBody MemoRequestDto memoRequestDto) {
return memoService.createMemo(memoRequestDto);
}
// Memo ์กฐํํ๊ธฐ
@GetMapping("/api/memos")
public List<Memo> getMemos() {
return memoService.getMemos();
}
// Memo ์์ ํ๊ธฐ
@PutMapping("/api/memos/{id}")
public Long updateMemo(@PathVariable Long id, // RESTful ํต์ ๋ฐฉ๋ฒ
@RequestBody MemoRequestDto memoRequestDto) {
return memoService.updateMemo(id, memoRequestDto);
}
// Memo ์ญ์ ํ๊ธฐ
@DeleteMapping("/api/memos/{id}")
public Long deleteMemo(@PathVariable Long id) {
return memoService.deleteMemo(id);
}
}
@Service
@RequiredArgsConstructor // final, @Notnull -> ์์ฑ์ ์๋์์ฑ
public class MemoService {
private final MemoRepository memoRepository;
// Memo ์์ฑํ๊ธฐ
// DB ์์
์ฒ๋ฆฌ ์ค, ์ค๋ฅ๋ฐ์ -> ๋ชจ๋ ์์
์์ํ๋ก ๋๋ฆผ
@Transactional
public Memo createMemo(MemoRequestDto memoRequestDto) {
// Memo : ๋ฐํํ์
(Memo ํด๋์ค์ ๊ฐ์ฒด๋ฐํ)
Memo memo = new Memo(memoRequestDto);
memoRepository.save(memo);
return memo;
}
// Memo ์กฐํํ๊ธฐ
@Transactional(readOnly = true)
public List<Memo> getMemos() {
// ์์ฑ์๊ฐ๊ธฐ์ค ๋ด๋ฆผ์ฐจ์์ผ๋ก ๋ฐ์ดํฐ ๊ฐ์ ธ์ด
// Repository๋ ์ฐ๊ฒฐํ์ (๋ฐํํ์
+ ์กฐํ์กฐ๊ฑด)
return memoRepository.findAllByOrderByModifiedAtDesc();
}
// Memo ์์ ํ๊ธฐ
@Transactional
public Long updateMemo(Long id, MemoRequestDto memoRequestDto) {
// ํ๋ผ๋ฏธํฐ๋ก ๋ฐ์์จ Id -> DB์ ์กด์ฌํ๋์ง ํ์ธ & ๊ฐ์ฒด์ ๋ด์์ด
// ํ๋ผ๋ฏธํฐ๋ก ๋ฐ์์จ Id -> DB์ ์กด์ฌํ์ง ์์ผ๋ฉด ์์ธ์ฒ๋ฆฌ
Memo memo = memoRepository.findById(id).orElseThrow(
() -> new IllegalArgumentException("์์ด๋๊ฐ ์กด์ฌํ์ง ์์ต๋๋ค.")
);
// Dto๊ฐ -> ๊ธฐ์กด ๊ฐ์ฒด(ํ๋ผ๋ฏธํฐ id๋ก ์ฐพ์)์ ๋ฎ์ด์์ฐ๊ธฐ
memo.update(memoRequestDto);
// Id๊ฐ ๋ฐํ
return memo.getId();
}
// Memo ์ญ์ ํ๊ธฐ
@Transactional
public Long deleteMemo(Long id) {
// Id ์กด์ฌ์ฌ๋ถ ํ์ธ -> ์์ธ์ฒ๋ฆฌ
memoRepository.findById(id).orElseThrow(
() -> new IllegalArgumentException("์์ด๋๊ฐ ์กด์ฌํ์ง ์์ต๋๋ค.")
);
// Memo ์ญ์
memoRepository.deleteById(id);
// Id๊ฐ ๋ฐํ
return id;
}
}
Memo ์์ฑ ํ ์กฐํ
Memo ์ญ์
Memo ์กฐํ