2016-08-04 110 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); 
}