如何在springboot中为两个独立的东西创建一个搜索栏?它应该能够接受名字或姓氏,或者两者都接受,以给予输出

b5lpy0ml  于 2023-06-22  发布在  Spring
关注(0)|答案(1)|浏览(62)

我有一个搜索栏,可以接受任何字符串。它将检查数据库,以查看记录是否在名字或姓氏中与字符串匹配。但它也应该能够将全名作为参数,并返回基于它的记录。
我将fullName作为参数,并将其拆分以创建单独的名字和姓氏,并使用if语句检查是否匹配。但它似乎不起作用,尽管我看不出逻辑上的问题。
控制器类:

@Controller
public class EmployeeController {
private final EmployeeService empService;

@Autowired
public EmployeeController(EmployeeService empService) {
    this.empService = empService;
}

@PostMapping("search-by-full-name")
public String handleSearchByFullName(Model model, @RequestParam("fullName") String fullName) {
    if (fullName.contains(" ")) {
        String[] nameParts = fullName.split(" ");
        String firstName = nameParts[0];
        String lastName = nameParts[1];
        for (Employee emp : empService.findByFullName(fullName)) {
            if (emp.getFirstName().equals(firstName) && emp.getLastName().equals(lastName)) {
                model.addAttribute("emps", empService.findByFirstName(firstName));
                return "showEmployees";
            }
        }
    } else if (!empService.findByFullName(fullName).isEmpty()) {
        model.addAttribute("emps", empService.findByFullName(fullName));
        return "showEmployees";
    }
    return "errorPage";
}
}

服务等级:

@Service
public class EmployeeService {
private final EmployeeRepository empRepo;

@Autowired
public EmployeeService(EmployeeRepository empRepo) {
    this.empRepo = empRepo;
}

public List<Employee> findByFullName(String fullName) {
    return empRepo.findByFirstNameContainingIgnoreCaseOrLastNameContainingIgnoreCase(fullName, fullName);
}

public List<Employee> findByFirstName(String firstName) {
    return empRepo.findByFirstNameContainingIgnoreCase(firstName);
}

public List<Employee> findByLastName(String lastName) {
    return empRepo.findByLastNameContainingIgnoreCase(lastName);
}

}
存储库类:

@Repository
public interface EmployeeRepository extends 
JpaRepository<Employee, Long> {
List<Employee> findByFirstNameContainingIgnoreCaseOrLastNameContainingIgnoreCase(String firstName, String lastName);

List<Employee> findByFirstNameContainingIgnoreCase(String firstName);

List<Employee> findByLastNameContainingIgnoreCase(String lastName);

}

vawmfj5a

vawmfj5a1#

检查空格的名称。如果空间可用,则将第一部分作为名字,其余部分作为姓氏。使用and调用具有2个参数的查询方法,否则按原样使用name参数,使用or调用具有2个参数的方法。
您的代码通过额外的检查和查询过度考虑了所有这些。将逻辑移动到服务而不是控制器中。

public interface EmployeeRepository extends JpaRepository<Employee, Long> {

List<Employee> findByFirstNameContainingIgnoreCaseOrLastNameContainingIgnoreCase(String name, String name);

List<Employee> findByFirstNameContainingIgnoreCaseAndLastNameContainingIgnoreCase(String firstname, String lastname);
@Service
public EmployeeService {

  private final EmployeeRepository employees;

  public EmployeeService(EmployeeRepository employees) {
    this.employees=employees;
  }

  @Transactional(readOnly=true)
  public List<Employee> findEmployees(String name) {
    var idx = name.indexOf(' ');
    if (idx > -1) {
      var firstname = name.substring(0, idx);
      var lastname = name.substring(idx + 1);
      return employees. findByFirstNameContainingIgnoreCaseAndLastNameContainingIgnoreCase(firstname, lastname);      
    }
    return employees. findByFirstNameContainingIgnoreCaseOrLastNameContainingIgnoreCase(name, name);      
  }
}

现在在你的控制器中调用findEmployees方法。

@Controller
public class EmployeeController {

private final EmployeeService empService;

  public EmployeeController(EmployeeService empService) {
    this.empService = empService;
  }

  @PostMapping("search-by-full-name")
  public String handleSearchByFullName(Model model, @RequestParam("fullName") String fullName) {
    
    model.addAttribute("emps", empService.findEmployees(fullName));
    return "showEmployees";
  }
}

就像这样

相关问题