简体   繁体   English

接口(模拟)存储库模式 java spring

[英]interface (mock) repository pattern java spring

I am a beginner in Java and in the Spring environment.我是 Java 和 Spring 环境的初学者。 At the moment I have to implement a CustomerRepository.目前我必须实现一个 CustomerRepository。 The CustomerRepository implements a CrudRepository interface. CustomerRepository 实现了 CrudRepository 接口。 The purpose is that in the repository, customer objects should be stored and retrieved.目的是在存储库中,应该存储和检索客户对象。 I have to use a Mock implementation of the Spring class CrudRepository.我必须使用 Spring class CrudRepository 的 Mock 实现。

The classdiagram looks like this: classdiagram类图看起来像这样:类图

CrudRepository interface: CrudRepository 接口:

public interface CrudRepository<Customer, String> {

<S extends Customer> S save( S entity );
...
}

The complete CustomerRepository class:完整的 CustomerRepository class:

public class CustomerRepository implements CrudRepository<Customer, String> {
 
private CrudRepository crudrepository;

/*long count();*/
@Override
public long count() {
    long count = crudrepository.count();
    return count;
}

/*<S extends Customer> S save( S entity );*/
@Override
public <S extends Customer> S save(S entity) {
    crudrepository.save(entity);
    return entity;
}

/*<S extends Customer> Iterable<S> saveAll( Iterable<S> entities );*/
@Override
public <S extends Customer> Iterable<S> saveAll(Iterable<S> entities) {
    Iterable<S> response = crudrepository.saveAll(entities);
    return (Iterable<S>) response;
}

/*Optional<Customer> findById(String id );*/
@Override
public Optional<Customer> findById(String id) {
    Optional<Customer> customerResponse = crudrepository.findById(id);
    return customerResponse;

}

/*Iterable<Customer> findAllById(Iterable<String> ids );*/
@Override
public Iterable<Customer> findAllById(Iterable<String> ids) {
    Iterable<Customer> customerResponse = crudrepository.findAllById(ids);
    return customerResponse;
}

/*Iterable<Customer> findAll();*/
@Override
public Iterable<Customer> findAll() {
    Iterable<Customer> customerResponse = (Iterable<Customer>) crudrepository
            .findAll();
    return customerResponse;
}

/*boolean existsById(String id );*/
@Override
public boolean existsById(String id) {
    return crudrepository.existsById(id);
}

/*void deleteById(String id );*/
@Override
public void deleteById(String id) {
    crudrepository.deleteById(id);

}

/*void delete(Customer entity );*/
@Override
public void delete(Customer entity) {
    crudrepository.delete(entity);

}

/*void deleteAllById(Iterable<? extends String> ids );*/
@Override
public void deleteAllById(Iterable<? extends String> entities) {
    crudrepository.deleteAll(entities);

}

/*void deleteAll();*/
@Override
public void deleteAll() {
    crudrepository.deleteAll();
}

/*void deleteAll(Iterable<? extends Customer> entities );*/
@Override
public void deleteAll(Iterable<? extends Customer> entities) {
    crudrepository.deleteAll(entities);
    
} }

How does that look for you?你觉得怎么样? Any suggestions?有什么建议么?

I think you are misunderstanding some concepts.我认为你误解了一些概念。

CrudRepository is a Spring object and it's an interface you don't need to implement. CrudRepository是一个 Spring object ,它是一个您不需要实现的接口。 You have to extend it and Spring provides you all the magic.你必须扩展它,Spring 为你提供所有的魔法。

You can achieve your goal simply in the following way:您可以通过以下方式简单地实现您的目标:

Repository存储库

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

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

}

Service服务

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CustomerService {
    
    @Autowired
    private CustomerRepository customerRepository;

    public Customer save(Customer customer) {
        Customer response = customerRepository.save(customer);
        return response;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM