连接spring boot和mongodb时出现问题

lawou6xi  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(146)

我想连接mongodb和spring项目。但我面对的是nullpointerexception。如何在接口mongorepository中使用save()?我应该创建具体类并重写它吗?
关于接口mongorepository:https://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/repository/mongorepository.html
我对java,spring,di(依赖注入)了解不多。?

[Test.java]

@SpringBootTest
class PatientsServiceTest {

    private PatientsService patientsService = new PatientsService();

    @Test
    void save() {
        Patients patient = new Patients();

        ~~~ object patient initialize ~~~

        patientsService.save(patient);
    }
}
[PatientsService.java]

@Service
public class PatientsService {

    @Autowired
    public PatientsRepository patientsRepository; // <- Null exists

    public void save(Patients patients){

        ~~~ additional codes ~~

        patientsRepository.save(patients);
    }
}
[PatientsRepository.java]

@Repository
public interface PatientsRepository extends MongoRepository<Patients, String> {
    public Patients findBySSN(String SSN);
}

错误消息

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题