【Mybaits】2从JDBD的封装方面重新认识Mybaits

本文介绍了MyBatis框架如何通过SQLSession封装JDBC,包括其核心组件、Mapper接口、Configuration配置、映射文件以及Statement的不同类型。MyBatis通过这些组件简化数据库操作,支持面向对象的数据库访问。
摘要由CSDN通过智能技术生成

前言:SQLSession是对JDBC的封装

MyBatis 是一个 Java 持久化框架,它通过对 JDBC 的封装来简化数据库访问操作。核心的 SQLSession 对象是 MyBatis 的核心组件之一,负责管理数据库连接、执行 SQL 语句以及映射查询结果等功能。

具体来说,MyBatis 通过以下几个关键组件来实现对 JDBC 的封装:

1. SQLSession:
   SQLSession 是 MyBatis 提供的与数据库交互的主要接口。通过 SQLSession,可以执行 SQL 语句、提交事务、获取映射器(Mapper)等。在底层,SQLSession 使用 JDBC 的 Connection 对象来访问数据库。

SqlSession 是 MyBatis 中执行 SQL 增删改查操作的关键接口,提供了对数据库操作的方法,包括插入、更新、删除、查询等。在 MyBatis 中,SqlSession 通过 SqlSessionFactory 创建,并负责管理数据库连接以及执行 SQL 语句

2. Configuration:
   Configuration 是 MyBatis 的核心配置对象,它负责读取和解析 MyBatis 的配置文件,并构建相应的 SQLSession 和映射器(Mapper)等对象。Configuration 对象存储了包括数据库连接信息、映射器配置、类型处理器配置等在内的各种配置信息。

3. Mapper 接口和映射文件:
   MyBatis 使用 Mapper 接口和 XML 映射文件来定义与数据库的操作。Mapper 接口定义了与数据库相关的方法,映射文件定义了 Mapper 接口方法与 SQL 语句之间的映射关系。通过配置和使用映射文件,MyBatis 可以将 SQL 语句的执行结果映射到 Java 对象,并提供方便的查询语句编写和参数传递方式。

4. 映射器(Mapper):
   映射器是 MyBatis 的一个重要概念,负责定义与数据库交互的方法。MyBatis 根据 Mapper 接口的定义,动态生成对应的代理对象,实现了访问数据库的逻辑,包括执行 SQL 语句、参数传递、结果映射等。

通过以上组件的协作,MyBatis 封装了 JDBC 的底层细节,提供了更便捷和灵活的方式进行数据库操作。使用 MyBatis,可以通过定义 Mapper 接口和映射文件,以面向对象的方式进行数据库访问,而无需编写繁琐的 JDBC 代码。

一:SQLSession和JDBC的对照说明

Mybatis是对JDBC的封装,将JDBC封装成了一个核心的SQLSession对象
JDBC当中的核心对象:Connection、Statement、ResultSet

二:三种Statement补充说明


Statement:普通的Statement
PeparedStatement:预编译Statement
CallableStatement:适用于存储过程Statement

三:Statement的作用

相对于以上来讲,SQLSession是对JDBC的封装,SQLSessionFactory是创建SQLSession对象的工厂,我们还基于mybatis-config.xml配置Mybatis,并且在Mapper.xml当中配置SQL,


MyBatis当中的配置信息一共有两种:mybatis-config.xml和DaoMapper.xml。
其中mybatis-config.xml封装成了org.apache.ibatis.session.Configuration对象,DAOMapper.xml封装成了MapperdStatement部分数据是在Configuration当中进行保存的。
 

Configuration对象


Configuration是数据存储类对象,是将Mybatis当中的mybatis-config.xml封装成Configuration对象,Mapper.xml封装成了MappedStatement对象,当然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;

}

标签settings


mybatis-config.xml中的标签:

     <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;

标签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();


标签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的形式。

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所以,也是唯一,注定了在一个Mybatis当中会有N个MapperStatement对象。

这里边的statementType是什么意思,指的就是普通,预编译,存储过程。默认使用的就是preparedStatement,所以在我们的SQL标签上也肯定有这个属性,这个属性默认一定是prepared

四:MappedStatement和Configuration对象关系


MappedStatement当中可以找到Configuration对象,Configurantion对象可以找到MapperdStatement对象,他俩互相引用,双向关联,可以互相找到。

尾声

什么时候创建Configuration什么时候创建MappedStatement,以及他与我们的SQLSessions(Mybatis核心功能)是怎么交互的呀?我们后续再讲,操作类对象我们下篇文章在进行分析。

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

Excutor
StatementHandler
ParameterHandler
ResultSetHandler
TypeHandler

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值