Mybatis 快速入门(XML方式) 底层源码对象初始化过程分析

本文介绍了Mybatis的基础知识,包括SqlMapConfig.xml配置、SqlSessionFactory创建过程、SqlSession的使用以及Executor执行器。详细解析了如何通过XML配置文件和SqlSessionFactory构建会话工厂,进而执行数据库操作。还涵盖了Mapper接口和XML映射文件的关联,以及Mybatis的 CRUD 操作。同时,文章深入探讨了源码层面的构建者模式和XPath在解析过程中的应用。
摘要由CSDN通过智能技术生成

导读

官网地址

https://mybatis.org/mybatis-3/zh/index.html

架构原理图

说明

mybatis配置文件

  1. SqlMapConfig.xml,此文件为mybatis的全局配置文件,配置了mybatis的运行环境等信息
  2. XXXMapper.xml,此文件作为mybatis的sql映射文件,文件中配置了操作数据库的CRUD语句。需要在SqlMapConfig.xml中加载

SqlSessionFactory

  1. 通过mybatis环境等配置信息构造SqlSessionFactory,既会话工厂

***跟底层源码查看创建SqlSessionFactory流程***

注:底层如何获取标签值,请自行研究(剧透:for循环遍历XML获取标签中的值,然后放入Map)!~

SqlSession

  1. 通过会员工厂创建SqlSession即会话,程序通过SqlSession会话接口对数据库进行CRUD操作。

Executor执行器

  mybatis底层自定义了Executor执行器接口来具体操作数据库,Executor接口有两个实现,一个是基本执行器(默认),一个缓存执行器,SqlSession底层是通过executor接口操作数据库

Mapped Statement

  他是mybatis一个底层封装对象,包装了mybatis配置信息及XXXMapper.xml映射文件等。XXXMapper.xml文件中一个个select/insert/update/delete标签对应一个Mapped Statement对象

原始JDBC代码

  原始JDBC和mybatis操作数据库数据,与上面架构图流程相对应。

public class JDBCTest {

    public static void main(String[] args) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            // 加载数据库驱动
            Class.forName("com.mysql.jdbc.Driver");

            // 通过驱动管理类获取数据库链接connection = DriverManager
            connection = DriverManager.getConnection(
                              "jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8",
                             "root", 
                              "root"
                              );

            // 定义sql语句 ?表示占位符
            String sql = "select * from user where username = ?";
            // 获取预处理 statement
            preparedStatement = connection.prepareStatement(sql);
            
            // 设置参数,第一个参数为 sql 语句中参数的序号(从 1 开始),第二个参数为设置的
            preparedStatement.setString(1, "王五");
            // 向数据库发出 sql 执行查询,查询出结果集
            resultSet = preparedStatement.executeQuery();
            // 遍历查询结果集
            while (resultSet.next()) {
                System.out.println(
                                  resultSet.getString("id") 
                                  + " " + 
                                  resultSet.getString("username")
                     );
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 释放资源
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block e.printStackTrace();
                }
            }
        }
    }
}

Mybatis 入门基础

表结构

 表数据

Mybatis环境搭建 

添加依赖

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.cyb</groupId>
    <artifactId>mybatis</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>mybatis Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <!-- mybatis依赖 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>
        <!-- mysql依赖 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.20</version>
        </dependency>

        <!-- 单元测试 -->
        <dependency>
            <grou
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苗力

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值