mysql-plus mapper实现分析

1.jar包版本:mybatis-plus-boot-starter 3.5.3.1

还是从MybatisPlusAutoConfiguration开始说。

创建MybatisSqlSessionFactoryBean时在applyConfiguration方法中为factoory对象configuration属性设置了MybatisConfiguration类型的对象。

在创建对象SqlSessionTemplate时将sqlSessionFactory作为构造参数传入了SqlSessionTemplate对象。

在定义MapperScannerRegistrarNotFoundConfiguration类时使用了@import注解导入了AutoConfiguredMapperScannerRegistrar类,这个类实现了ImportBeanDefinitionRegistrar接口,会调用接口的registerBeanDefinitions方法设置bean定义。

这里引入了MapperScannerConfigurer的bean定义。这个类实现了BeanDefinitionRegistryPostProcessor接口,BeanDefinitionRegistryPostProcessor在AbstractApplicationContext的refresh方法中会被调用。

继续跟MapperScannerConfigurer的postProcessBeanDefinitionRegistry方法会调用ClassPathMapperScanner的processBeanDefinitions方法。

在processBeanDefinitions方法中对bean定义设置了beanclass为MapperFactoryBean,mapperInterface为业务的Mapper接口。在创建bean时便会创建一个MapperFactoryBean对象。

MapperFactoryBean继承了SqlSessionDaoSupport类,该类实现了InitializingBean接口,所以在创建MapperFactoryBean对象调用init方法之前会调用afterPropertiesSet方法,该方法会调用到checkDaoConfig方法将mapper接口放入MybatisConfiguration的MybatisMapperRegistry对象的knownMappers属性中。

在创建业务mapper(我们这里是TestMapper)的bean对象时最终创建的是MybatisMapperProxyFactory对象。跟踪MybatisMapperAnnotationBuilder的parse方法可以发现BaseMaper中方法的生成方式。一直跟到injectMappedStatement方法的调用。

这些类都是AbstractMethod的子类,这类的名称是不是与BaseMapper中的方法一样。以DeleteById这个类为例,看injectMappedStatement的第二句代码SqlMethod sqlMethod = SqlMethod.LOGIC_DELETE_BY_ID,SqlMethod.LOGIC_DELETE_BY_ID的内容为

是不是sql语句就出来了。

接着先前的问题看SqlSessionFactory的创建。

这里的属性SqlSessionFactory在MapperFactoryBean类以及父类中不存在,那他是怎么来的?

那就跟unsatisfiedNonSimpleProperies这个方法,在这个方法中调用getCachedIntrospectionResults方法,在这个放方法中又调用CachedIntrospectionResults.forClass(getWrappedClass())。一路跟到Introspector的getTargetPropertyInfo方法。

在这个方法中会解析set/get方法,拿到SqlSessionFactory属性。

回到MybatisMapperProxyFactory类上来,这个类的newInstance创建的是一个代理执行的是methodCache的MapperMethodInvoker中的inoke方法。

PlainMethodInvoker的实现类PlainMethodInvoker代用MybatisMapperMethod的execute方法。

下面就是查询的逻辑了,就可以结合上一篇分页插件实现联系起来了,整个流程就走通了。

总结流程:

修改mapper定义中的beanclass属性为MapperFactoryBean,创建MapperFactoryBean时会调用InitializingBean的afterPropertiesSet方法,在该方法中调用addMapper设置MybatisMapperRegistry对象的knownMappers属性,保存接口与MybatisMapperProxyFactory的映射关系。

创建mapper时会代用MybatisFactoryBean的getObject,然后调用MybatisMapperRegistry的getMapper接口从knownMappers获取到MybatisMapperProxyFactory,通过MybatisMapperProxyFactory.newInstance创建代理对象MybatisMapperProxy。MybatisMapperProxy调用MapperMethodInvoker。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
创建和获取 MyBatis-Plus 的 Mapper 对象,可以通过以下两种方式进行: 1. 使用 Spring Boot 自动装配 如果您的项目使用了 Spring Boot,可以直接使用 Spring Boot 提供的自动装配机制来创建和获取 MyBatis-Plus 的 Mapper 对象。具体来说,您需要完成以下两个步骤: 1. 在 `application.properties` 或 `application.yml` 中配置 MyBatis-Plus 的相关参数,包括数据源配置、Mapper 扫描路径等。例如: ```yaml # 数据源配置 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase spring.datasource.username=root spring.datasource.password=123456 # MyBatis-Plus 配置 mybatis-plus.mapper-locations=classpath:mapper/**/*.xml mybatis-plus.type-aliases-package=com.example.entity ``` 2. 在需要使用 Mapper 对象的地方,通过 `@Autowired` 注解注入对应的 Mapper 对象即可。例如: ```java @RestController public class MyController { @Autowired private MyMapper myMapper; @GetMapping("/getVariable") public String getVariable() { return myMapper.getVariable(); } } ``` 在上述代码中,我们使用 `@Autowired` 注解将 `MyMapper` 对象注入到了 `MyController` 控制器中,并在 `getVariable` 方法中使用该对象查询数据库并返回结果。 2. 手动创建 Mapper 对象 如果您的项目没有使用 Spring Boot,或者需要手动创建 Mapper 对象,可以通过以下步骤进行: 1. 在 `pom.xml` 中引入 MyBatis-Plus 的依赖: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatis-plus.version}</version> </dependency> ``` 2. 在需要使用 Mapper 对象的地方,通过 `SqlSessionFactory` 和 `MapperRegistry` 对象手动创建 Mapper 对象。例如: ```java public class MyBatisPlusUtils { // 获取 SqlSessionFactory 对象 private static SqlSessionFactory getSqlSessionFactory() { // 创建数据源对象 DataSource dataSource = new PooledDataSource( "com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/mydatabase", "root", "123456" ); // 创建 MyBatis 配置对象 MybatisConfiguration configuration = new MybatisConfiguration(); configuration.setMapUnderscoreToCamelCase(true); // 创建 SqlSessionFactory 对象 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder() .build(configuration, dataSource); return sessionFactory; } // 获取 Mapper 对象 public static <T> T getMapper(Class<T> mapperClass) { // 创建 SqlSessionFactory 对象 SqlSessionFactory sessionFactory = getSqlSessionFactory(); // 创建 MapperRegistry 对象 MapperRegistry mapperRegistry = new MapperRegistry(sessionFactory.getConfiguration()); // 注册 Mapper 对象 mapperRegistry.addMapper(mapperClass); // 获取 Mapper 对象 return sessionFactory.openSession().getMapper(mapperClass); } } ``` 在上述代码中,我们定义了一个 `getSqlSessionFactory` 方法,用于创建 `SqlSessionFactory` 对象;并定义了一个 `getMapper` 方法,用于手动创建 Mapper 对象。在 `getMapper` 方法中,我们首先通过 `getSqlSessionFactory` 方法获取到了 `SqlSessionFactory` 对象,然后创建了一个 `MapperRegistry` 对象,并将需要创建的 Mapper 对象注册到了该对象中;最后通过 `SqlSessionFactory` 对象的 `openSession` 方法获取到了一个 `SqlSession` 对象,并调用该对象的 `getMapper` 方法获取到了需要的 Mapper 对象。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值