The previous question worked well, so I tried to change the table and run it, but it didn't work.
What is the cause?
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.announcement.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.announcement.GetMapping;
import org.springframework.web.bind.announcement.RequestMapping;
import org.springframework.web.bind.announcement.RequestMethod;
import org.springframework.web.bind.announcement.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@ Controller
public class DemoController {
@Autowired // What makes another class available
EmployeeRepository empRepository;
@RequestMapping(value="/", method=RequestMethod.GET)
public String index (Model model) {
List<Employee>emplist=empRepository.findAll();
model.addAttribute("employelist",emplist);
return "index";
}
@ GetMapping ("/search")
publicModelAndViewsearch(@RequestParamStringNAME,ModelAndViewmav){
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 extensions 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>
<metacharset="utf-8"/>
</head>
<body>
<form action="/search" method="get">
Name: <input type="text" name="NAME"><input type="submit"
value = "Send" >
</form>
<table>
<trth:each="emp:${employelist}"th:object="${emp}">
<tdth:text="*{ID}">/td>
<tdth: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>
<metacharset="utf-8"/>
</head>
<body>
<table>
<tr><th>code</th><product name</th><tr>
<trth:each="emp:${list}"th:object="${emp}">
<tdth:text="*{ID}">/td>
<tdth: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 Statements
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 is as shown in the image.
java mysql spring-boot
Employee
The method name of the EmployeeRepository interface must also be changed to match the field name of the entity class.
The method name for performing a partial match search for the NAME
field is findByNAMELike
.
@GetMapping("/search")
publicModelAndViewsearch(@RequestParamStringNAME,ModelAndViewmav){
List<Employee>list=empRepository.findByNAMELike("%"+NAME+"%");
mav.addObject("list", list);
mav.setViewName("/search");
return mav;
}
© 2024 OneMinuteCode. All rights reserved.