Spring Data crud 存储库查询总是返回 0

问题描述 投票:0回答:0

我有一个带有实体类的服务和存储库类

package com.example.onlinebookstore.service;

import com.example.onlinebookstore.repository.CustomerRepository;
import com.example.onlinebookstore.model.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class CustomerService implements ICustomerService {
    @Autowired
    CustomerRepository customerRepository;

    @Override
    public String login(Customer customer) {
        List<Customer> cus = customerRepository.findByUsername(customer.getUserName());
        
        if (cus != null || cus.size() > 0) {
            if (cus.get(0).getUserName().equals(customer.getPassword())){
                return "Success";
            }
        }
        return "Failed";
    }
}

package com.example.onlinebookstore.repository;

import com.example.onlinebookstore.model.Customer;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import java.util.List;

@Repository
public interface CustomerRepository extends CrudRepository<Customer, Integer> {

    List<Customer> findByUsername(String username);
}
package com.example.onlinebookstore.model;

import com.fasterxml.jackson.annotation.JsonProperty;
import javax.persistence.*;

@Entity
@Table(name="customer")
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @JsonProperty(value="Id")
    @Column(name = "id")
    int id;

    @JsonProperty(value="Username")
    @Column(name = "username")
    String username;

    @JsonProperty(value="Password")
    @Column(name = "password")
    String password;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUserName() {
        return username;
    }

    public void setUserName(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

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

    @Override
    public String toString() {
        return "Customer{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

This is DB data

This is postman request

我想根据用户名过滤数据库数据,但它总是返回 0(零)。这在添加 findAll() 方法时有效。为什么总是0?

我想根据用户名过滤数据。 CustomerService.java 第 18 行返回 0。DB 是 postgressql。数据库列是客户(ID、用户名、密码)。

spring-boot postman postgresql-9.1 pgadmin crud-repository
© www.soinside.com 2019 - 2024. All rights reserved.