0

前の質問で上手くいったので、テーブルを変えて実行してみようとしましたが、上手く行きません。

何が原因でしょうか。

package com.example.demo.mysql;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import lombok.Getter;
import lombok.Setter;

@Entity
@Table(name="user")
@Getter
@Setter
public class Employee {
  @Id
  @Column(name="ID")
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  private String ID;
  @Column(name="PASS")
  private String PASS;
  @Column(name="NAME")
  private String NAME;
  @Column(name="KANA")
  private String KANA;
}
package com.example.demo.mysql;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@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 NAME, ModelAndView mav) {
    List<Employee> list = empRepository.findByempnameLike("%" + NAME + "%");
    mav.addObject("list", list);
    mav.setViewName("/search");
    return mav;
  }
}
package com.example.demo.mysql;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface EmployeeRepository extends JpaRepository<Employee, String> {

  List<Employee> findByempnameLike(String string);


}
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
 xmlns:th="http://www.thymeleaf.org">
<head>
<title>indexhtml</title>
<meta charset="utf-8" />
</head>
<body>
 <form action="/search" method="get">
  名前: <input type="text" name="NAME"> <input type="submit"
   value="送信">
 </form>
 <table>
  <tr th:each="emp : ${employeelist}" th:object="${emp}">
<td th:text="*{ID}"></td>
   <td th:text="*{NAME}"></td>
  </tr>
 </table>
</body>
</html>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:th="http://www.thymeleaf.org">
<head>
<title>searchhtml</title>
<meta charset="utf-8" />
</head>
<body>
  <table>
  <tr><th>コード</th><th>商品名</th></tr>
    <tr th:each="emp : ${list}" th:object="${emp}">
      <td th:text="*{ID}"></td>
      <td th:text="*{NAME}"></td>
    </tr>
  </table>
</body>
</html>
spring.datasource.url=jdbc:mysql://localhost:3306/xxx
spring.datasource.username=root
spring.datasource.password=xxx
spring.jpa.database=MYSQL
spring.jpa.hibernate.ddl-auto=update

エラー文

Error creating bean with name 'demoController': Unsatisfied dependency expressed through field 'empRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeRepository' defined in com.example.demo.mysql.EmployeeRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract java.util.List com.example.demo.mysql.EmployeeRepository.findByempnameLike(java.lang.String)! Reason: Failed to create query for method public abstract java.util.List com.example.demo.mysql.EmployeeRepository.findByempnameLike(java.lang.String)! No property empname found for type Employee!; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.example.demo.mysql.EmployeeRepository.findByempnameLike(java.lang.String)! No property empname found for type Employee!

画像の説明をここに入力

mysqlは画像のとおりです。

1
  • エラーメッセージには No property empname found for type Employee とあります。プロパティ名を変更したらメソッド名も変更する必要があるのではないでしょうか。
    – metropolis
    2021年10月25日 13:30

1 件の回答 1

1

Employee エンティティクラスのフィールド名に合わせて EmployeeRepository インタフェースのメソッド名も変える必要があります。

NAME フィールドに対する部分一致検索を行うためのメソッド名は findByNAMELike になります。

  @GetMapping("/search")
  public ModelAndView search(@RequestParam String NAME, ModelAndView mav) {
    List<Employee> list = empRepository.findByNAMELike("%" + NAME + "%");
    mav.addObject("list", list);
    mav.setViewName("/search");
    return mav;
  }
0

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

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