【问题标题】:Inserting parameter inside HQL and fetch result using SpringBoot在 SQL 中插入参数并使用 Spring Boot 获取结果
【发布时间】:2023-02-23 20:02:27
【问题描述】:

我有一个映射到 Employee 表的 Employee 类

public Class Employee {

  int id;

  String name;

}

我写了一个 hql 查询来从各种参数中获取 Employee..like

Select x from Employee x where x.id = (:id) and x.name = (:name);

我创建了一个地图,其键为 id,名称和值如下所示:

Map<String, Object> params = new HashMap<>();
params.put("id", 1);
params.put("name", "Testname");

如何在 Spring Boot 中发出请求并使用填充了 Map params 中的值的 hql 查询并获取 Employee 对象列表。

编辑:实际上,我想在 HQL 中插入带有键和值的参数映射并获得结果。

【问题讨论】:

    标签: java spring-boot spring-mvc spring-data-jpa


    【解决方案1】:

    是否可以使用 Spring JPA

          @Repository
    public interface EmployeeRepository extends JpaRepository<Employee, Long> {
    @Query("SELECT e FROM Employee e WHERE e.id = :id AND e.name = :name")
        List<Employee> findEmployeesByIdAndName(@Param("id") int id, @Param("name") String name);
    
    }
    

    【讨论】:

    • 不..我知道这个..实际上,我想在 HQL 中插入带有键和值的参数映射并获得结果
    猜你喜欢
    • 2022-01-16
    • 1970-01-01
    • 2018-07-10
    • 2019-04-06
    • 1970-01-01
    • 1970-01-01
    • 2018-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多