网站建设太难了,仿制网站建设,wordpress模板影视,建站之星网站 seo优化前言#xff1a;SQLSession是对JDBC的封装
一#xff1a;SQLSession和JDBC的对照说明 左边是我们的客户端程序#xff0c;右边是我们的MySQL数据仓#xff0c;或者叫MySQL实例
Mybatis是对JDBC的封装#xff0c;将JDBC封装成了一个核心的SQLSession对象 JDBC当中的核心对…前言SQLSession是对JDBC的封装
一SQLSession和JDBC的对照说明 左边是我们的客户端程序右边是我们的MySQL数据仓或者叫MySQL实例
Mybatis是对JDBC的封装将JDBC封装成了一个核心的SQLSession对象 JDBC当中的核心对象Connection、Statement、ResultSet
二三种Statement补充说明
Statement普通的Statement PeparedStatement预编译Statement CallableStatement适用于存储过程Statement
三Statement的作用
通过这些Statement与我们的数据库进行交互然后由我们的结果集对象ResultSet对象进行封装。 SqlSession是对以上内容进行了封装。 相对于以上来讲SQLSession是对JDBC的封装SQLSessionFactory是创建SQLSession对象的工厂我们还基于mybatis-config.xml配置Mybatis并且在Mapper.xml当中配置SQL了解到这里我们对于Mybatis的认知就比较权限 在Java中或者说在JVM当中对Mybatis相关的配置信息进行封装。这里边设计到很多的配置文件我们不可能说用点就读一次文件这样会有极大的IOIO是操作系统层面的资源他的创建绝不是虚拟机单独完成的是很耗时的少操作或者能复用最好。 对于这种东西我们都是一次性读取存储在Java对象当中 MyBatis当中的配置信息一共有两种mybatis-config.xml和DaoMapper.xml。 其中mybatis-config.xml封装成了org.apache.ibatis.session.Configuration对象DAOMapper.xml封装成了MapperdStatement部分数据是在Configuration当中进行保存的。 基于以上认知我们可以知道在Mybatis当中有两类对象数据储存类对象 操作类对象。
第一章Configuration对象
Configuration是数据存储类对象是将Mybatis当中的mybatis-config.xml封装成Configuration对象Mapper.xml封装成了MappedStatement对象
一mybatis-config.xml与Configuration属性的映射关系
1标签environments
mybatis-config.xml中的environments 标签 environments defaultdefaultenvironment iddefaulttransactionManager typeJDBC/transactionManagerdataSource typePOOLEDproperty namedriver valuecom.mysql.jdbc.Driver/propertyproperty nameurl valuejdbc:mysql://localhost:3306/suns?useSSLfalse/propertyproperty nameusername valueroot/propertyproperty namepassword value123456/property/dataSource/environment/environmentsConfiguration当中的对应属性
public class Configuration {protected Environment environment;}
2标签settings
mybatis-config.xml中的s标签 settings-- 应用二级缓存的一个内容。setting namecacheEnabled valuetrue//settingsConfiguration当中的 protected boolean safeRowBoundsEnabled;protected boolean safeResultHandlerEnabled true;protected boolean mapUnderscoreToCamelCase;-- 关联属性的懒加载配置protected boolean aggressiveLazyLoading;protected boolean multipleResultSetsEnabled true;-- 主键生成的配置protected boolean useGeneratedKeys;protected boolean useColumnLabel true;-- 应用二级缓存的一个内容protected boolean cacheEnabled true;protected boolean callSettersOnNulls;protected boolean useActualParamName true;protected boolean returnInstanceForEmptyRow;cacheEnabled从这个属性我们可以看到这个我们可以写也可以不写因为我们不写的话我们默认走的就是默认值。
3标签typeAliases
mybatis-config.xml中的s标签
typeAliasestypeAlias typecom.baizhiedu.entity.User aliasUser/typeAlias typecom.baizhiedu.entity.Account aliasAccount/
/typeAliasesConfiguration当中的 protected final MapperRegistry mapperRegistry new MapperRegistry(this);protected final InterceptorChain interceptorChain new InterceptorChain();protected final TypeHandlerRegistry typeHandlerRegistry new TypeHandlerRegistry();protected final TypeAliasRegistry typeAliasRegistry new TypeAliasRegistry();protected final LanguageDriverRegistry languageRegistry new LanguageDriverRegistry();4标签Mappers
mybatis-config.xml中的s标签 mappers!--package name--mapper resourceUserDAOMapper.xml/mapper resourceAccountDAOMapper.xml//mappersConfiguration当中的
protected final SetString loadedResources new HashSetString();二mapper.xml与Configuration属性的映射关系
Configuration当中的 protected final MapString, MappedStatement mappedStatements new StrictMapMappedStatement(Mapped Statements collection);protected final MapString, Cache caches new StrictMapCache(Caches collection);protected final MapString, ResultMap resultMaps new StrictMapResultMap(Result Maps collection);protected final MapString, ParameterMap parameterMaps new StrictMapParameterMap(Parameter Maps collection);protected final MapString, KeyGenerator keyGenerators new StrictMapKeyGenerator(Key Generators collection);cachesparameterMapsresultMapsMapperdStatement,keyGenerators 这些是把Mapper.xml文件中的内容进行了封装。 resultMaps所有的Mapper.xml文件中resultMap标签。 parameterMaps是对sql标签上的parameterMap是属性做了处理。
上边这些属性都加了S都代表了是复数也就是他的数量不只一个。这玩意存储的不是公共的而是所有的。里边存储了对于所有的Mapper.xml文件中的这些属性都封装到这里边了。
这些不仅仅要存还要用所以是将他们存入到了一个Map中他是有key的他的key就是namespace.id。所以你就发现这一组。这些对象封装到Configuration对象中之后都是采用的MapString,xxx这样的形式key是namespace.id的形式。
三Configuration对象可以创建操作类对象
new 就是创造这里边创造了很多Mybatis核心的对象 这个Configuration类是整个Mabatis当中的核心类把不仅仅是把Mybatis其他涉及到的核心对象也创建出来不仅仅是上述存储类对象其中就包括ExcuterStatementHanlerResultHandlerParamerHandler
public ParameterHandler newParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {ParameterHandler parameterHandler mappedStatement.getLang().createParameterHandler(mappedStatement, parameterObject, boundSql);parameterHandler (ParameterHandler) interceptorChain.pluginAll(parameterHandler);return parameterHandler;}public ResultSetHandler newResultSetHandler(Executor executor, MappedStatement mappedStatement, RowBounds rowBounds, ParameterHandler parameterHandler,ResultHandler resultHandler, BoundSql boundSql) {ResultSetHandler resultSetHandler new DefaultResultSetHandler(executor, mappedStatement, parameterHandler, resultHandler, boundSql, rowBounds);resultSetHandler (ResultSetHandler) interceptorChain.pluginAll(resultSetHandler);return resultSetHandler;}public StatementHandler newStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {StatementHandler statementHandler new RoutingStatementHandler(executor, mappedStatement, parameterObject, rowBounds, resultHandler, boundSql);statementHandler (StatementHandler) interceptorChain.pluginAll(statementHandler);return statementHandler;}public Executor newExecutor(Transaction transaction) {return newExecutor(transaction, defaultExecutorType);}public Executor newExecutor(Transaction transaction, ExecutorType executorType) {executorType executorType null ? defaultExecutorType : executorType;executorType executorType null ? ExecutorType.SIMPLE : executorType;Executor executor;if (ExecutorType.BATCH executorType) {executor new BatchExecutor(this, transaction);} else if (ExecutorType.REUSE executorType) {executor new ReuseExecutor(this, transaction);} else {executor new SimpleExecutor(this, transaction);}if (cacheEnabled) {executor new CachingExecutor(executor);}executor (Executor) interceptorChain.pluginAll(executor);return executor;}四Configuration对象的作用
作用一封装Mybatis-Config.xml先关的内容。 environments属性封装的environments标签 接下来图里边的是typeAliases标签实体全限定类型和简称的映射这个也在Configuration当中也有封装 Mappers标签我们在Configuration当中也是有对象进行对应的。其中对应的是 Set loadResources 到这Mybatis-config.xml所有的标签我们在configuration对象当中就都可以找到了。
作用二Configuration将xxxMapper.xml封装成了MapperStatment对象组放到了Configurantion对象中进行引用。 Configuration中的属性是MapString,MappedStatement mappedStatements 其中的String还是nameSpace.id Configuration对象还包括还有其他的结果集参数使用返回参数keycachesparameterMapsresultMapsMapperdStatement,keyGenerators 等等。
作用三他的第三个核心作用就是帮我们创建Mybatis其他涉及到的核心对象也创建出来所以我们认为他是Mybatis当中最为核心的对象。 在这里可以认为Configuration实现是这些对象的执行对象的工厂对象。
第二章MappedStatement对象
一MappedStatement属性
public final class MappedStatement {private String resource;private Configuration configuration;private String id;private Integer fetchSize;private Integer timeout;private StatementType statementType;private ResultSetType resultSetType;private SqlSource sqlSource;private Cache cache;private ParameterMap parameterMap;private ListResultMap resultMaps;private boolean flushCacheRequired;private boolean useCache;private boolean resultOrdered;private SqlCommandType sqlCommandType;private KeyGenerator keyGenerator;private String[] keyProperties;private String[] keyColumns;private boolean hasNestedResultMaps;private String databaseId;private Log statementLog;private LanguageDriver lang;private String[] resultSets;}
二MappedStatement和Mapper.xml关系
MappedStatement对像也是一个存储了对象存储的是Mapper文件中的Statement也就是我们定义的SQL标签其中封装的是我们Mapper文件中的一个个标签举例来讲 其中一个标签就会被封装成MappedStatement对象
我们的标签当中肯定会有id的属性在我们的MappedStatement当中也会有id的属性。id属性完全唯一他存储的是namespace.id所以也是唯一注定了在一个Mabatis当中会有N个MapperStatement对象。
这里边的statementType是什么意思指的就是普通预编译存储过程。默认使用的就是preparedStatement所以在我们的SQL标签上也肯定有这个属性这个属性默认一定是prepared
四MappedStatement和Configuration对象关系
MappedStatement当中可以找到Configuration对象Configurantion对象可以找到MapperdStatement对象他俩互相引用双向关联可以互相找到。
尾声
什么时候创建Configuration什么时候创建MappedStatement,以及他与我们的SQLSessionsMybatis核心功能是怎么交互的呀我们后续再讲操作类对象我们下篇文章在进行分析。
操作类对象大致有一下几种
Excutor StatementHandler ParameterHandler ResultSetHandler TypeHandler
这些对象是Configuration对象进行创建的。有了操作类对象之后我们基于上述存储类对象我们就可以对数据库进行相应的操作了。