簡體   English   中英

如何使用Spring Data MongoDB或Spring Data Jpa

[英]How can I use Spring Data MongoDB or Spring Data Jpa

我正在嘗試使用spring數據mongoDB或spring數據jpa而不重復太多代碼:

我有一個客戶模型:

@Document
@Entity
public class Customer {

    @Id
    @GeneratedValue
    private BigInteger id;

    private String firstName;
    private String lastName;

    public Customer() {}

    public Customer(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return String.format(
                "Customer[id=%d, firstName='%s', lastName='%s']",
                id, firstName, lastName);
    }

}

然后,我有兩個包:repository.jpa和repository.mongo(如實施例11中提到在這里

public interface CustomerJpaRepository extends CrudRepository<Customer, BigInteger> {
    public Customer findByFirstName(String firstName);
    public List<Customer> findByLastName(String lastName);
}

public interface CustomerMongoRepository extends MongoRepository<Customer, BigInteger> {
    public Customer findByFirstName(String firstName);
    public List<Customer> findByLastName(String lastName);
}

最后是應用程序:

@Configuration
@EnableAutoConfiguration
@EnableJpaRepositories(basePackages = "hello.repository.jpa")
@EnableMongoRepositories(basePackages = "hello.repository.mongo")
@EnableTransactionManagement
public class Application implements CommandLineRunner {

    @Autowired
    private CustomerMongoRepository repositoryMongo;
    @Autowired
    private CustomerJpaRepository repositoryJpa;


    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void run(String... args) throws Exception {

        System.out.println("-------------------------------");
        System.out.println("MongoDB");
        repositoryMongo.deleteAll();

        // save a couple of customers
        repositoryMongo.save(new Customer("Alice", "Smith"));
        repositoryMongo.save(new Customer("Bob", "Smith"));

        // fetch all customers
        System.out.println("Customers found with findAll():");
        System.out.println("-------------------------------");
        for (Customer customer : repositoryMongo.findAll()) {
            System.out.println(customer);
        }
        System.out.println();

        // fetch an individual customer
        System.out.println("Customer found with findByFirstName('Alice'):");
        System.out.println("--------------------------------");
        System.out.println(repositoryMongo.findByFirstName("Alice"));

        System.out.println("Customers found with findByLastName('Smith'):");
        System.out.println("--------------------------------");
        for (Customer customer : repositoryMongo.findByLastName("Smith")) {
            System.out.println(customer);
        }

        System.out.println("-------------------------------");
        System.out.println("JPA");
        repositoryJpa.deleteAll();

        // save a couple of customers
        repositoryJpa.save(new Customer("Ludo2", "Smith"));
        repositoryJpa.save(new Customer("John2", "Smith"));

        // fetch all customers
        System.out.println("Customers found with findAll():");
        System.out.println("-------------------------------");
        for (Customer customer : repositoryJpa.findAll()) {
            System.out.println(customer);
        }
        System.out.println();

        // fetch an individual customer
        System.out.println("Customer found with findByFirstName('Ludo2'):");
        System.out.println("--------------------------------");
        System.out.println(repositoryJpa.findByFirstName("Ludo2"));

        System.out.println("Customers found with findByLastName('Smith'):");
        System.out.println("--------------------------------");
        for (Customer customer : repositoryJpa.findByLastName("Smith")) {
            System.out.println(customer);
        }
    }
}

我想做的是:

選擇使用Jpa或Mongo(但不能同時使用兩者),並避免在Application類中重復代碼

任何幫助將是有用的。

謝謝

JPA將用於存儲在RDBMS數據庫中。 DATA JPA Mongo將用於將文檔存儲到Mongo中。

因此,您必須決定需要使用哪個數據庫

但是,如果您的用例是將所有文檔都存儲在mongo中,而只檢索其中的幾個文檔並保留在db中,並使用sql查詢,那么我認為您的代碼應該可以工作。

您可以創建一個通用接口並擴展您的存儲庫接口,並使用某種策略模式來使用適當的存儲庫

public interface CommonRepo{
    public Customer findByFirstName(String firstName);
    public List<Customer> findByLastName(String lastName);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM