• MybatisPlus的Id填充笔记整理


    启动阶段

    构建 sqlSessionFactory对象
    1. MybatisPlusAutoConfiguration#sqlSessionFactory
    2. 从Spring容器获取 IdentifierGenerator 对象放入到 GlobalConfig对象中
    3. 设置 GlobalConfig 到 MybatisSqlSessionFactoryBean
    4. MybatisSqlSessionFactoryBean#getObject -> MybatisSqlSessionFactoryBean#afterPropertiesSet
    5. MybatisSqlSessionFactoryBean#buildSqlSessionFactory
    6. 将GlobalConfig保存到GlobalConfigUtils的全局静态变量中(根据Configuration生成唯一hash值)
    7. new MybatisSqlSessionFactoryBuilder().build(targetConfiguration)构建SqlSessionFactory对象
    MybatisSqlSessionFactoryBuilder#build详情
    1. 根据Configuration获取GlobalConfig对象
    2. 当globalConfig中identifierGenerator为null时构建默认值放入DefaultIdentifierGenerator.getInstance()
    3. 并将identifierGenerator保存到IdWorker全局静态变量中
    4. new DefaultSqlSessionFactory(config)
    5. 缓存 sqlSessionFactory到globalConfig对象中

    执行阶段

    自动填充Id执行流程
    1. SimpleExecutor#doUpdate -> configuration.newStatementHandler(this, ms, parameter, RowBounds.DEFAULT, null, null);
    2. new RoutingStatementHandler(executor, mappedStatement, parameterObject, rowBounds, resultHandler, boundSql)
    3. new PreparedStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql)
    4. configuration.newParameterHandler(mappedStatement, parameterObject, boundSql)
    5. MybatisXMLLanguageDriver#createParameterHandler
    6. new MybatisParameterHandler(mappedStatement, parameterObject, boundSql) -> processParameter(parameter)
    7. MybatisParameterHandler#process(parameter) -> populateKeys(tableInfo, metaObject, entity)
    populateKeys业务逻辑详解(以ASSIGN_ID为例)
    1. 根据实体信息获取IdType类型枚举值
    2. 从GlobalConfigUtils全局静态变量获取GlobalConfig对象: GlobalConfigUtils.getGlobalConfig(this.configuration)
    3. 从GlobalConfig中获取IdentifierGenerator#getIdentifierGenerator
    4. 从输入Entity对象获取id值,如果为空,且IdType枚举值是否为 ASSIGN_ID 或 ASSIGN_UUID
    5. 调用identifierGenerator.nextId(entity)获取id
    6. 将上一步获取的id值赋值给待insert的实体对象
  • 相关阅读:
    docker镜像创建成功之后设置多个数据卷挂载
    C#【高级篇】 C# 多线程
    EasyNLP集成K-BERT算法,借助知识图谱实现更优Finetune导读
    spring 注解: 更加简单的存储 Bean
    Windows与Linux之间的文件互传
    python中scipy求解方法
    XPS问题收集及解答-科学指南针
    刚来的00后真的卷,听说工作还没两年,跳到我们公司直接起薪20k...
    0源码基础学习Spring源码系列(一)——Bean注入流程
    DDD防腐层设计
  • 原文地址:https://blog.csdn.net/yichengjie_c/article/details/134358108