Spring Boot 启动类使用 @MapperScan 注解替代 @Mapper 注解
@MapperScan 注解是 MyBatis 提供的一个注解,用于自动扫描并注册 Mapper 接口。在 Spring Boot 项目中,可以通过在启动类上添加 @MapperScan 注解来替代 @Mapper 注解,实现 Mapper 接口的自动配置。
例如,以下代码展示了如何在 Spring Boot 启动类上添加 @MapperScan 注解:
@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 包下的所有 Mapper 接口并注册到 Spring 容器中。
使用 @MapperScan 注解可以简化 MyBatis 的配置,提高代码可读性和维护性。
原文地址: https://www.cveoy.top/t/topic/mXul 著作权归作者所有。请勿转载和采集!