2016-08-04 165 views
0

我試圖用簧數據的MongoDB或彈簧數據的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

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

最後的應用程序(如例如11 here中提及):

@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或蒙戈(但不能同時),並避免重複代碼的應用類

任何幫助將是有益的。

謝謝

回答

0

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

所以,你必須讓你需要的用戶

但是,如果你的使用情況是存儲在蒙戈的所有文件和檢索少數並在數據庫和查詢使用SQL依然存在,數據庫的決定,我想你代碼應該工作。

您可以創建一個通用的接口和擴展倉庫接口,並使用某種策略模式的使用適當的回購

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