【问题标题】:How to search using condition OR spring jpa如何使用条件或 spring jpa 进行搜索
【发布时间】:2018-11-19 07:25:38
【问题描述】:

我正在 mvc spring 中执行搜索功能。我想同时搜索名字和姓氏。如果我在搜索框中输入一个字符串并按回车键,我将在 firstName 中进行搜索,如果在 lastName 中找不到它执行搜索,反之亦然。这意味着我想输入一个字符串并搜索所有字段。这是我的节目内容。

员工:

@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);

}

控制器:

@RequestMapping(value = "/search")
    public ModelAndView searchByFirstName(@RequestParam String searchName) {
        ModelAndView view = new ModelAndView("showEmployee");
        return view;
    }

查看:

 <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

【问题讨论】:

  • 问题是……?
  • 它不能同时搜索名字或姓氏。我想如果我输入 seach 输入 abc 如果 abc 包含名字 spring 显示结果。如果不包含在 lastName 中的 firstName Spring 搜索。这意味着一个参数搜索两个字段。

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


【解决方案1】:

您需要提供两个字段:

List<Employee> findEmployeeByFirstNameOrLastName(String firstName, String lastName);

然后调用它:

findEmployeeByFirstNameOrLastName("searchName", "searchName");

获取Employee,其firstNamelastName等于"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;
}

对于更复杂的情况,您可能希望查看 JPA Criteria API 或 QueryDsl 来创建条件组合,而不是像上面那样提供两个字段 firstNamelastName

【讨论】:

  • 你不能把两个@RequestParam 同名的控制器放到控制器中
  • 您将 searchName 传递给存储库/服务,而不是在控制器中声明它们
  • 它不能工作。我不能将相同的名称放入存储库或服务中。你试过了吗???
  • 谢谢。我使用相同的参数(“searchName”)在 searchByFirstName 方法中调用存储库,它可以工作
  • 哦,好的。我已经编辑了答案以向您展示它是如何工作的。加油~
【解决方案2】:

Spring Data 的派生查询很有用,但简单地指定查询通常更简单明了:

@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);

}

【讨论】:

  • 我试过了,但是春天给我报错了。它不接受两个相同的 '@Query' 中的 ':searchName'。我不明白。但是当我删除'@query'并传入两个参数 List findEmployeeByFirstNameOrLastName(String firstName,String lastName);并调用 employeeDao.findEmployeeByFirstNameOrLastName(searchName, searchName) 它对我有用。但我想谢谢你帮助我。
  • 我认为可以通过添加@Param("searchName") 来解决这个问题。查看更新的答案。
猜你喜欢
  • 1970-01-01
  • 2018-04-08
  • 2023-01-02
  • 1970-01-01
  • 1970-01-01
  • 2016-05-06
  • 2018-08-26
  • 1970-01-01
  • 2014-11-13
相关资源
最近更新 更多