【问题标题】:I am not able to search my users by email using JPA spring boot我无法使用 JPA spring boot 通过电子邮件搜索我的用户
【发布时间】:2022-01-10 15:24:02
【问题描述】:

控制器

package com.bird.bird.Controllers;

import com.bird.bird.Entity.Manager;
import com.bird.bird.Repository.ManagerRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/manager")
public class ManagerController {
    
    @Autowired
    private ManagerRepository managerRepository;


    @GetMapping("/teste")
    public Object teste() {
        return "teste";
    }

    @PostMapping("/save")
    public Manager save(@RequestBody Manager manager){
        return managerRepository.save(manager);
    };
    @GetMapping("/list")
    public Iterable<Manager> findAll(){
        return managerRepository.findAll();
    };

    @GetMapping("/findID/{id}")
    public Manager findById( @PathVariable int id){
        return managerRepository.findById(id);
    };

    @GetMapping("/findCPF/{CPF}")
    public Manager findByCPF( @PathVariable String CPF){
        return managerRepository.findByCPF(CPF);
    };
}

存储库

package com.bird.bird.Repository;

import java.util.List;

import com.bird.bird.Entity.Manager;

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

public interface ManagerRepository extends JpaRepository<Manager, Integer> {

    Manager findById(int id);
    Manager findByEmail(String email);
    Manager findByCPF(String CPF);
    //Manager findByPhone(String phone);

}

实体

package com.bird.bird.Entity;

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

@Entity
@Table(name = "manager")
public class Manager implements java.io.Serializable {
    @Id
    @GeneratedValue
    private int IdManager;
    @Column(name = "name", length = 30, nullable = false)
    private String Name;
    @Column(name = "birth", length = 15, nullable = false)
    private String Birth;
    @Column(name = "email", length = 30, unique = true, nullable = false)
    private String Email;
    @Column(name = "CPF", length = 30, unique = true, nullable = false)
    private String CPF;
    @Column(name = "password", length = 30, nullable = false)
    private String Password;
    @Column(name = "phone", length = 30, nullable = false)
    private String Phone;
    @Column(name = "address", length = 30, nullable = false)
    private String Address;
    @Column(name = "city", length = 30, nullable = false)
    private String City;
    @Column(name = "state", length = 30, nullable = false)
    private String State;
    @Column(name = "zipcode", length = 30, nullable = false)
    private String ZipCode;
    @Column(name = "country", length = 30, nullable = false)
    private String Country;

    public Manager() {
    }
    public Manager(String name, String birth, String email, String CPF, String password, String phone, String address, String city, String state, String zipCode, String country) {
        this.Name = name;
        this.Birth = birth;
        this.Email = email;
        this.CPF = CPF;
        this.Password = password;
        this.Phone = phone;
        this.Address = address;
        this.City = city;
        this.State = state;
        this.ZipCode = zipCode;
        this.Country = country;
    }

    public int getIdManager() {
        return IdManager;
    }

    public void setIdManager(int idManager) {
        IdManager = idManager;
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public String getBirth() {
        return Birth;
    }

    public void setBirth(String birth) {
        Birth = birth;
    }

    public String getEmail() {
        return Email;
    }

    public void setEmail(String email) {
        Email = email;
    }

    public String getCPF() {
        return CPF;
    }

    public void setCPF(String CPF) {
        this.CPF = CPF;
    }

    public String getPassword() {
        return Password;
    }

    public void setPassword(String password) {
        Password = password;
    }

    public String getPhone() {
        return Phone;
    }

    public void setPhone(String phone) {
        Phone = phone;
    }

    public String getAddress() {
        return Address;
    }

    public void setAddress(String address) {
        Address = address;
    }

    public String getCity() {
        return City;
    }

    public void setCity(String city) {
        City = city;
    }

    public String getState() {
        return State;
    }

    public void setState(String state) {
        State = state;
    }

    public String getZipCode() {
        return ZipCode;
    }

    public void setZipCode(String zipCode) {
        ZipCode = zipCode;
    }

    public String getCountry() {
        return Country;
    }

    public void setCountry(String country) {
        Country = country;
    }
}


错误: eption:创建名为“managerController”的 bean 时出错:通过字段“managerRepository”表示的依赖关系不满足;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在 JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration 上声明的 @EnableJpaRepositories 中定义的 com.bird.bird.Repository.ManagerRepository 中创建名称为“managerRepository”的 bean 时出错:调用 init 方法失败

简历: 我想进行电子邮件搜索以进行测试,然后再次登录以比较电子邮件和密码

【问题讨论】:

  • 你能发布你的配置类和控制器类吗。
  • 我还在学习,它没有配置类
  • 我也放了我的控制器

标签: java spring spring-boot hibernate jpa


【解决方案1】:

对于您的经理类,您已经定义了所有以大写字母开头的字段(按照惯例这是错误的),因此您需要用小写字母更新它们,然后我们才能使用 jpa 接口数据派生查询(findByFieldName ) 因此,将 Manager 类中的 Email 字段更新为:

private String email ;    
// standard  getters and setters 

除此之外,

 @GetMapping("/findByEmail/{email}")
public Manager findByEmail(@PathVariable String email){
    return managerRepository.findByEmail(email);
};

您需要更新它以获取映射。从数据库中检索对象时,我们使用 @GetMapping 而不是 Post。另外,我们在 url 中传递变量,而不是在请求正文中。

【讨论】:

  • 我正在切换到你提到的表格
  • 可以在github上分享你的项目吗?
  • 是的,一会儿。
  • 您的解决方案删除了​​错误,但没有为查询带来答案
【解决方案2】:

将 @Repositroy 注释添加到 ManagerRepository 并改变你的班级,比如

public class Manager {
    @Id
    @GeneratedValue
    private int idManager;
    @Column(name = "name", length = 30, nullable = false)
    private String name;
    @Column(name = "birth", length = 15, nullable = false)
    private String birth;
    @Column(name = "email", length = 30, unique = true, nullable = false)
    private String email;
    @Column(name = "CPF", length = 30, unique = true, nullable = false)
    private String cpf;
    @Column(name = "password", length = 30, nullable = false)
    private String password;
    @Column(name = "phone", length = 30, nullable = false)
    private String phone;
    @Column(name = "address", length = 30, nullable = false)
    private String address;
    @Column(name = "city", length = 30, nullable = false)
    private String city;
    @Column(name = "state", length = 30, nullable = false)
    private String state;
    @Column(name = "zipcode", length = 30, nullable = false)
    private String zipCode;
    @Column(name = "country", length = 30, nullable = false)
    private String country;

    public ManagerDao(int idManager, String name, String birth, String email, String cpf, String password, String phone, String address, String city, String state, String zipCode, String country) {
        this.idManager = idManager;
        this.name = name;
        this.birth = birth;
        this.email = email;
        this.cpf = cpf;
        this.password = password;
        this.phone = phone;
        this.address = address;
        this.city = city;
        this.state = state;
        this.zipCode = zipCode;
        this.country = country;
    }

    public int getIdManager() {
        return idManager;
    }

    public void setIdManager(int idManager) {
        this.idManager = idManager;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getBirth() {
        return birth;
    }

    public void setBirth(String birth) {
        this.birth = birth;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getCpf() {
        return cpf;
    }

    public void setCpf(String cpf) {
        this.cpf = cpf;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getZipCode() {
        return zipCode;
    }

    public void setZipCode(String zipCode) {
        this.zipCode = zipCode;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }
}

然后删除“;”不需要“}”之后的控制器中的方法。

Gurkirat Singh Guliani回答正确

【讨论】:

  • 还是同样的错误
  • 我没有配置类,不知道这是什么,还在学习中
  • 错误不在控件中,但是当我用findByEmail保存时,已经有错误,不让我使用
  • 我无法从存储库中的这个 findByEmail 开始
  • 配置类里面有什么?
猜你喜欢
  • 2021-12-02
  • 1970-01-01
  • 2013-05-28
  • 1970-01-01
  • 2021-02-12
  • 2017-10-15
  • 2011-03-17
  • 1970-01-01
  • 2022-11-09
相关资源
最近更新 更多