jap把不少數據庫訪問都封裝了,而且提交了默認的一切數據方法簽名的約定,你們按着約定走,能夠不寫SQL語句,而若是比較複雜的狀況,也須要寫SQL,這裏咱們介紹一下查詢和修改的實例方法,有一點要注意,==倉儲的寫操做是沒有返回值==的。java
/** * 產品個性化接口. */ @Repository public interface ProductDetailRepository extends CrudRepository<ProductDetail, Integer>, PagingAndSortingRepository<ProductDetail, Integer> { @Query("select p from ProductDetail p where UPPER(p.productName) like UPPER(?1)") List search(String term); @Transactional @Modifying @Query("UPDATE ProductDetail p SET p.shortDescription = ?2 WHERE p.productId = ?1") void updateDescrption(int id, String description); }
@RestController @RequestMapping("/products") public class ProductDetailController { private final ProductDetailRepository repository; private final ObjectMapper objectMapper; @Autowired public ProductDetailController(ProductDetailRepository repository, ObjectMapper objectMapper) { this.repository = repository; this.objectMapper = objectMapper; } @PutMapping("{id}") public HttpEntity search(@PathVariable int id, @RequestParam("q") String des) { repository.updateDescrption(id, des); return new ResponseEntity<>(HttpStatus.ACCEPTED); } }
org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML operations