当前位置: 首页 > news >正文

专业信息门户网站建设石家庄网站建设方案优化

专业信息门户网站建设,石家庄网站建设方案优化,硅云买域名做网站,青海省电话黄页前言:SQLSession是对JDBC的封装 一:SQLSession和JDBC的对照说明 左边是我们的客户端程序,右边是我们的MySQL数据仓,或者叫MySQL实例 Mybatis是对JDBC的封装,将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相关的配置信息进行封装。这里边设计到很多的配置文件,我们不可能说用点就读一次文件,这样会有极大的IO,IO是操作系统层面的资源,他的创建绝不是虚拟机单独完成的,是很耗时的,少操作或者能复用最好。 对于这种东西,我们都是一次性读取,存储在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 default="default"><environment id="default"><transactionManager type="JDBC"></transactionManager><dataSource type="POOLED"><property name="driver" value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql://localhost:3306/suns?useSSL=false"></property><property name="username" value="root"></property><property name="password" value="123456"></property></dataSource></environment></environments>

Configuration当中的对应属性:

public class Configuration {protected Environment environment;}

2:标签settings

mybatis-config.xml中的s标签:

     <settings>-- 应用二级缓存的一个内容。<setting name="cacheEnabled" value="true"/></settings>

Configuration当中的:

  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标签:

<typeAliases><typeAlias type="com.baizhiedu.entity.User" alias="User"/><typeAlias type="com.baizhiedu.entity.Account" alias="Account"/>
</typeAliases>

Configuration当中的:

  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 resource="UserDAOMapper.xml"/><mapper resource="AccountDAOMapper.xml"/></mappers>

Configuration当中的:

protected final Set<String> loadedResources = new HashSet<String>();

二:mapper.xml与Configuration属性的映射关系

Configuration当中的:

  protected final Map<String, MappedStatement> mappedStatements = new StrictMap<MappedStatement>("Mapped Statements collection");protected final Map<String, Cache> caches = new StrictMap<Cache>("Caches collection");protected final Map<String, ResultMap> resultMaps = new StrictMap<ResultMap>("Result Maps collection");protected final Map<String, ParameterMap> parameterMaps = new StrictMap<ParameterMap>("Parameter Maps collection");protected final Map<String, KeyGenerator> keyGenerators = new StrictMap<KeyGenerator>("Key Generators collection");

caches,parameterMaps,resultMaps,MapperdStatement,keyGenerators 这些是把Mapper.xml文件中的内容进行了封装。
resultMaps:所有的Mapper.xml文件中resultMap标签。
parameterMaps:是对sql标签上的parameterMap是属性做了处理。

上边这些属性都加了S都代表了是复数,也就是他的数量不只一个。这玩意存储的不是公共的,而是所有的。里边存储了对于所有的Mapper.xml文件中的这些属性都封装到这里边了。

这些不仅仅要存还要用,所以是将他们存入到了一个Map中,他是有key的,他的key就是namespace.id。所以你就发现这一组。这些对象封装到Configuration对象中之后都是采用的Map<String,xxx>这样的形式,key是namespace.id的形式。

三:Configuration对象可以创建操作类对象

new 就是创造,这里边创造了很多Mybatis核心的对象
这个Configuration类是整个Mabatis当中的核心类,把不仅仅是把Mybatis其他涉及到的核心对象也创建出来,不仅仅是上述存储类对象,其中就包括Excuter,StatementHanler,ResultHandler,ParamerHandler

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中的属性是Map<String,MappedStatement> mappedStatements 其中的String还是nameSpace.id
Configuration对象还包括:还有其他的结果集,参数,使用返回参数key(caches,parameterMaps,resultMaps,MapperdStatement,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 List<ResultMap> 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,以及他与我们的SQLSessions(Mybatis核心功能)是怎么交互的呀?我们后续再讲,操作类对象我们下篇文章在进行分析。

操作类对象大致有一下几种:

Excutor
StatementHandler
ParameterHandler
ResultSetHandler
TypeHandler

这些对象是Configuration对象进行创建的。有了操作类对象之后,我们基于上述存储类对象,我们就可以对数据库进行相应的操作了。

http://www.zhongyajixie.com/news/61306.html

相关文章:

  • 网站建设 实训网页制作软件免费版
  • iapp怎么把网站做软件注册网站流程和费用
  • wordpress 301重定向 www站长seo综合查询工具
  • wordpress文章列表分页安卓手机优化软件排名
  • 专业电商网站建设防控措施持续优化
  • 美国建设网站的条款b2b免费推广网站
  • 潍坊专升本考点搜索关键词排名优化技术
  • 神华公司两学一做网站接app推广的单子在哪接
  • 网站收录查询方法优化设计电子课本下载
  • 帮企业做网站赚钱百度北京总部电话
  • 澳洲同城app下载企业seo关键词优化
  • 凤阳做网站怎么找专业的营销团队
  • 360浏览器怎么加入可信站点淘宝产品关键词排名查询
  • 网络架构拓扑图seosem是指什么意思
  • 网站空间大小怎么查看成都关键词自然排名
  • 朋友做色情网站挣了好多钱seo排名哪家有名
  • 电子商务网站建设李洪心课后答案找关键词
  • 怎么做轮胎网站专业网站优化培训
  • 上海社区网站建设百度广告开户
  • 提供网站建设电话网上推广赚钱项目
  • 专线可以做网站seo快速工具
  • 网站设计专家淘宝站内推广方式有哪些
  • 深圳福田网站建设百度写作助手
  • 淘宝网站那个做的白城seo
  • 公司网站建设知乎站长之家最新域名查询
  • wap网站建设方案今日头条权重查询
  • 银川网站建设哪家好q群排名优化软件
  • 县政府网站建设管理工作制度百度引流推广哪家好
  • 闲鱼钓鱼网站怎么做寻找客户的渠道和方法
  • 注册公司有什么风险aso如何优化