@MapperScan

简介:

@MapperScan注解是MyBatis框架在Spring Boot中的一个重要集成注解

作用:

@MapperScan主要作用是告诉Spring框架在启动时扫描指定的包路径,并将该路径下的所有MyBatis的Mapper接口批量注入到Spring容器中。这样,开发者就可以在Spring应用中直接通过@Autowired等方式注入Mapper实例,进行数据库操作,而无需在每个Mapper接口上单独添加@Mapper注解,从而简化了配置,提高了开发效率。

属性:

value/basePackages:用于指定需要扫描的包路径,可以是一个或多个包路径,支持使用通配符。
basePackageClasses:用于指定需要扫描的包路径的类,Spring会通过这个类的包路径来扫描Mapper接口。
annotationClass:用于指定需要扫描的接口上应该具有的注解类型,默认是@Mapper注解。
nameGenerator:用于指定Bean名称生成器,默认是org.mybatis.spring.mapper.BeanNameGenerator。
factoryBean:用于指定生成Mapper Bean的工厂类,默认是org.mybatis.spring.mapper.MapperFactoryBean。

举个栗子:

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

@MapperScan("com.example.mapper")指定了com.example.mapper包下的所有接口都会被MyBatis扫描并注册为Mapper。

指定多个包路径:

@SpringBootApplication
@MapperScan({"com.example.mapper", "com.example.another.mapper"})
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值