1. 程式人生 > 其它 >Spring Boot掃描mapper介面類的兩種實現方式

Spring Boot掃描mapper介面類的兩種實現方式

技術標籤:系統架構解決方案mybatisspring boot

1、方式一:使用註解@Mapper

在所有mapper介面上添加註解@Mapper;Spring boot啟動註解自動掃描。

以下是Spring Boot預設掃描配置,自動開啟自動掃描,即啟動就會自動掃描所有自定義bean

2、方式二:使用註解@MapperScan

在springboot啟動類上添加註解@MapperScan,標註dao所在包路徑。一勞永逸,推薦使用!!

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableSwagger2
@EnableDiscoveryClient
@MapperScan(basePackages = {"com.mp.service.provider.dao"})
@SpringBootApplication
public class MpServiceProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(MpServiceProviderApplication .class, args);
    }
}