Advertisement

利用@MapperScan研究Spring自定义注解扫描器

阅读量:

在整合Spring Boot与MyBatis的过程中,需在启动类中添加@MapperScan注解,并指定mapper接口所在的包路径。随后即可从Spring IOC容器中获取对应的mapper Bean,从而执行持久化操作。然而,@MapperScan注解究竟是如何实现对mapper接口的实例化,并将其注入至Spring IOC容器中的呢?

首先,创建一个Spring Boot项目,并引入MyBatis及MySQL相关的Maven依赖。在application.properties文件中完成数据库连接参数的配置。这些基础操作在此处不再赘述。接下来,新建一个mapper接口,在本例中将其命名为dao,其命名方式仅为示意之用,实际含义已明确。

复制代码
    package com.p6spy.demop6spy.dao;
    
    import org.apache.ibatis.annotations.Select;
    public interface TestDao {
    @Select("select count(*) from subject_sae_detail")
    String getInfo();
    }
    
    
      
      
      
      
      
      

全部评论 (0)

还没有任何评论哟~