【发布时间】:2021-11-30 22:35:01
【问题描述】:
我有一个客户类,我想做的是从客户实体中单独调用客户名字和姓氏字段,以填充投资表格中的客户名称下拉字段。我使用接口添加了一个投影,但响应值为 null。
这是客户类
public class Customer implements Serializable {
/**
*
*/
private static final long serialVersionUID = 8348682056500740593L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String firstName ;
private String lastName;
这是投影类
public interface CustomerInter {
String getFirstName();
String getLastName();
}
这是 repository 类
@Repository
public interface CustomerAccountRepo extends JpaRepository <Customer, Long >
{
Optional<Customer> findById(Long id);
// Optional<Customer> findByFirstNameAndLastName(String firstName , String lastName);
@Query(value="select c.firstName, c.lastName from Customer c")
List<CustomerInter> findByFirstNameAndLastName();
}
这是服务类
@Service
public class CustomerAccountService {
@Autowired
private CustomerAccountRepo custRepo;
public List<CustomerInter> getFirstNameAndLastNameOnly() throws Exception {
// LOGGER.info("Inside getAllCustomerFirst&LastName");
List<CustomerInter> customerName = custRepo.findByFirstNameAndLastName();
return customerName;
}
}
这是控制器类
@CrossOrigin(origins = {"http://localhost:3000"})
@RestController
public class CustomerController {
@Autowired
CustomerAccountService customerRepo;
@GetMapping(value="/customerFirstAndLastName")
public List<CustomerInter> getFirstNameAndLastNameOnly() throws Exception
{
return customerRepo.getFirstNameAndLastNameOnly();
}
【问题讨论】:
标签: java mysql reactjs spring-boot jpa