与spring的整合主要是mapper接口注入进IOC容器。
有两种方法。1.使用@Mapper注解,2.使用@MapperScan(“包”)
@Mapper注解的扫描在自动配置类:AutoConfiguredMapperScannerRegistrar#registerBeanDefinitions

    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {logger.debug("Searching for mappers annotated with @Mapper");// 这是关键。ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);try {if (this.resourceLoader != null) {scanner.setResourceLoader(this.resourceLoader);}List<String> packages = AutoConfigurationPackages.get(this.beanFactory);if (logger.isDebugEnabled()) {for (String pkg : packages) {logger.debug("Using auto-configuration base package '{}'", pkg);}}// 扫描有Mapper注解的类scanner.setAnnotationClass(Mapper.class);// 注册过滤器,包括在内的,排除的scanner.registerFilters();// 传入包位置,将核实的类创建bd,注册进factory.scanner.doScan(StringUtils.toStringArray(packages));} catch (IllegalStateException ex) {logger.debug("Could not determine auto-configuration package, automatic mapper scanning disabled.", ex);}}

org.mybatis.spring.mapper.ClassPathMapperScanner#doScan

  public Set<BeanDefinitionHolder> doScan(String... basePackages) {// 得到目标bdSet<BeanDefinitionHolder> beanDefinitions = super.doScan(basePackages);if (beanDefinitions.isEmpty()) {logger.warn("No MyBatis mapper was found in '" + Arrays.toString(basePackages) + "' package. Please check your configuration.");} else {// 处理dbprocessBeanDefinitions(beanDefinitions);}return beanDefinitions;}
private void processBeanDefinitions(Set<BeanDefinitionHolder> beanDefinitions) {GenericBeanDefinition definition;// 遍历bdfor (BeanDefinitionHolder holder : beanDefinitions) {definition = (GenericBeanDefinition) holder.getBeanDefinition();if (logger.isDebugEnabled()) {logger.debug("Creating MapperFactoryBean with name '" + holder.getBeanName() + "' and '" + definition.getBeanClassName() + "' mapperInterface");}// the mapper interface is the original class of the bean// 设置bd的构造函数参数,实例化是使用该构造器      definition.getConstructorArgumentValues().addGenericArgumentValue(definition.getBeanClassName()); // issue #59// 实例化的class换成了mapperFactoryBean的classdefinition.setBeanClass(this.mapperFactoryBean.getClass());definition.getPropertyValues().add("addToConfig", this.addToConfig);boolean explicitFactoryUsed = false;if (StringUtils.hasText(this.sqlSessionFactoryBeanName)) {definition.getPropertyValues().add("sqlSessionFactory", new RuntimeBeanReference(this.sqlSessionFactoryBeanName));explicitFactoryUsed = true;} else if (this.sqlSessionFactory != null) {definition.getPropertyValues().add("sqlSessionFactory", this.sqlSessionFactory);explicitFactoryUsed = true;}if (StringUtils.hasText(this.sqlSessionTemplateBeanName)) {if (explicitFactoryUsed) {logger.warn("Cannot use both: sqlSessionTemplate and sqlSessionFactory together. sqlSessionFactory is ignored.");}definition.getPropertyValues().add("sqlSessionTemplate", new RuntimeBeanReference(this.sqlSessionTemplateBeanName));explicitFactoryUsed = true;} else if (this.sqlSessionTemplate != null) {if (explicitFactoryUsed) {logger.warn("Cannot use both: sqlSessionTemplate and sqlSessionFactory together. sqlSessionFactory is ignored.");}definition.getPropertyValues().add("sqlSessionTemplate", this.sqlSessionTemplate);explicitFactoryUsed = true;}if (!explicitFactoryUsed) {if (logger.isDebugEnabled()) {logger.debug("Enabling autowire by type for MapperFactoryBean with name '" + holder.getBeanName() + "'.");}// 注入模式是根据类型注入属性definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);}}}

实例化的class变成了mapperfactorybean.
实例化的类的时候,将该接口注册经configuration缓存。

  protected void checkDaoConfig() {super.checkDaoConfig();notNull(this.mapperInterface, "Property 'mapperInterface' is required");Configuration configuration = getSqlSession().getConfiguration();if (this.addToConfig && !configuration.hasMapper(this.mapperInterface)) {try {configuration.addMapper(this.mapperInterface);} catch (Exception e) {logger.error("Error while adding the mapper '" + this.mapperInterface + "' to configuration.", e);throw new IllegalArgumentException(e);} finally {ErrorContext.instance().reset();}}}// 实例化的时候,因为是factorybean,会调用getObejct()方法获得目标类的实例。这是一个代理类。public T getObject() throws Exception {return getSqlSession().getMapper(this.mapperInterface);}

@MapperScan注解 使用的底层的@import注入bean.原来依旧是ClassPathMapperScanner
org.mybatis.spring.annotation.MapperScannerRegistrar#registerBeanDefinitions

  public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {AnnotationAttributes annoAttrs = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(MapperScan.class.getName()));ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);// this check is needed in Spring 3.1if (resourceLoader != null) {scanner.setResourceLoader(resourceLoader);}Class<? extends Annotation> annotationClass = annoAttrs.getClass("annotationClass");if (!Annotation.class.equals(annotationClass)) {scanner.setAnnotationClass(annotationClass);}Class<?> markerInterface = annoAttrs.getClass("markerInterface");if (!Class.class.equals(markerInterface)) {scanner.setMarkerInterface(markerInterface);}Class<? extends BeanNameGenerator> generatorClass = annoAttrs.getClass("nameGenerator");if (!BeanNameGenerator.class.equals(generatorClass)) {scanner.setBeanNameGenerator(BeanUtils.instantiateClass(generatorClass));}Class<? extends MapperFactoryBean> mapperFactoryBeanClass = annoAttrs.getClass("factoryBean");if (!MapperFactoryBean.class.equals(mapperFactoryBeanClass)) {scanner.setMapperFactoryBean(BeanUtils.instantiateClass(mapperFactoryBeanClass));}scanner.setSqlSessionTemplateBeanName(annoAttrs.getString("sqlSessionTemplateRef"));scanner.setSqlSessionFactoryBeanName(annoAttrs.getString("sqlSessionFactoryRef"));List<String> basePackages = new ArrayList<String>();for (String pkg : annoAttrs.getStringArray("value")) {if (StringUtils.hasText(pkg)) {basePackages.add(pkg);}}for (String pkg : annoAttrs.getStringArray("basePackages")) {if (StringUtils.hasText(pkg)) {basePackages.add(pkg);}}for (Class<?> clazz : annoAttrs.getClassArray("basePackageClasses")) {basePackages.add(ClassUtils.getPackageName(clazz));}scanner.registerFilters();scanner.doScan(StringUtils.toStringArray(basePackages));}

与spring的整合相关推荐

  1. spring boot整合spring security笔记

    最近自己做了一个小项目,正在进行springboot和spring Security的整合,有一丢丢的感悟,在这里分享一下: 首先,spring boot整合spring security最好是使用T ...

  2. RabbitMQ使用及与spring boot整合

    1.MQ 消息队列(Message Queue,简称MQ)--应用程序和应用程序之间的通信方法 应用:不同进程Process/线程Thread之间通信 比较流行的中间件: ActiveMQ Rabbi ...

  3. Spring Cloud整合Redis

    2019独角兽企业重金招聘Python工程师标准>>> 项目需要使用Redis来做缓存,研究了一下如何将其与Spring Boot整合.网上的demo要么就是太过于庞大,要么就是版本 ...

  4. Spring Boot 教程(三): Spring Boot 整合Mybatis

    教程简介 本项目内容为Spring Boot教程样例.目的是通过学习本系列教程,读者可以从0到1掌握spring boot的知识,并且可以运用到项目中.如您觉得该项目对您有用,欢迎点击收藏和点赞按钮, ...

  5. 五、spring boot整合mybatis-plus

    spring boot整合mybatis-plus 简介 mybatis 增强工具包,简化 CRUD 操作. 文档 http://mp.baomidou.com http://mybatis.plus ...

  6. spring boot 整合mybatis 无法输出sql的问题

    使用spring boot整合mybatis,测试功能的时候,遇到到了sql问题,想要从日志上看哪里错了,但是怎么都无法输出执行的sql,我使用的是log4j2,百度了一下,很多博客都说,加上下面的日 ...

  7. struts2+hibernate-jpa+Spring+maven 整合(1)

    1.0.0 struts2 与 spring 的整合. 1.1.0 新建maven工程 , 编写pom.xml ,这里只需要简单的添加 一个组件就够了: 在myeclipse 生成的pom.xml 添 ...

  8. Spring boot 整合 Mybatis 实现增删改查(MyEclipse版)

    1.首先搭建好一个Spring boot 程序,编写好启动类. 启动类代码如下: @SpringBootApplication public class Start {public static vo ...

  9. spring boot 系列之四:spring boot 整合JPA

    上一篇我们讲了spring boot 整合JdbcTemplate来进行数据的持久化, 这篇我们来说下怎么通过spring boot 整合JPA来实现数据的持久化. 一.代码实现 修改pom,引入依赖 ...

  10. MongoDB和Java(4):Spring Data整合MongoDB(XML配置)

    最近花了一些时间学习了下MongoDB数据库,感觉还是比较全面系统的,涉及了软件安装.客户端操作.安全认证.副本集和分布式集群搭建,以及使用Spring Data连接MongoDB进行数据操作,收获很 ...

最新文章

  1. 软件开发过程中遇到的问题
  2. struts+spring+hibernate总结
  3. SQL Server数据库查询速度慢的原因和解决方法
  4. jquery动画顺序执行_jquery怎样设置才能做完上一步动画再执行下一步
  5. python 教程 网盘-python教程网盘
  6. GetClientRect()和GetWindowRect()
  7. din算法 代码_深度兴趣网络(DIN,Deep Interest Network)
  8. curPos和tgtPos
  9. php模拟远程提交get 、post 实例函数
  10. 最小生成树——克鲁斯卡尔算法
  11. 神经网络模型中有什么样的算子_浅析图卷积神经网络
  12. 微课|玩转Python轻松过二级(1.5节):安装扩展库
  13. 水稻细菌性条斑病的分割与严重程度估计方法
  14. python连续质数计算代码分析,素性测试的Miller-Rabin算法完全解析 (C语言实现、Python实现)...
  15. springboot日志管理_最近Springboot有点火,只是因为面试问的频率高吗?
  16. oracle没有卸载,oracle卸载没有正确卸载怎么办?
  17. 硬件设计-基于热敏电阻的水温检测控制系统(模电课设)
  18. 网易云发送验证码短信,发送通知短信,java版
  19. 搭建基于DataX的可视化界面
  20. 洋洋洒洒688字带你彻底吃透Zuul的插件机制及定制化开发

热门文章

  1. php 编译 sass,如何在Symfony 3中使用纯PHP编译SASS(scss)
  2. 计算机控制技术数据存储器有,计算机控制技术复习资料.doc
  3. 卷积神经网络CNN基本原理和相关基本概念
  4. IMPORTANT: You may need to close and restart your shell after running ‘conda init‘.
  5. archlinux mysql_在Arch Linux中安装MySQL
  6. sql中如何将视图中某一字段相同的数据合并 不同内容的字段相加_Tableau基础如何合并你的数据?理解与逻辑(上)...
  7. oracle如何查找谁删除了数据_php如何删除session中数据
  8. python备注(持续更新……)
  9. 【 Educational Codeforces Round 71 (Rated for Div. 2) F】Remainder Problem【分块】
  10. 【带权并查集经典例题】银河英雄传说【同POJ 1988 cube stacking】