-
MybatisPlus的Id填充笔记整理
启动阶段
构建 sqlSessionFactory对象
- MybatisPlusAutoConfiguration#sqlSessionFactory
- 从Spring容器获取 IdentifierGenerator 对象放入到 GlobalConfig对象中
- 设置 GlobalConfig 到 MybatisSqlSessionFactoryBean
- MybatisSqlSessionFactoryBean#getObject -> MybatisSqlSessionFactoryBean#afterPropertiesSet
- MybatisSqlSessionFactoryBean#buildSqlSessionFactory
- 将GlobalConfig保存到GlobalConfigUtils的全局静态变量中(根据Configuration生成唯一hash值)
- new MybatisSqlSessionFactoryBuilder().build(targetConfiguration)构建SqlSessionFactory对象
MybatisSqlSessionFactoryBuilder#build详情
- 根据Configuration获取GlobalConfig对象
- 当globalConfig中identifierGenerator为null时构建默认值放入DefaultIdentifierGenerator.getInstance()
- 并将identifierGenerator保存到IdWorker全局静态变量中
- new DefaultSqlSessionFactory(config)
- 缓存 sqlSessionFactory到globalConfig对象中
执行阶段
自动填充Id执行流程
- SimpleExecutor#doUpdate -> configuration.newStatementHandler(this, ms, parameter, RowBounds.DEFAULT, null, null);
- new RoutingStatementHandler(executor, mappedStatement, parameterObject, rowBounds, resultHandler, boundSql)
- new PreparedStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql)
- configuration.newParameterHandler(mappedStatement, parameterObject, boundSql)
- MybatisXMLLanguageDriver#createParameterHandler
- new MybatisParameterHandler(mappedStatement, parameterObject, boundSql) -> processParameter(parameter)
- MybatisParameterHandler#process(parameter) -> populateKeys(tableInfo, metaObject, entity)
populateKeys业务逻辑详解(以ASSIGN_ID为例)
- 根据实体信息获取IdType类型枚举值
- 从GlobalConfigUtils全局静态变量获取GlobalConfig对象: GlobalConfigUtils.getGlobalConfig(this.configuration)
- 从GlobalConfig中获取IdentifierGenerator#getIdentifierGenerator
- 从输入Entity对象获取id值,如果为空,且IdType枚举值是否为 ASSIGN_ID 或 ASSIGN_UUID
- 调用identifierGenerator.nextId(entity)获取id
- 将上一步获取的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