启动阶段
构建 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的实体对象