【发布时间】:2016-12-10 08:55:14
【问题描述】:
我正在尝试使用 spring data mongoDB 或 spring data 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 here 中所述)
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 类中重复代码
任何帮助都会很有用。
谢谢
【问题讨论】:
标签: java spring-data spring-data-jpa spring-data-mongodb