中文字幕精品亚洲无线码二区,国产黄a三级三级三级看三级,亚洲七七久久桃花影院,丰满少妇被猛烈进入,国产小视频在线观看网站

springboot~jpa個性化數據操作(zuo)接口

jap是個全能倉儲

jap把很多數據庫訪問都封裝了,并且提交了默認的一切數據方法簽名的約定,大家按著約定走,可以不寫SQL語句,而如果比較復雜的情況,也需要寫SQL,這里我們介紹一下查詢和修改的實例方法,有一點要注意,倉儲的寫操作是沒有返回值的。

  • 商品倉儲個性接口
/**
 * 產品個性化接口.
 */
@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);
}
  • controller中可以直接調用它,當前IOC這塊于spring框架為我們實現了,直接使用注解即可
@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);

  }
}
  • 對于使用@Query實現寫操作時,需要注釋以下幾點
  1. 方法返回值必須是void
  2. 必須添加 @Transactional和@Modifying注解
  3. SQL代碼里表名和字段名都是 java里的實體名,而不是數據庫的
  • 如果不遵循約定,它將出現下面的異常!
    org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML operations
posted @ 2018-08-01 18:06  張占嶺  閱讀(943)  評論(0)    收藏  舉報