网站地图怎么做XML,中小型网站建设信息,网站出现风险如何处理,顺德网络科技有限公司原理图#xff1a;Configuration解析#xff1a;Configuration表示配置#xff0c;该对象中维护了很多mybatis的配置参数#xff1b;大致可分为四部分#xff1a;1.环境变量Environment 2.配置参数#xff1b;3.缓存集合#xff1b;4.插件及其他1.1环境变量EnvironmentE…原理图Configuration解析Configuration表示配置该对象中维护了很多mybatis的配置参数大致可分为四部分1.环境变量Environment 2.配置参数3.缓存集合4.插件及其他1.1环境变量EnvironmentEnvironment.java源码通过Environment源码可以看出Environment仅维护了一个id 唯一标识TransactionFactory(事物工厂负责事物管理Mybatis提供了两套事物管理1.org.apache.ibatis.transaction.jdbc 基于jdbc实现事物管理2.org.apache.ibatis.transaction.managed 用于扩展第三方事物管理)和DataSource(数据源)。1.2配置参数Configuration提供了一些默认配置参数并可通过get/set方法修改参数值1.3缓存集合:Configuration维护了很多缓存容器如cachesresulrtMaps等’Configuration初始化1.api构建2.xml初始化Configuration初始化MappendStatement解析该对象是mapper.xml在对象中的体现是整个mybatis框架中最为核心的对象我们也可以不必通过xml文件来构建该对象可以直接通过编码方式构建像最常用的简单的增删改查操作完全可以手动构建mappedStatement对象并加入到mybatis容器中这样我们就不需要在xml文件中手写CRUD操作了,mybatis-plus框架设计的思想就是鉴于此。public final class MappedStatement { private String resource; private Configuration configuration; //sql的ID private String id; //尝试影响驱动程序每次批量返回的结果行数和这个设置值相等 private Integer fetchSize; //SQL超时时间 private Integer timeout; //Statement的类型STATEMENT/PREPARE/CALLABLE private StatementType statementType; //结果集类型FORWARD_ONLY/SCROLL_SENSITIVE/SCROLL_INSENSITIVE private ResultSetType resultSetType; //表示解析出来的SQL private SqlSource sqlSource; //缓存 private Cache cache; //已废弃 private ParameterMap parameterMap; //对应的ResultMap private List resultMaps; private boolean flushCacheRequired; private boolean useCache; private boolean resultOrdered; //SQL类型INSERT/SELECT/DELETE private SqlCommandType sqlCommandType; //和SELECTKEY标签有关 private KeyGenerator keyGenerator; private String[] keyProperties; private String[] keyColumns; private boolean hasNestedResultMaps; //数据库ID用来区分不同环境 private String databaseId; private Log statementLog; private LanguageDriver lang; //多结果集时 private String[] resultSets; ...}源码分析1.SqlSessionFactory 装配Configuration对象2.将配置文件中的mapers注册到configuration的mapperRegistry中Mybatis代理模式原理分析buildSqlSessionFactory方法XMLMapperBuilder xmlMapperBuilder new XMLMapperBuilder(mapperLocation.getInputStream(), configuration, mapperLocation.toString(), configuration.getSqlFragments());xmlMapperBuilder.parse();SqlSession提供select/insert/update/delete方法在旧版本中使用使用SqlSession接口的这些方法但是新版的Mybatis中就会建议使用Mapper接口的方法。射器其实就是一个动态代理对象进入到MapperMethod的execute方法就能简单找到SqlSession的删除、更新、查询、选择方法从底层实现来说通过动态代理技术让接口跑起来之后采用命令模式最后还是采用了SqlSession的接口方法(getMapper()方法等到Mapper)执行SQL查询(也就是说Mapper接口方法的实现底层还是采用SqlSession接口方法实现的)。解析XMLStatementBuilder.java:将xml文件转换成MappedStatement对象public void parseStatementNode() { String id context.getStringAttribute(id); String databaseId context.getStringAttribute(databaseId); if (!databaseIdMatchesCurrent(id, databaseId, this.requiredDatabaseId)) { return; } Integer fetchSize context.getIntAttribute(fetchSize); Integer timeout context.getIntAttribute(timeout); String parameterMap context.getStringAttribute(parameterMap); String parameterType context.getStringAttribute(parameterType); Class parameterTypeClass resolveClass(parameterType); String resultMap context.getStringAttribute(resultMap); String resultType context.getStringAttribute(resultType); String lang context.getStringAttribute(lang); LanguageDriver langDriver getLanguageDriver(lang); Class resultTypeClass resolveClass(resultType); String resultSetType context.getStringAttribute(resultSetType); StatementType statementType StatementType.valueOf(context.getStringAttribute(statementType