Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 存储库模式的好处和Spring实现_Java_Spring_Design Patterns_Repository_Dao - Fatal编程技术网

Java 存储库模式的好处和Spring实现

Java 存储库模式的好处和Spring实现,java,spring,design-patterns,repository,dao,Java,Spring,Design Patterns,Repository,Dao,本文对DAO和存储库模式之间的差异进行了很好的解释 我简短的复述-DAO使我们使用多种方法扩展接口,这阻碍了更改和测试。反过来,存储库使用query方法封装所有定制/更改,该方法接受规范作为参数。当您需要存储库中的新行为时—您不应该更改它,而应该创建一个新的规范继承人 我的consusion-repository模式比DAO更好,因为它的接口不易修改 到目前为止我说的对吗?我是否错过了存储库模式的一些好处 但是,如果你看一下,你会发现下一个代码: public interface Custome

本文对DAO和存储库模式之间的差异进行了很好的解释

我简短的复述-DAO使我们使用多种方法扩展接口,这阻碍了更改和测试。反过来,存储库使用
query
方法封装所有定制/更改,该方法接受
规范
作为参数。当您需要存储库中的新行为时—您不应该更改它,而应该创建一个新的
规范继承人

我的consusion-repository模式比DAO更好,因为它的接口不易修改

到目前为止我说的对吗?我是否错过了存储库模式的一些好处

但是,如果你看一下,你会发现下一个代码:

public interface CustomerRepository extends CrudRepository<Customer, Long> {
    List<Customer> findByLastName(String lastName); //each time you need custom behavior you need to add a method
    //...
}
public接口CustomerRepository扩展了CrudRepository{
List findByLastName(String lastName);//每次需要自定义行为时,都需要添加一个方法
//...
}
这与前一篇文章不冲突吗?为什么Spring的存储库迫使我们向接口添加新方法

我的consusion-repository模式比DAO更好,因为它的 接口不允许修改

这取决于……
因为存储库模式更复杂,因为它需要编写更多的代码,并且对于存储库的客户端和它的实现来说,它比DAO模式具有更高的抽象级别。
当您需要在查询中具有灵活性和/或查询在结果中混合了多个实体时,存储库模式可以满足这些需求。 如果您需要在表上执行简单的crud操作(基本的创建、读取、更新和删除操作),我认为使用真实的存储库(以及规范)可能会带来开销

但是,如果你看看Spring的访问数据JPA指南,你会发现 下一个代码:

public interface CustomerRepository extends CrudRepository<Customer, Long> {
    List<Customer> findByLastName(String lastName); //each time you need custom behavior you need to add a method
    //...
}
JpaSpecificationExecutor
提供了使用规范的方法,从而促进了查询源代码中重复处理的减少:

/*
 * Copyright 2008-2011 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.data.jpa.repository;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;

/**
 * Interface to allow execution of {@link Specification}s based on the JPA criteria API.
 * 
 * @author Oliver Gierke
 */
public interface JpaSpecificationExecutor<T> {

    /**
     * Returns a single entity matching the given {@link Specification}.
     * 
     * @param spec
     * @return
     */
    T findOne(Specification<T> spec);

    /**
     * Returns all entities matching the given {@link Specification}.
     * 
     * @param spec
     * @return
     */
    List<T> findAll(Specification<T> spec);

    /**
     * Returns a {@link Page} of entities matching the given {@link Specification}.
     * 
     * @param spec
     * @param pageable
     * @return
     */
    Page<T> findAll(Specification<T> spec, Pageable pageable);

    /**
     * Returns all entities matching the given {@link Specification} and {@link Sort}.
     * 
     * @param spec
     * @param sort
     * @return
     */
    List<T> findAll(Specification<T> spec, Sort sort);

    /**
     * Returns the number of instances that the given {@link Specification} will return.
     * 
     * @param spec the {@link Specification} to count instances for
     * @return the number of instances
     */
    long count(Specification<T> spec);
}
这样,您就可以拥有一个真正的存储库,只使用规范方法。
我不知道它是否有效。我从没试过

编辑:回复评论

存储库模式是Hibernate没有提供现成解决方案的概念。
但是Hibernate和更一般的JPA2规范确实提供了标准作为创建类规范的基本要素
然后,您可以创建一个自定义类,该类通过使用规范作为输入来建议所需的方法来实现存储库模式。 我认为您可以使用ORM和存储库,因为您可以通过使用criteriaapi将ORM与规范一起使用。 有些人反对ORM和存储库,我不同意。ORM不是基于DAO模式的。DAO是操作数据库中数据的方法。ORM是构造数据对象以表示数据库结构的方法。 例如,通过使用这两种方法,您可以从规范的灵活性、关系对象映射和实体之间的交织能力中获益

如果您不使用ORM或类似ORM的工具作为IBatis,您应该自己编写ORM提供给您的代码:对象关系映射。

感谢您的充分披露!还有一个可能是离题的问题——Hibernate是否有现成的存储库?使用ORM和存储库可以吗?它有
标准
机制来简化它们的实施。但另一方面,ORM是基于DAO模式的,使用ORM和存储库模式对我来说似乎很奇怪。@Volodymyr Bakhmatiuk欢迎您。我的评论太长了。我将编辑我的答案:)
/*
 * Copyright 2008-2011 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.data.jpa.repository;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;

/**
 * Interface to allow execution of {@link Specification}s based on the JPA criteria API.
 * 
 * @author Oliver Gierke
 */
public interface JpaSpecificationExecutor<T> {

    /**
     * Returns a single entity matching the given {@link Specification}.
     * 
     * @param spec
     * @return
     */
    T findOne(Specification<T> spec);

    /**
     * Returns all entities matching the given {@link Specification}.
     * 
     * @param spec
     * @return
     */
    List<T> findAll(Specification<T> spec);

    /**
     * Returns a {@link Page} of entities matching the given {@link Specification}.
     * 
     * @param spec
     * @param pageable
     * @return
     */
    Page<T> findAll(Specification<T> spec, Pageable pageable);

    /**
     * Returns all entities matching the given {@link Specification} and {@link Sort}.
     * 
     * @param spec
     * @param sort
     * @return
     */
    List<T> findAll(Specification<T> spec, Sort sort);

    /**
     * Returns the number of instances that the given {@link Specification} will return.
     * 
     * @param spec the {@link Specification} to count instances for
     * @return the number of instances
     */
    long count(Specification<T> spec);
}
public interface CustomerRepository extends Repository<Customer, Long>, JpaSpecificationExecutor {
 …
}