Java添加自定义注解

今天在整合MyBatis过程中,我使用扫描的方式去扫描DAO目录下的Java文件,但是在Service层使用Autowired的时候报错,但是工程能正常运行,此处有Bug!

解决方案:

  1.创建 annotation包

  2.定义一个注解

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Component
public @interface MybatisMapper {
    String value() default "";
}

  3.在spring-mybatis.xml中配置MapperScannerConfigurer

  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.cyou.nad.goldlocker.dao.mybatis" />
        <property name="annotationClass" value="com.cyou.nad.goldlocker.annotation.MybatisMapper"/>
  </bean>

  4.在DAO层Mapper文件上添加注解

@MybatisMapper
public interface TopicMapper {}

  5.Service层正常使用

@Autowired
private TopicMapper topicMapper;

 

posted @ 2016-11-23 20:29  0xcafedaddy  阅读(671)  评论(0编辑  收藏  举报