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或蒙戈(但不能同時),並避免重複代碼的應用類
任何幫助將是有益的。
謝謝