MyBatis底层基础全面介绍(精髓)

上传人:我*** 文档编号:134500477 上传时间:2020-06-05 格式:PPT 页数:22 大小:282KB
返回 下载 相关 举报
MyBatis底层基础全面介绍(精髓)_第1页
第1页 / 共22页
MyBatis底层基础全面介绍(精髓)_第2页
第2页 / 共22页
MyBatis底层基础全面介绍(精髓)_第3页
第3页 / 共22页
MyBatis底层基础全面介绍(精髓)_第4页
第4页 / 共22页
MyBatis底层基础全面介绍(精髓)_第5页
第5页 / 共22页
点击查看更多>>
资源描述

《MyBatis底层基础全面介绍(精髓)》由会员分享,可在线阅读,更多相关《MyBatis底层基础全面介绍(精髓)(22页珍藏版)》请在金锄头文库上搜索。

1、MyBatis底层基础和拦截器 http www mybatis tk 看一个例子SimpleMyBatis 第一遍从头到尾简单过一遍代码看看代码中的简单注释了解MyBatis内部如何使用这些对象 第二遍我们对照XML来看这段代码了解这些XML和对象之间的关系 代码和XML对照 Configuration 创建配置文件finalConfigurationconfig newConfiguration config setCacheEnabled true config setLazyLoadingEnabled false config setAggressiveLazyLoading tru

2、e 代码和XML对照 拦截器 为了后续说明拦截器 这里添加两个简单例子config addInterceptor newSampleExecutorInterceptor config addInterceptor newSampleResultSetHandlerInterceptor 代码和XML对照 Environment 创建DataSourcePropertiesprops Resources getResourceAsProperties jdbc properties UnpooledDataSourcedataSource newUnpooledDataSource dataS

3、ource setDriver props getProperty driver dataSource setUrl props getProperty url dataSource setUsername props getProperty username dataSource setPassword props getProperty password Transactiontransaction newJdbcTransaction dataSource null false finalExecutorexecutor config newExecutor transaction 代码

4、和XML对照 Cache finalCachecountryCache newSynchronizedCache 同步缓存newSerializedCache 序列化缓存newLoggingCache 日志缓存newLruCache 最少使用缓存newPerpetualCache country cache 持久缓存 msBuilder cache countryCache 相当于将添加到xml的方法上 就是这么复杂的一个对象 我们在中可以配置属性是type和eviction 对应上面代码中PerpetualCache和LruCache 当type不变的时候 就是上面的这个结构 evictio

5、n可以修改LruCache这一层 当type修改的时候 整个对象就是你type设置的这一层缓存对象 不会有这么复杂的结构 修饰模式 https zh wikipedia org wiki 修饰模式 代码和XML对照 select SELECT FROMcountryWHEREid id 这样的一个 或 对应的就是一个MappedStatement 这个对象比较复杂 由很多对象组合而成 其中resultType和resultMap对应了ResultMap对象 id 会产生ParameterMapping中间的SQL会产生一个合适的SqlSource对象 代码和XML对照 resultMap fi

6、nalResultMapresultMap newResultMap Builder config defaultResultMap Country class newArrayList 默认都是普通的 有个flags属性可以设置为add newResultMapping Builder config id id int class build add newResultMapping Builder config countryname countryname String class build add newResultMapping Builder config countrycode

7、 countrycode registry getTypeHandler String class build build 代码和XML对照 resultType finalResultMapresultMap newResultMap Builder config defaultResultMap Country class newArrayList build resultMap和resultType ListresultMaps newArrayList resultMaps add resultMap 设置返回值的resultMapmsBuilder resultMaps result

8、Maps 代码和XML对照 SqlSource StaticSqlSourcesqlSource newStaticSqlSource config SELECT FROMcountryWHEREid SELECT FROMcountryWHEREid id 参数映射 ParameterMap 由于上面的SQL有个参数id 这里需要提供ParameterMapping 参数映射 ListparameterMappings newArrayList 通过ParameterMapping Builder创建ParameterMappingparameterMappings add newParam

9、eterMapping Builder config id registry getTypeHandler int class build 通过ParameterMap Builder创建ParameterMapmsBuilder parameterMap newParameterMap Builder config defaultParameterMap Country class parameterMappings build MappedStatement 将前面说的这些对象组合到MappedStatement中后 就得到了ms 创建msMappedStatement Builderms

10、Builder newMappedStatement Builder config tk mybatis selectCountry sqlSource SqlCommandType SELECT msBuilder parameterMap paramBuilder build 设置返回值的resultMapmsBuilder resultMaps resultMaps 设置缓存msBuilder cache countryCache 创建msMappedStatementms msBuilder build 第三遍我们重点看 SystemMetaObject InterceptorSqlS

11、ource SystemMetaObject用法 protectedvoidsetSqlSource MappedStatementms SqlSourcesqlSource MetaObjectmsObject SystemMetaObject forObject ms msObject setValue sqlSource sqlSource 如果是Jdbc3KeyGenerator 就设置为MultipleJdbc3KeyGeneratorKeyGeneratorkeyGenerator ms getKeyGenerator if keyGeneratorinstanceofJdbc3K

12、eyGenerator msObject setValue keyGenerator newMultipleJdbc3KeyGenerator MappedStatementms MappedStatement invocation getArgs 0 MetaObjectmsObject SystemMetaObject forObject ms DataSourcedataSource DataSource msObject getValue configuration environment dataSource Stringurl dataSource getConnection ge

13、tMetaData getURL SystemMetaObject两个简单例子 拦截器 Interceptor Executor update query flushStatements commit rollback getTransaction close isClosed ParameterHandler getParameterObject setParameters ResultSetHandler handleResultSets handleOutputParameters StatementHandler prepare parameterize batch update qu

14、ery MyBatis允许使用插件来拦截的方法调用包括 SqlSource ProviderSqlSource 由 XXXProvider注解生成的 执行期间 会传入入参 通过具体判断条件转换为StaticSqlSourceRawSqlSource 通过XML解析 静态SQL的 没有等标签的 会转换为StaticSqlSource 这一步会自动生成ParameterMapDynamicSqlSource 通过XML解析 动态SQL的 包含类似等标签的 执行期间 会根据具体条件会转换为StaticSqlSourceStaticSqlSource 上面3个使用 最终使用的都是StaticSqlSource MyBatis的SqlSource有4种类型 MyBatis服务 MyBatis相关技术咨询服务 地址 在www mybatis tk上于2015 11 20 含 之前捐赠过的朋友将会免费获得一次该服务 仅限于QQ咨询 mybatis tk

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > PPT模板库 > PPT素材/模板

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号