首页 > 解决方案 > 更新 Spring 存储库的最佳实践

问题描述

我有如下所示的 Spring Repo 类:

@Repository
public interface EmployeeRepository
        extends CrudRepository<Employee, Long> {


    @Query("Select s from Employee s where s.Id = ?1")
    Optional<Employee> findEmployeeByID(String Id);


     @Transactional
     @Modifying 
     @Query(" How to DO that")
     UpdateEmployeeDetails()


}

如何有效地更新员工详细信息,因为员工表包含超过 15 个字段我无法为每个字段编写函数,我们如何生成具有通用逻辑的查询来更新

例如:如果我们只想更新名称,那么UpdateEmployeeDetails()将执行此查询:

UPDATE Employee SET firstName=UPPER(firstName) WHERE id in :ids

如果我们要更新名称和地址:

UPDATE Employee SET firstName=UPPER(firstName), Address = "Address" WHERE id in :ids

我们如何有效地做到这一点,因为有可能的字段,所以我们想动态生成查询

标签: javaspringspring-boot

解决方案


推荐阅读