如何在spring数据jpa中搜索搜索框中的多个字段

时间:2018-02-15 05:33:01

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

我正在使用spring数据jpa并正在编写搜索功能。在我的模型中包括firstName和lastName。我希望我的搜索对话框能够同时搜索firstName和lastName。例如,firstName是Alen,lastName是Lee,如果我在搜索框中输入Alen或Lee然后spring仍然找到正确的值。有没有办法帮助我在spring data jpa中实现这个目标?

EmployeeDao

package com.baotrung.springcrudjpafull.dao;

import com.baotrung.springcrudjpafull.model.Employee;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface EmployeeDao extends CrudRepository<Employee,Integer> {
        Employee findAllByFirstNameAndLastName(String firstName,String lastName);
}

EmployeeController

@RequestMapping(value = "/searchEmployee", method = RequestMethod.GET)
    public String getEmployeeByName(@RequestParam("firstName") String firstName, @RequestParam("lastName") String lastName, ModelMap modelMap) {
        Employee employee = servicesDao.findAllByFirstNameAndLastName(firstName,lastName);
        modelMap.addAttribute("employees", employee);
        return "Employee";
    }

但是在jsp中我不知道如何传递参数以便能够调用它。如果使用,只使用一个参数。那么如何在spring中的搜索框中找到多个字段。请帮助

3 个答案:

答案 0 :(得分:0)

试试这个

findAllByFirstNameLikeOrLastNameLike

或者

findAllByFirstNameContainingOrLastNameContaining

第一个将生成like比较的请求,第二个将参数包装到%%

答案 1 :(得分:0)

如果你想使用GET方法,参数应该是@PathVaraible而不是@RequestParam,你必须用/searchEmployee/Alen/Lee调用方法

控制器应该是;

@RequestMapping(value = "/searchEmployee/{firstname}/{lastname}", method = RequestMethod.GET)
    public String getEmployeeByName(@PathVariable("firstName") String firstName, @PathVariable("lastName") String lastName, ModelMap modelMap) {
        List<Employee> employee = servicesDao.findAllByFirstNameAndLastName(firstName,lastName);
        modelMap.addAttribute("employees", employee);
        return "employee";
    }

如果您希望用户可以键入搜索值,您需要在jsp中创建一个html表单并提交您的表单/searchEmployee uri并且控制器应该是

@RequestMapping(value = "/searchEmployee", method = RequestMethod.POST)
    public String getEmployeeByName(@RequestParam("firstName") String firstName, @RequestParam("lastName") String lastName, ModelMap modelMap) {
        List<Employee> employee = servicesDao.findAllByFirstNameAndLastName(firstName,lastName);
        modelMap.addAttribute("employees", employee);
        return "employee";
    }

并且你的控制器方法应该返回没有后缀的jsp文件名。对于前者如果您的jsp文件名为employee.jsp,则方法应为return "employee";

我认为存储库应该是这样的;

@Repository
public interface EmployeeDao extends CrudRepository<Employee,Integer> {
        List<Employee> findAllByFirstNameAndLastName(String firstName,String lastName);
}

但这取决于您的员工数据模型。我的意思是如果firstname和lastname对两者都有唯一约束,那么你也可以只返回Employee对象

答案 2 :(得分:0)

检查一下:

回购中

@Repository
public interface EmployeeDao extends JpaRepository<Employee, Integer> {
    @Query("select e from Employee e where lower(e.firstName) like lower(concat('%', :search, '%')) " +
"or lower(e.lastName) like lower(concat('%', :search, '%'))")
    List<Employee> findByFirstNameOrLastName(@Param("search") String search);
}

在控制器中:

@RequestMapping("/searchEmployee", method = RequestMethod.GET)
    public String getEmployeeByName(@RequestParam(name = "search") String search, Model model) {
        Employee employee = servicesDao.findAllByFirstNameAndLastName(search);
        model.addAttribute("employees", employee);
        return "Employee";
    }

在jsp中应该与在百里香中相似:

<form action="#" th:action="@{search}" method="get" class="input-group">
    <input class="form-control form-control-dark" type="text" placeholder="Search" name="search" aria-label="Search">
    <div class="input-group-append">
        <button class="btn btn-outline-light" value="Search">Search</button>
    </div>
</form>