@PostMapping("/modify/{id}") // get방식
public String modify(@PathVariable Long id, Book book){
// 포스트방식
// book.id(2) --> Table(2) : update
bookService.update(id, book);
return "redirect:/list";
}
}
modify.html
<h2>책 수정하기</h2>
<input type="hidden" name="id" th:value="${book.id}">
<table border="1">
```
에서
```java
<h2>책 수정하기</h2>
<form th:action="@{/modify/{id}(id=${book.id})}" method="post">
<table border="1">
으로변경

@Transactional 필요

@Transactional
public Book update(Long id, Book book){
Optional<Book> optional=bookRepository.findById(book.getId());
if(optional.isPresent()) {
Book dbbook = optional.get(); // DB에서 가져온 Book
dbbook.setTitle(book.getTitle());
dbbook.setPrice(book.getPrice());
dbbook.setAuthor(book.getAuthor());
dbbook.setPage(book.getPage());
//bookRepository.save(dbbook); // update SQL / dbbook이 테이블이라서 세이브안해도 테이블이 변경은 업데이트하는 것 @Transactional 필요
return dbbook; // 리턴할때 update가 됩니다. // 업데이트시 수정이됩니다. (더티체킹) 조금 늦게됩니다.
// jpa에서 알아서하니 편하지만 메모리 체킹하는 시간필요 성능저하
}else {
throw new RuntimeException("Book not found with id:"+book.getId());
}
명시적 save 하는게 나을것같습니다. 살리는게 낫다고 봅니다.
bookRepository.save(dbbook); // update SQL

터미널로 git에 올리는 방법

깃계정 (레파지토리)

git remote add origin https://github.com/LOOKSHINE0205/springboot.git
로컬레파지토리 - 스냅샷(변경된 부분)



error: src refspec main does not match any
error: failed to push some refs to 'https://github.com/LOOKSHINE0205/springboot.git'


git 주소를 바꾸려하면 origin을 삭제
git remote remove set-url

git remote add origin 주소

git push origin main
git push -f origin main (에러시 그냥 강제 덮어쓰기)