MapperScannerConfigurer中获取applicayion.yml配置,进行动态加载BasePackage
由于在MapperScannerConfigurer的bean 优先于@value,导致@value取出来的时候都是null,所以只能使用Environment 来获取值
import org.mybatis.spring.mapper.MapperScannerConfigurer; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; @Configuration public class MyBatisConfig { @Value("${mybatis.mapper.basePackage}") private String basePackage; @Bean public MapperScannerConfigurer mapperScannerConfigurer(Environment environment) {
environment.getProperty("xxx.xxxx") MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer(); // 如果需要使用环境变量中的值,可以这样设置 mapperScannerConfigurer.setBasePackage(environment.getProperty("mybatis.mapper.basePackage")); // 其他配置... return mapperScannerConfigurer; } }