【发布时间】:2019-05-16 18:56:37
【问题描述】:
我是 Java 和 Spring Boot 的新手。 我创建了一个简单的 spring 应用程序,它使用 JPArepository 从数据库中获取学生详细信息。 以下是 studentDetails 实体:
package com.example.webcustomertracker.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "StudentDetails")
public class StudentDetails {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer StudentID;
private String Name;
private String Surname;
private String City;
public StudentDetails() {}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getSurname() {
return Surname;
}
public void setSurname(String surname) {
Surname = surname;
}
public String getCity() {
return City;
}
public void setCity(String city) {
City = city;
}
public StudentDetails(String name, String surname, String city) {
Name = name;
Surname = surname;
City = city;
}
@Override
public String toString() {
return "StudentDetails [Name=" + Name + ", Surname=" + Surname + ", City=" + City + "]";
}
}
以下是 JPARepo:
package com.example.webcustomertracker.data;
import org.springframework.data.jpa.repository.JpaRepository;
import com.example.webcustomertracker.entity.StudentDetails;
public interface StudentDetailsRepository extends JpaRepository<StudentDetails, Integer>
{
}
以下是服务类:
package com.example.webcustomertracker.data;
import java.util.Optional;
import com.example.webcustomertracker.entity.StudentDetails;
public interface StudentDetailsService {
public abstract Optional<StudentDetails> getStudentDetails(int StudentId);
}
以下是服务类实现
package com.example.webcustomertracker.data;
import java.util.Optional;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.example.webcustomertracker.entity.StudentDetails;
@Component
public class StudentDetailsDataAccess implements StudentDetailsService {
private StudentDetailsRepository studentDetailsRepository;
public StudentDetailsDataAccess(StudentDetailsRepository theStudentDetailsRepository) {
this.studentDetailsRepository = theStudentDetailsRepository;
}
@Transactional
public Optional<StudentDetails> getStudentDetails(int StudentId) {
// TODO Auto-generated method stub
Optional<StudentDetails> objStud = this.studentDetailsRepository.findById(StudentId);
return objStud;
}
}
以下是启动 spring 框架的主类。我只是想调用服务的功能之一,但服务实例为空并且无法执行。
package com.example.webcustomertracker;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.example.webcustomertracker.data.StudentDetailsDataAccess;
import com.example.webcustomertracker.data.StudentDetailsService;
import com.example.webcustomertracker.entity.StudentDetails;
@SpringBootApplication
public class WebCustomerTrackerApplication {
@Autowired
private StudentDetailsService studentDetailsService;
public Optional<StudentDetails> getTheStudentDetails(int id) {
return studentDetailsService.getStudentDetails(id);
}
public static void main(String[] args) {
SpringApplication.run(WebCustomerTrackerApplication.class, args);
Optional<StudentDetails> objStudent = new WebCustomerTrackerApplication().getTheStudentDetails(11);
}
}
以下是我运行代码后遇到的错误:
Exception in thread "main" java.lang.NullPointerException
at com.example.webcustomertracker.WebCustomerTrackerApplication.getTheStudentDetails(WebCustomerTrackerApplication.java:20)
at com.example.webcustomertracker.WebCustomerTrackerApplication.main(WebCustomerTrackerApplication.java:26)
【问题讨论】:
-
你需要自动装配 StudentDetailsService
-
做了............还是同样的问题。
标签: hibernate jpa spring-data-jpa