实现jparepository的服务类在Spring Boot应用程序中始终返回null

时间:2019-04-15 10:47:14

标签: hibernate jpa spring-data-jpa

我是Java和Spring Boot的新手。 我创建了一个简单的spring应用程序,该应用程序使用JPArepository从数据库中获取学生的详细信息。 以下是studentDetais实体:

=SUM(IF(R4>D4;(D4*$D$2)*1000;(R4*$D$2)*1000)+IF(S4>E4;(E4*$E$2)*1000;(S4*$E$2)*1000)+IF(T4>F4;(F4*$F$2)*1000;(T4*$F$2)*1000)+IF(U4>G4;(G4*$G$2)*1000;(U4*$G$2)*1000)+IF(V4>H4;(H4*$H$2)*1000;(V4*$H$2)*1000)+IF(W4>I4;(I4*$I$2)*1000;(W4*$I$2)*1000)+IF(X4>J4;(J4*$J$2)*1000;(X4*$J$2)*1000)+IF(Y4>K4;(K4*$K$2)*1000;(Y4*$K$2)*1000)+IF(Z4>L4;(L4*$L$2)*1000;(Z4*$L$2)*1000))

以下是JPARepo:

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 + "]";
	}
	
	

}

以下是服务类别:

  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);
}

以下是引导spring框架的主要类。我只是试图调用服务的功能之一,但是服务实例即将变为null,并且无法执行。

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;
	}
	
	
	
}

以下是运行代码后出现的错误:

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);

	}

	
}

2 个答案:

答案 0 :(得分:1)

在控制器层自动接线。

假设您有一个名为IndexController的控制器 在那里自动接线。

例如

    StudentDetailsService studentService;
    @Autowired
    public IndexController(StudentDetailsService studentService){
Optional<StudentDetails> objStudent = new studentService.getTheStudentDetails(11);
}

答案 1 :(得分:0)

另一种解决方法是

    @SpringBootApplication
public class WebCustomerTrackerApplication {

    @Autowired
    private StudentDetailsService studentDetailsService;

    public Optional<StudentDetails> getTheStudentDetails(int id) {
        return studentDetailsService.getStudentDetails(id);
    }

    public static void main(String[] args) throws InterruptedException {
        SpringApplication.run(WebCustomerTrackerApplication.class, args); 
        Thread.sleep(1500);
        Optional<StudentDetails> objStudent = new WebCustomerTrackerApplication().getTheStudentDetails(11);

    }

}

在这里等待,直到应用程序启动(所有组件都已加载)。 到目前为止,自创建服务对象以来,您将不会获得空指针异常。