Estoy creando crud en Spring. Me funciona correctamente listar y agregar. Cuando hice el borrar ahí vino el problema. He tratado de solucinarlo pero no me han funcionado las soluciones de internet. acá está el código.
el pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>nin5</groupId>
<artifactId>ninja5</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ninja5</name>
<description>proyecto rninja5</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
archivo application.properties
server.port=8090
# se indica la base de datos, usuario y password de la bd
spring.datasource.url= jdbc:mysql://localhost:3306/cursobase?useSSL=false&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username= root
spring.datasource.password=
# Indica si debe mostrar el log de las consultas sql ejecutadas
# Bueno a la hora de depurar
spring.jpa.show-sql=true
# Configurar Hibernate
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL5Dialect
Hay 2 controllers
controller
package paqueteninja5.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import paqueteninja5.constant.ViewConstant;
import paqueteninja5.model.ContactClass;
import paqueteninja5.service.ContactService;
@Controller
@RequestMapping("/contacts")
public class ControllerContact {
@Autowired
@Qualifier("contactserviceimpl")
private ContactService contactservice;
@GetMapping("/contactform")
public String redirectcontactform(Model modelo) {
modelo.addAttribute("contactModel", new ContactClass());
return ViewConstant.CONTACTFORM;
}
@GetMapping("/cancel")
public String cancel() {
//return ViewConstant.CONTACTS;
return "redirect:/contacts/showcontacts";
}
@PostMapping("/addcontact")
public String addcontact(@ModelAttribute (name="contactModel") ContactClass contactModel, Model model ) {
//contactmodel.addAttribute("contactModel", "");
if (contactservice.addcontact(contactModel)!=null) {
model.addAttribute("resultado", 1);
}
else {model.addAttribute("resultado", 0);}
//return ViewConstant.CONTACTS;
return "redirect:/contacts/showcontacts";
}
@GetMapping("/showcontacts")
public ModelAndView showcontacts() {
ModelAndView mav=new ModelAndView(ViewConstant.CONTACTS);
mav.addObject("contacts", contactservice.listallcontacts());
return mav;
}
@GetMapping("/removecontacts")
public ModelAndView removecontacts(@RequestParam(name="id", required=true) int id) {
// contactservice.removecontact(id);
return showcontacts();
}
}
package paqueteninja5.controller;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import paqueteninja5.constant.ViewConstant;
import paqueteninja5.model.UserCredential;
@Controller
public class ControllerLogin {
private static final Log LOGGER=LogFactory.getLog(ControllerLogin.class);
@GetMapping("/")
public String redirigir() {
return "redirect:/login";
}
@GetMapping("/login")
public String showLogin(Model model, @RequestParam(name="error", required=false)String errorp,
@RequestParam(name="logout", required=false)String logoutp)
{
LOGGER.info("Metodo showLogin(() " + "parametro errorp --->" + errorp + "parametro logoutp-->" + logoutp);
model.addAttribute("objetologin", new UserCredential());
model.addAttribute("error", errorp);
model.addAttribute("logout", logoutp);
return ViewConstant.LOGIN;
}
@PostMapping("/procesar")
public String procesarLogin(@ModelAttribute(name="usuariocredencial") UserCredential usercredential, Model model) {
LOGGER.info("METODO--> PROCESARLOGIN()"+ "PARAMETRO USERCREDENTIAL" + usercredential.toString());
if(usercredential.getUsername().equals("mauricio") && usercredential.getPassword().equals("lara")) {
model.addAttribute("usuario", usercredential.getUsername());
LOGGER.info("returning vista contacts");
//return ViewConstant.CONTACTS;
return "redirect:/contacts/showcontacts";
}
LOGGER.info("redirecting to /login con parametro error");
return "redirect:/login?error";
}
}
service
package paqueteninja5.service;
import java.util.List;
import paqueteninja5.entity.Contact;
import paqueteninja5.model.ContactClass;
public interface ContactService {
public abstract ContactClass addcontact(ContactClass contactmodel);
public abstract List<ContactClass> listallcontacts();
public abstract Contact findcontactbyid(int id);
public abstract void removecontact(int id);
}
clase que implementa service
package paqueteninja5.service.serviceimpl;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import paqueteninja5.component.ComponentConverter;
import paqueteninja5.entity.Contact;
import paqueteninja5.model.ContactClass;
import paqueteninja5.repository.ContactRepository;
import paqueteninja5.service.ContactService;
@Service("contactserviceimpl")
public class ContactServiceImpl implements ContactService{
@Autowired
@Qualifier("contactrepository")
private ContactRepository contactrepository;
@Autowired
@Qualifier("contactconverter")
private ComponentConverter contactconverter;
@Override
public ContactClass addcontact(ContactClass contactmodel) {
Contact contact = contactrepository.save(contactconverter.modeltoentity(contactmodel));
return contactconverter.entitytomodel(contact);
}
@Override
public List<ContactClass> listallcontacts() {
List<Contact> contacts=contactrepository.findAll();
List<ContactClass> contactsmodel=new ArrayList<ContactClass>();
contactrepository.findAll();
for(Contact contact: contacts) {
contactsmodel.add(contactconverter.entitytomodel(contact));
}
return contactsmodel;
}
@Override
public Contact findcontactbyid(int id) {
return contactrepository.findbyid(id);
}
@Override
public void removecontact(int id) {
Contact contact = findcontactbyid(id);
if(contact!=null) {
contactrepository.delete(contact);
}
}
}
repository
package paqueteninja5.repository;
import java.io.Serializable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import paqueteninja5.entity.Contact;
@Repository("contactrepository")
public interface ContactRepository extends JpaRepository<Contact, Serializable>{
public abstract Contact findbyid(int id);
//public abstract Contact findbyid(int id);
}
modelo 2 clases
package paqueteninja5.model;
public class ContactClass {
private int id;
private String firstname;
private String lastname;
private String telephone;
private String city;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public ContactClass(int id, String firstname, String lastname, String telephone, String city) {
super();
this.id = id;
this.firstname = firstname;
this.lastname = lastname;
this.telephone = telephone;
this.city = city;
}
public ContactClass() {
}
}
el error
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'contactserviceimpl': Unsatisfied dependency expressed through field 'contactrepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contactrepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract paqueteninja5.entity.Contact paqueteninja5.repository.ContactRepository.findbyid(int)! No property findbyid found for type Contact!