0

部分一致検索ができるwebアプリケーションを作成中です。
index.htmlは正常に動くのですが、検索ボタンを押しsearch.htmlに遷移しますと何も表示されません。

解決方法、分かる方いましたら教えていただきたいです。

index.htmlは全件表示。
search.htmlでは部分一致検索の結果が表示されるしようにしたいです。

@Controller
public class DemoController {
  @Autowired //別のクラスを使えるようにしてくれるもの
  EmployeeRepository empRepository;

  @RequestMapping(value = "/", method = RequestMethod.GET)
  public String index(Model model) {
    List<Employee> emplist = empRepository.findAll();
    model.addAttribute("employeelist", emplist);
    return "index";
  }

  @GetMapping("/search")
  public ModelAndView search(@RequestParam String empname, ModelAndView mav) {
    List<Employee> emplist = empRepository.findByempnameLike("%empname%");
    mav.addObject("employeelist", emplist);
    mav.setViewName("/search");
    return mav;
  }
@Entity
@Table(name="employee_tbl")
@Getter
public class Employee {
  @Id
  @Column(name="empno")
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  private Long empno;

  @Column(name="empname")
  private String empname;
}
@Repository //JpaRepository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {

  List<Employee> findByempnameLike(String string);

}
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
 xmlns:th="http://www.thymeleaf.org">
<head>
<title>index.html</title>
<meta charset="utf-8" />
</head>
<body>
 <form action="/search" method="get">
  名前:
  <input type="text" name="empname">
  <input type="submit" value="送信">
 </form>
 <table>
  <tr th:each="emp : ${employeelist}" th:object="${emp}">
   <td th:text="*{empno}"></td>
   <td th:text="*{empname}"></td>
  </tr>
 </table>
</body>
</html>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:th="http://www.thymeleaf.org">
<head>
<title>search.html</title>
<meta charset="utf-8" />
</head>
<body>
  <table>
    <tr th:each="emp : ${employeelist}" th:object="${emp}">
      <td th:text="*{empno}"></td>
      <td th:text="*{empname}"></td>
    </tr>
  </table>
</body>
</html>
2
  • empRepository.findByempnameLike("%empname%"); としていますが、それですと empname という文字列を含む、という意味になってしまいます。そこは empRepository.findByempnameLike("%"+empname+"%"); とするのではないでしょうか。
    – metropolis
    2021年10月22日 5:27
  • 1
    解決した場合には回答に対してや役に立ったコメントに対して コメントで 返信してください。(質問文は質問の体を保ってください)
    – cubick
    2021年10月25日 9:56

1 件の回答 1

1

質問文中の書き方 "%empname%" だと、文字列 empname を含むものをクエリしていることになるので、想定通り入力文字列を利用するには次のように書く必要があります。

    List<Employee> emplist = empRepository.findByempnameLike("%" + empname + "%");

補足1:

上記のままでも動作するようですが、命名の慣例としては findByEmpnameLike とするのが良いかと思います(By の直後の変数名の頭を大文字に)。

補足2:

一般的には、 LIKE に指定する文字列は % をエスケープする必要があるかと思います。
ですので、実際には次のようになるべきではないかと思われます:

  @GetMapping("/search")
  public ModelAndView search(@RequestParam String empname, ModelAndView mav) {
    empname = EscapeCharacter.DEFAULT.escape(empname);
    List<Employee> emplist = empRepository.findByEmpnameLike("%" + empname + "%");
    // ...
  }

補足3:

Like の代わりに (今回のような部分一致の場合)Containing を利用できます。
%について気にせずに済むので Like より楽です:

  public ModelAndView search(@RequestParam String empname, ModelAndView mav) {
    List<Employee> emplist = empRepository.findByEmpnameContaining(empname);
    // ...
  }

参考:

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。