봄 부팅 + JPA + Mysql-- 사용자 정의 업데이트

 

기본 값이 null 기본 JPA의 xx.save (책)을 사용하는 경우가 통과하지 않는 경우, 방법은 모든 필드를 업데이트, 하나의 필드를 업데이트

 

ID입니다 상태 필드 업데이트 SQL (updata 책 세트 상태 = 1 여기서 ID = 20), 메시지 ID를 삭제 따라

데이터 액세스 레이어

public interface BookRepository extends JpaRepository<Book,Long> {

    @Transactional
    @Modifying
    @Query("update Book b set b.status = ?1 where id = ?2")
    int updateByJPQL(int status, long id);

    @Transactional
    @Modifying
    @Query("delete from Book b where b.id = ?1")
    int deleteByJPQL(long id);
}

Yewuluojiceng 서비스

@Service
public class BookService {

    @Autowired
    private BookRepository bookRepository;
    /**
     * 自定义更新
     * @param status
     * @param id
     * @return
     */
    @Transactional
    public int updateByJPQL(int status, long id) {

        return bookRepository.updateByJPQL(status, id);
    }
    /**
     * 自定义删除
     * @param id
     * @return
     */
    @Transactional
    public int deleteByJPQL(long id) {

        return bookRepository.deleteByJPQL( id);
    }

}

웹 층

@RestController
@RequestMapping("/api/v1")
public class BookApp {

    @Autowired
    private BookService bookService;
    @PostMapping("/books/by")
    public int findBy(@RequestParam int status,@RequestParam long id) {
//        return bookService.findByAuthor(author);
//        return bookService.findByAuthorAndStatus(author, status);
        //return bookService.findByDescriptionEndsWith(description);
        //return bookService.findByJPQL(len);
        return bookService.updateByJPQL(status, id);
        //  return bookService.deleteByJPQL(id);
    }
}

테스트 업데이트

전 업데이트

업데이트

콘솔 출력 :

 

 

게시 된 153 개 원래 기사 · 원 찬양 6 · 전망 2355

추천

출처blog.csdn.net/yangshengwei230612/article/details/103765238