【问题标题】:How can I use Spring Data MongoDB or Spring Data Jpa如何使用 Spring Data MongoDB 或 Spring Data Jpa
【发布时间】: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


    【解决方案1】:

    JPA 将用于存储在 RDBMS 数据库中。 DATA JPA Mongo 将用于将文档存储到 Mongo。

    所以你必须决定你需要使用哪个数据库

    但是,如果您的用例是将所有文档存储在 mongo 中并检索少量 to 并保留在 db 中并使用 sql 查询,我认为您的代码应该可以工作。

    您可以创建一个通用接口并扩展您的存储库接口并使用某种策略模式来使用适当的存储库

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

    【讨论】:

      猜你喜欢
      • 2014-10-21
      • 2017-07-30
      • 2017-02-22
      • 1970-01-01
      • 1970-01-01
      • 2017-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多