如何使用条件或spring jpa进行搜索
我正在mvc spring中执行搜索功能。我想对Firstname和LastName进行搜索。如果我在搜索框中输入一个字符串并按enter键,如果找不到它,我将在firstName中进行搜索,反之亦然。这意味着我想输入一个字符串并搜索所有字段。这是我的节目内容 雇员:如何使用条件或spring jpa进行搜索,spring,spring-mvc,spring-boot,spring-data-jpa,spring-data,Spring,Spring Mvc,Spring Boot,Spring Data Jpa,Spring Data,我正在mvc spring中执行搜索功能。我想对Firstname和LastName进行搜索。如果我在搜索框中输入一个字符串并按enter键,如果找不到它,我将在firstName中进行搜索,反之亦然。这意味着我想输入一个字符串并搜索所有字段。这是我的节目内容 雇员: @Entity public class Employee implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDEN
@Entity
public class Employee implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String firstName;
private String lastName;
private int age;
private double salary;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
存储库:
@Repository
public interface EmployeeDAO extends JpaRepository<Employee,Integer>{
List<Employee> findEmployeeByFirstNameOrLastName(String searchName);
}
视图:
<div class="row">
<form method="get" action="/search">
<div class="small-3 columns">
<input type="text" id ="txt" name="searchName" >
</div>
<div class="small-5 columns end">
<button id="button-id" type="submit">Search</button>
</div>
</form>
</div>
搜寻
我已经运行了,但它无法同时搜索firstName和LastName。如何使用一个参数同时搜索firstName或lastName您需要提供两个字段:
List<Employee> findEmployeeByFirstNameOrLastName(String firstName, String lastName);
获取名或姓等于“searchName”的员工
最后的代码如下所示:
@Repository
public interface EmployeeDAO extends JpaRepository<Employee,Integer>{
List<Employee> findEmployeeByFirstNameOrLastName(String firstName, String lastName);
}
@RequestMapping(value = "/search")
public ModelAndView searchByFirstName(@RequestParam String searchName) {
ModelAndView view = new ModelAndView("showEmployee");
// Access employeeDAO here, or use employeeService if you using service
List<Employee> employees = employeeDao.findEmployeeByFirstNameOrLastName(searchName, searchName);
view.addObject("employees", employees);
return view;
}
@存储库
公共界面EmployeeDAO扩展了JpaRepository{
列出FindEmployeeByFirstName或lastName(字符串firstName,字符串lastName);
}
@请求映射(value=“/search”)
公共模型和视图searchByFirstName(@RequestParam String searchName){
ModelAndView视图=新的ModelAndView(“showEmployee”);
//在此处访问employeeDAO,或者使用employeeService(如果使用此服务)
List employeeDao.findeemployeebyfirstname或lastname(searchName,searchName);
view.addObject(“雇员”,雇员);
返回视图;
}
对于更复杂的情况,您可能希望查看JPA Criteria API或QueryDsl来创建条件组合,而不是提供两个字段firstName
,lastName
,类似于上面的Spring数据的派生查询很有用,但是简单地指定查询通常更简单、更清晰:
@Repository
public interface EmployeeDAO extends JpaRepository<Employee,Integer>{
@Query("select e from Employee e where e.firstName = :searchName
or e.lastName = :searchName)
List<Employee> findEmployeeByFirstNameOrLastName(
@Param("searchName") String searchName);
}
@存储库
公共界面EmployeeDAO扩展了JpaRepository{
@查询(“从员工e中选择e,其中e.firstName=:searchName
或e.lastName=:searchName)
列出FindEmployeeByFirstName或LastName(
@参数(“搜索名称”)字符串(搜索名称);
}
问题是…?它不能同时搜索firstName或lastName。我希望如果我输入搜索abc如果abc包含firstName spring,则显示结果。如果firstName中不包含,则在lastName中搜索spring。这意味着一个参数搜索两个字段。您不能将两个@RequestParam相同的名称放入控制器您将searchName传递给存储库/服务,而不是dec在controllerIt不能工作。我不能在respository或服务中使用相同的名称。您已经尝试过了???谢谢。我使用相同的参数(“searchName”)在searchByFirstName方法中调用存储库没问题。我已经编辑了答案来告诉你它是如何工作的。Cheel~我已经尝试过了,但是spring给了我一个错误。它不接受两个相同的:“@Query”中的“searchName”。我不明白。但是当我删除“@Query”并传入两个参数列表FindEmployeeByFirstName或lastName(String firstName,String lastName)时;并致电employeeDao.findeEmployeebyfirstname或lastname(searchName,searchName)这对我很有用。但我想谢谢你的帮助。我想这可以通过添加@Param(“searchName”)
来解决。请参阅更新的答案。
@Repository
public interface EmployeeDAO extends JpaRepository<Employee,Integer>{
List<Employee> findEmployeeByFirstNameOrLastName(String firstName, String lastName);
}
@RequestMapping(value = "/search")
public ModelAndView searchByFirstName(@RequestParam String searchName) {
ModelAndView view = new ModelAndView("showEmployee");
// Access employeeDAO here, or use employeeService if you using service
List<Employee> employees = employeeDao.findEmployeeByFirstNameOrLastName(searchName, searchName);
view.addObject("employees", employees);
return view;
}
@Repository
public interface EmployeeDAO extends JpaRepository<Employee,Integer>{
@Query("select e from Employee e where e.firstName = :searchName
or e.lastName = :searchName)
List<Employee> findEmployeeByFirstNameOrLastName(
@Param("searchName") String searchName);
}