在SpringBoot中将参数插入HQL并获取结果。

huangapple go评论71阅读模式
英文:

Inserting parameter inside HQL and fetch result using SpringBoot

问题

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

public class Employee {

  int id;

  String name;

}

我已经编写了一个HQL查询,以获取各种参数的Employee,例如

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

我已经创建了一个映射,其中键是id和name,值如下:

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

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

英文:

I have a Employee class mapped to Employee table

public Class Employee {

  int id;

  String name;

}

i have written a hql query to fetch Employee from various parameters..like

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

I have create a map with key as id and name and values as below:

Map&lt;String, Object&gt; params = new HashMap&lt;&gt;();
params.put(&quot;id&quot;, 1);
params.put(&quot;name&quot;, &quot;Testname&quot;);

How to make a request in Spring Boot and with a hql query populated with values in Map params and fetch a list of Employee objects.

Edit: actually, I want to insert params Map with key and values in the HQL and get result.

答案1

得分: 1

如果你决定使用 HQL 查询,这个帖子应该能帮到你。
点击这里查看链接

还有一些更好地利用 Spring 和 JpaRepository 强大功能的方法。首先是在你的存储库中声明一个方法,包含你的变量名称,并在你的服务中使用该方法:

Repository

@Repository
Public interface EmployeeRepository extends JpaRepository&lt;Employee, Integer&gt; {
    Employee findByIdAndName(int id, String name);
}

Service

@Service
Public class EmployeeService {
    @Autowired
    private EmployeeRepository empRepo;

    public Employee findByIdAndName(int id, String name) {
        return empRepo.findByIdAndName(id, name);
    }
}

如果你只有一个或两个条件,这样做很棒,但如果条件多于两个,方法名就会变得相当长。另一个建议是使用 Specifications。要使用这些,你还需要在你的存储库中扩展 JpaSpecificationExecutor。你可以创建一个单独的类来存放你的规格,但在这个例子中,我会把它放在服务中。

Repository

@Repository
Public interface EmployeeRepository extends JpaRepository&lt;Employee, Integer&gt;, JpaSpecificationExecutor&lt;Employee&gt; {}

Service

@Service
Public class EmployeeService {
    @Autowired
    private EmployeeRepository empRepo;

    public Employee findByIdAndName(int id, String name) {
        return empRepo.find(idEquals(id).and(nameEquals(name)));
    }

    private Specification&lt;Employee&gt; idEquals(int id) {
        return (root, query, cb) -&gt; cb.equal(root.get(Employee_.id), id);
    }

    private Specification&lt;Employee&gt; nameEquals(String name) {
        return (root, query, cb) -&gt; cb.equal(root.get(Employee_.name), name);
    }
}

请注意,要使用静态元模型 Employee_,你将需要额外的依赖,具体来说是 org.hibernate.jpamodelgen 如果我没记错的话。

英文:

If you’re set on using HQL queries, this thread should be able to help you.
enter link description here

There are also a couple ways to do this that better utilize the power of Spring and JpaRepository. The first is to declare a method in your repository that includes your variable names and use that method in your service:

Repository

@Repository
Public interface EmployeeRepository extends JpaRepository&lt;Employee, Integer&gt; {
    Employee findByIdAndName(int id, String name);
}

Service

@Service
Public class EmployeeService {
    @Autowired
    private EmployeeRepository empRepo;

    public Employee findByIdAndName(int id, String name) {
        return empRepo.findByIdAndName(id, name);
    }
}

This is great if you only have one or two criteria, but more than that and the method name starts to get rather long. The other suggestion is to use Specifications. To use these, you need to also extend JpaSpecificationExecutor in your repo. You can create a separate class to house your specifications, but for this example I’m going to put it in the service.

Repository

@Repository
Public interface EmployeeRepository extends JpaRepository&lt;Employee, Integer&gt;, JpaSpecificationExecutor&lt;Employee&gt; {}

Service

@Service
Public class EmployeeService {
    @Autowired
    private EmployeeRepository empRepo;

    public Employee findByIdAndName(int id, String name) {
        return empRepo.find(idEquals(id).and(nameEquals(name)));
    }

    private Specification&lt;Employee&gt; idEquals(int id) {
        return (root, query, cb) -&gt; cb.equal(root.get(Employee_.id), id);
    }

    private Specification&lt;Employee&gt; nameEquals(String name) {
        return (root, query, cb) -&gt; cb.equal(root.get(Employee_.name), name);
    }

}

Note that to use the static metamodel Employee_ you will need extra dependencies, specifically org.hibernate.jpamodelgen if memory serves.

答案2

得分: 0

是不是可以使用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);
}
英文:

Is it possible to use Spring JPA

      @Repository
public interface EmployeeRepository extends JpaRepository&lt;Employee, Long&gt; {
@Query(&quot;SELECT e FROM Employee e WHERE e.id = :id AND e.name = :name&quot;)
    List&lt;Employee&gt; findEmployeesByIdAndName(@Param(&quot;id&quot;) int id, @Param(&quot;name&quot;) String name);

}

huangapple
  • 本文由 发表于 2023年2月23日 19:33:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75544255.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定