前言:
本章节介绍MyBatis-Puls的CRUD使用。在开始之前,先简单讲解下上章节关于Spring Boot是如何自动配置MyBatis-Plus。
一、自动配置
当Spring Boot应用从主方法main()启动后,首先加载Spring Boot注解类@SpringBootApplication。
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
在该类里加载注解类@EnableAutoConfiguration。
在EnableAutoConfiguration类使用注解类@Import导入了AutoConfigurationImportSelector自动配置选择器类来加载其他可自动配置的组件,步骤如下:
1、AutoConfigurationImportSelector自动配置选择器调用getCandidateConfigurations方法,方法中SpringFactoriesLoader类通过loadFactoryNames方法扫描获取各jar包类路径下的META-INF/spring.factories文件
2、将扫描到的META-INF/spring.factories文件封装成Properties对象
3、遍历Properties对象,从中取出属性名org.springframework.boot.autoconfigure.EnableAutoConfiguration.EnableAutoConfiguration对应的值,值就为当前Jar包需Spring Boot加载的配置类,加载到容器中,并根据配置条件实例化配置类中的类对象
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.");
return configurations;
}
private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {
MultiValueMap<String, String> result = (MultiValueMap)cache.get(classLoader);
if (result != null) {
return result;
} else {
try {
//扫描META-INF/spring.factories文件
Enumeration<URL> urls = classLoader != null ? classLoader.getResources("META-INF/spring.factories") : ClassLoader.getSystemResources("META-INF/spr