【发布时间】: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