美文网首页Spring boot
SpringBoot 的MapperScan和 MapperSc

SpringBoot 的MapperScan和 MapperSc

作者: 三也视界 | 来源:发表于2020-11-13 15:11 被阅读0次

    @Mapper 是 Mybatis 的注解,和 Spring 没有关系,@Repository 是 Spring 的注解,用于声明一个 Bean。(重要)

    使用 Mybatis 有 XML 文件或者注解的两种使用方式,如果是使用 XML 文件的方式,我们需要在配置文件中指定 XML 的位置。
    这里先使用xml的方式示例。

    1、@Mapper注解:
    作用:在接口类上添加了@Mapper,在编译之后会生成相应的接口实现类
    添加位置:接口类上面

    @Mapper
    public interface UserMapper{
       /**
         * @param username 用户名
         * @param password 密码
         * @param memberLevelId 会员级别
         * @param phone 手机号码
         */
        void addUser(String username,String password,int memberLevelId,String phone,String createTime);
    }
    

    如果想要每个接口都要变成实现类,那么需要在每个接口类上加上@Mapper注解,比较麻烦,解决这个问题用@MapperScan

    2、@MapperScan
    作用:指定要变成实现类的接口所在的包,然后包下面的所有接口在编译之后都会生成相应的实现类
    添加位置:是在Springboot启动类上面添加,

    @SpringBootApplication
    @MapperScan("com.winter.dao")
    public class SpringbootMybatisDemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringbootMybatisDemoApplication.class, args);
        }
    }
    

    添加@MapperScan(“com.winter.dao”)注解以后,com.winter.dao包下面的接口类,在编译之后都会生成相应的实现类

    3、使用@MapperScan注解多个包
    (实际用的时候根据自己的包路径进行修改)

    @SpringBootApplication  
    @MapperScan({"com.kfit.demo","com.kfit.user"})  
    public class App {  
        public static void main(String[] args) {  
           SpringApplication.run(App.class, args);  
        }  
    } 
    

    4、 如果dao接口类没有在Spring Boot主程序可以扫描的包或者子包下面,可以使用如下方式进行配置:
    (没验证过,不确定能否使用,或许需要根据自己定义的包名进行修改路径)

    @SpringBootApplication  
    @MapperScan({"com.philos.*.mapper","org.philos.*.mapper"})  
    public class App {  
        public static void main(String[] args) {  
           SpringApplication.run(App.class, args);  
        }  
    } 
    

    早点的时间是直接在Mapper类上面添加注解@Mapper,这种方式要求每一个mapper类都需要添加此注解,比较麻烦。

    现在通过使用@MapperScan可以指定要扫描的Mapper类的包的路径,比如:

    @SpringBootApplication
    @MapperScan("com.philos.demo.mapper")
    // 添加对mapper包扫描
    public class Application {
        public static void main(String[] args) {
          SpringApplication.run(Application.class, args);
        }
    }
    

    同时,使用@MapperScan注解多个包

    @SpringBootApplication  
    @MapperScan({"com.philos.demo","com.philos.user"})  
    public class App {  
        public static void main(String[] args) {  
           SpringApplication.run(App.class, args);  
        }  
    } 
    

    如果如果mapper类没有在Spring Boot主程序可以扫描的包或者子包下面,可以使用如下方式进行配置

    @SpringBootApplication  
    @MapperScan({"com.philos.*.mapper","org.philos.*.mapper"})  
    public class App {  
        public static void main(String[] args) {  
           SpringApplication.run(App.class, args);  
        }  
    } 
    

    相关文章

      网友评论

        本文标题:SpringBoot 的MapperScan和 MapperSc

        本文链接:https://www.haomeiwen.com/subject/cgypbktx.html