最近研究ssm框架整合,发现有的项目在DAO层的借口上有@Repository注解,而有的没有,研究好久也没发现区别,请问一下各位大神,这个在什么情况下必须加@Repository,什么情况下不用加@Repository?如果是跟配置有关系,请说明是那个配置,谢谢啦

解决方案 »

  1.   

    我写的都加上@Repository注解,没加的算默认吗?
      

  2.   

    使用上跟Component注解没啥区别,顶多也就是配置scan的时候有excluse和include时候可以包含或者排除。其实主要就是概念上的区别,存储库的概念,在DDD中尤其重要。
      

  3.   

    你在使用Mybatis的注解的时候。像@Select @Update的时候。这个注解时必须要要的。你觉得可有可无的的原因无非就是你的这个dao bean可能已经在xml中注册过了。
      

  4.   

    加不加注解,取决于你的这个dao层是否要在其他的bean里引用
      

  5.   

    注解是程序的一种说明,而要说明一个接口不一定只用注解,可以是实现接口,比如,Serializable接口就里面什么方法都没有,但实现该接口,就表示启用序列化功能等,也可能是约定好的, 不如:UserDaoImp 在spring中就是对UserDao接口的实现)
      

  6.   

    如果spring配置文件中配置了自动扫描mapper接口的话 就不需要@注解了,如果用的是sqlsessiondaosupport自定义dao的话就需要注解注入
      

  7.   

    谢谢uuuiiiooo099,
    "如果spring配置文件中配置了自动扫描mapper接口的话 就不需要@注解了,如果用的是sqlsessiondaosupport自定义dao的话就需要注解注入" 这个是我想要的答案,
    也谢谢其他大神的关注以及解答。
      

  8.   


    如果使用内置模板方法update insert 保存不上。
      

  9.   

    看你扫描的配置文件
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
            <property name="basePackage" value="com.xxx.dao"/>
            <!--<property name="annotationClass" value="org.springframework.stereotype.Repository" />-->
        </bean>
    如果  mapperScannerConfigurer 里面配置了annotationClass 这个属性,就需要加对应属性的注解,没有的话可以不加
      

  10.   


    然而大多数公司开发根本就没有TDD、DDD概念
      

  11.   

    Indicates that an annotated class is a "Repository", originally defined by Domain-Driven Design (Evans, 2003) as "a mechanism for encapsulating storage, retrieval, and search behavior which emulates a collection of objects". Teams implementing traditional J2EE patterns such as "Data Access Object" may also apply this stereotype to DAO classes, though care should be taken to understand the distinction between Data Access Object and DDD-style repositories before doing so. This annotation is a general-purpose stereotype and individual teams may narrow their semantics and use as appropriate. A class thus annotated is eligible for Spring DataAccessException translation when used in conjunction with a PersistenceExceptionTranslationPostProcessor. The annotated class is also clarified as to its role in the overall application architecture for the purpose of tooling, aspects, etc. As of Spring 2.5, this annotation also serves as a specialization of @Component, allowing for implementation classes to be autodetected through classpath scanning.为什么 @Repository 只能标注在 DAO 类上呢?这是因为该注解的作用不只是将类识别为Bean,同时它还能将所标注的类中抛出的数据访问异常封装为 Spring 的数据访问异常类型。 Spring本身提供了一个丰富的并且是与具体的数据访问技术无关的数据访问异常结构,用于封装不同的持久层框架抛出的异常,使得异常独立于底层的框架。
      

  12.   

    16楼应该是正解,前面那些人回答的都是些什么玩意。
    我看stackoverflow上一个答案,hibernatetemplate的一个优点是exception transalation,而有了@repository就不需要hibernatetemplate。
      

  13.   


    同意,如果在spring-mybatis的配置文件中对每个mapper接口都进行的 扫描注入,或者统一注入的话,可以不用 @repository注解 ;
    如果你不想在配置文件中定义 dao的bean 那么可以直接在 mapper接口上使用@repository 来实现自动注入。 【记得在配置文件开启  自动扫描 即:
    < context: component-scan base-package="自己的mapper所在包的全路径">】
      

  14.   

    ssm三大框架整合dao层我从来没用过注解,在service层中照样能注入,因为你在mybatis映射文件中关联了dao层的接口啊