SpringBoot复习之配置MyBatis

1.声明

当前内容主要为复习和理解如何在当前的SpringBoot中MyBatis

2. 查看解析

1.通过查看源码方式发现了mybatis-spring-boot-starter中存在一个org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration

发现注释:

/**
 * {@link EnableAutoConfiguration Auto-Configuration} for Mybatis. Contributes a
 * {@link SqlSessionFactory} and a {@link SqlSessionTemplate}.
 *
 * If {@link org.mybatis.spring.annotation.MapperScan} is used, or a
 * configuration file is specified as a property, those will be considered,
 * otherwise this auto-configuration will attempt to register mappers based on
 * the interface definitions in or under the root auto-configuration package.
 *
 * @author Eddú Meléndez
 * @author Josh Long
 * @author Kazuki Shimizu
 * @author Eduardo Macarrón
 */

如果使用了@org.mybatis.spring.annotation.MapperScan,注解就可以扫描dao层并创建代理了

2.查看创建sqlSessionFactory的bean创建方法

@Bean
  @ConditionalOnMissingBean
  public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
    
    
    SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
    factory.setDataSource(dataSource);
    factory.setVfs(SpringBootVFS.class);
    if (StringUtils.hasText(this.properties.getConfigLocation())) {
    
    
      factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
    }
    factory.setConfiguration(properties.getConfiguration());
    if (!ObjectUtils.isEmpty(this.interceptors)) {
    
    
      factory.setPlugins(this.interceptors);
    }
    if (this.databaseIdProvider != null) {
    
    
      factory.setDatabaseIdProvider(this.databaseIdProvider);
    }
    if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) {
    
    
      factory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
    }
    if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
    
    
      factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
    }
    if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
    
    
      factory.setMapperLocations(this.properties.resolveMapperLocations());
    }

    return factory.getObject();
  }

发现一个非常明显的属性this.properties,然后找到MybatisProperties这个类

@ConfigurationProperties(prefix = MybatisProperties.MYBATIS_PREFIX)

// 即使用mybatis.属性方式在application.properties就可注入到这个配置类中

然后通过查看代码发现:AutoConfiguredMapperScannerRegistrar这个东西

  public static class AutoConfiguredMapperScannerRegistrar
      implements BeanFactoryAware, ImportBeanDefinitionRegistrar, ResourceLoaderAware {
    
    

    private BeanFactory beanFactory;

    private ResourceLoader resourceLoader;

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
    
    

      log.debug("Searching for mappers annotated with @Mapper'");

      ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);

      try {
    
    
        if (this.resourceLoader != null) {
    
    
          scanner.setResourceLoader(this.resourceLoader);
        }

        List<String> pkgs = AutoConfigurationPackages.get(this.beanFactory);
        for (String pkg : pkgs) {
    
    
          log.debug("Using auto-configuration base package '" + pkg + "'");
        }

        scanner.setAnnotationClass(Mapper.class);
        scanner.registerFilters();
        scanner.doScan(StringUtils.toStringArray(pkgs));
      } catch (IllegalStateException ex) {
    
    
        log.debug("Could not determine auto-configuration " + "package, automatic mapper scanning disabled.");
      }
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
    
    
      this.beanFactory = beanFactory;
    }

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
    
    
      this.resourceLoader = resourceLoader;
    }
  }

发现了这样一句话:Searching for mappers annotated with @Mapper,也就是说没有@MapperScan,但是mapper接口上游@Mapper也是可以创建接口代理的,实现MapperScan同样的方法

3.总结

  1. 在SpringBoot中的application.properties配置扫描的映射文件Mapper.xml即可加载配置文件
    mybatis.mapper-locations=classpath:com/hy/filesys/mapper/*Mapper.xml

  2. 扫描dao层的接口可以使用两种方式:使用配置类上面添加@MapperScan或者在接口上添加@Mapper都可以实现扫描

  3. 还可以手动创建类似Spring和MyBatis整合时候的bean

  4. 所以为了方便,一般在application.properties中配置扫描的mapper.xml文件,在配置类上面添加@MapperScan或者在dao接口上面配置@Mapper

猜你喜欢

转载自blog.csdn.net/weixin_45492007/article/details/113843631