mybatis-spring:mapper实例化源码分析

    技术2025-02-04  5

    @Mapper public interface UserDao { @Select("select * from student where sid=1") String query(); } @Configuration @MapperScan("com.su.mybatis.dao") public class MybatisConfig { @Bean public DataSource dataSource(){ DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/mytest?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false"); dataSource.setUsername("root"); dataSource.setPassword("root"); return dataSource; } @Bean public SqlSessionFactoryBean sqlSessionFactoryBean() throws IOException { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(dataSource());//数据源 return sqlSessionFactoryBean; } }

    mybatis正是通过@MapperScan完成与spring的整合。 看一下@MapperScan做了什么 spring默认的自动装配模式是AUTOWIRE_NO,不进行自动装配,需要程序员手动在属性上加@Autowired才能完成注入。

    先来看看,MapperFactoryBean中有哪些属性需要被自动装配(关注set方法) MapperFactoryBean的父类SqlSessionDaoSupport有一个很重要的set方法setSqlSessionFactory(sqlSessionFactory),用于创建一个SqlSessionTemplate对象。 关于方法入参sqlSessionFactory实例对象怎么来的,参考Spring自动注入源码分析这篇文章。 当beanDefinition实例化的时候,会调用MapperFactoryBean的getObject()方法 getMapper()会调用sqlSessionTemplate的getMapper()方法 最终mapperProxyFactory.newInstance(sqlSession)返回的实例长什么样子呢 假设当bean实例已经创建完成,调用mapper的方法,来看一下 准确的说,this.sqlSessionProxy是一个DefaultSqlSession的代理对象。 进入this.sqlSessionProxy. selectOne(statement, parameter)看看,由于this.sqlSessionProxy是一个代理对象,肯定会进入一个invoke()方法里面 熟悉反射都知道,接下来会进入DefaultSqlSession的selectOne(args)方法 那么mybatis是什么时候拿到mapper里的sq语句并且封装在MappedStatement的呢?

    先来看下MapperFactoryBean的层级结构

    相关文章: ImportBeanDefinitionRegistrar解析 代理模式及手写jdk动态代理 Spring-IoC之加载Bean源码分析

    Processed: 0.009, SQL: 9