大家好,又见面了,我是你们的朋友全栈君。
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);
}
@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
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/107387.html原文链接:https://javaforall.cn