• 《通用源码阅读指导书-MyBatis源码详解》笔记一(MyBatis概述、使用、源码大体执行流程、源码大体结构)


    《通用源码阅读指导书-MyBatis源码详解》笔记一(MyBatis概述、使用、源码大体执行流程、源码大体结构)

    前言

    本篇文章主要是看了书籍如下几个章节后进行的笔记总结:

    • 第1章-源码阅读

    • 第2章-MyBatis概述

    • 第3章-MyBatis运行初探

    • 第4章-MyBatis源码结构概述
      斗图-开整

    核心问题点

    1. 为什么需要看优秀的开源源码???【⭐️⭐️⭐️⭐️】
    2. 有哪些优秀的开源项目值得去研究???【⭐️⭐️】
    3. 如何使用传统JDBC的方式访问数据库???有哪几个核心步骤???【⭐️⭐️⭐️⭐️⭐️】
    4. ORM是啥???常用的ORM框架有哪些???【⭐️⭐️⭐️】
    5. 使用MyBatis进行项目开发的步骤有哪些???【⭐️⭐️⭐️⭐️⭐️】
    6. 对比原生的JDBC的方式和MyBatis的方式,MyBatis有哪些优点???【⭐️⭐️⭐️】
    7. 从源码角度梳理MyBatis整体执行流程???【⭐️⭐️⭐️⭐️⭐️】
    8. MyBatis源码包的大体结构是啥???有哪些核心的功能包???【⭐️⭐️⭐️】

    问题答案剖析

    为什么需要看优秀的开源源码???【⭐️⭐️⭐️】

    额,对于这个问题我能说看源码是为了面试吗???emmmmm,当然不能的。
    在这里插入图片描述

    • 阅读源码是一个优秀软件开发者必备的能力

      很多时候我们接手一个前辈遗留项目的时候,这个时候千万不能慌,打开idea开始看代码,看有没有类似Thread.sleep的核心代码,如果有的话,那么我们就可以通过注释的方式优化系统。所以,熟悉一个项目就需要去看源码,了解整体的执行逻辑设计思想,这样才能做到心中有数,是优化系统的基础。当然,自认为写的不错的旧代码也需要时常温习一下。

    • 阅读源码可以学习大佬们的设计思想

      平时很多小伙伴可能和我一样,整天CRUD,看了一堆的设计模式,就是不能很滑溜地用到我们的项目中;这个时候就可以看大佬的源码,看看他们是如何对一个功能进行设计的,用到了什么设计模式,然后能不能借鉴到自己的项目中去。

    • 能发现自己的知识盲区,完善知识储备

      很多时候,源码就是一本教材,里面有很多的知识点值得我们去挖掘,去学习。经过自己的深挖,可能会发现一片蓝天,正所谓“知道的越多,不知道的越多”

    • 能快速定位BUG

      当对一个框架、项目、系统完全熟悉之后,代码就像是自己写的一样,对于遇到的问题可以快速定位到问题

    • 二次开发需要

      有些时候某些框架不能满足公司的个性化需求,这个时候就需要对框架进行二次开发,二次开发的前提就是,你需要对框架的核心模块很熟悉,不然真的就是盲人摸象了。

    有哪些优秀的开源项目值得去研究???【⭐️⭐️】

    在书中,作者也提了一些,比如:

    • apache/dubbo:一个高性能的RPC框架
    • netty/netty:网络IO框架
    • Spring-projects/spring-boot:开箱即用的Spring框架
    • alibaba/fastjson:用于JSON解析等
    • apache/kafka:一套实时数据流处理平台
    • Mybatis/mybatis-3:一套强大的ORM框架,当然,整本书的主角就是它

    其实,还有很多其他优秀的开源框架,比如RocketMQ、MyBatis-Plus、Nacos等,今天把话撂这了,上面提到的框架源码,后面都盘它一遍。

    在这里插入图片描述

    算了,感觉一次性确实盘不完,所以,我决定了,以后还是围绕MyBatis、Spring、SpringMVC、SpringBoot源码展开,不然车开太快会翻车的。

    使用JDBC的方式如何访问数据库,有哪几个核心步骤???【⭐️⭐️⭐️⭐️⭐️】

    在这里插入图片描述

    public class UserDao {
       
      private static final String driver = "com.mysql.cj.jdbc.Driver";
      private static final String url = "jdbc:mysql://localhost:3306/test_mybatis?allowPublicKeyRetrieval=true";
      private static final String userName = "root";
      private static final String password = "12345678";
    
      public static Connection getConnection() {
       
        Connection connection = null;
        try {
       
          Class.forName(driver);// 1.注册驱动
          connection = DriverManager.getConnection(url, userName, password);// 2.获取连接对象
        } catch (Exception e) {
       
          e.printStackTrace();
        }
        return connection;
      }
    
    
      // 测试查询user列表
      public static void testSelectUser() {
       
        Connection connection = getConnection();// 获取connection对象
        if (connection == null) {
       
          System.err.println("获取connection对象出现异常");
          return;
        }
        Statement statement = null;
        try {
       
          statement = connection.createStatement();// 3.创建statement对象
          String sql = "select * from user";
          ResultSet resultSet = statement.executeQuery(sql);// 4.使用statement对象执行SQL语句
          List<User> users = new ArrayList<>();
          while (resultSet.next()) {
       // 5.处理resultSet结果集
            User user = new User();
            user.setId(resultSet.getInt("id"));
            user.setName(resultSet.getString("name"));
            user.setAge(resultSet.getInt("age"));
            users.add(user);
          }
          System.out.println(users);// 打印处理结果
        } catch (Exception e) {
       
          e.printStackTrace();
        } finally {
       
          if (statement != null) {
       
            try {
       
              statement.close();// 6.关闭资源
            } catch (SQLException e) {
       
              e.printStackTrace();
            }
          }
          try {
       
            connection.close();// 7.关闭资源
          } catch (SQLException e) {
       
            e.printStackTrace();
          }
        }
      }
      
      // 修改操作
      public static void updateUserByUser(User user) {
       
        Connection connection = getConnection();
        if (connection == null) {
       
          System.err.println("获取connection对象出现异常");
          return;
        }
        String sql = "update user set age = ?,name = ? where id = ?";
        PreparedStatement statement = null;
        try {
       
          statement = connection.prepareStatement(sql);// 创建statement
          statement.setInt(1, user.getAge());// 设置参数
          statement.setString(2, user.getName());
          statement.setInt(3, user.getId());
          statement.executeUpdate();// 执行sql
        } catch (Exception e) {
       
          e.printStackTrace();
        } finally {
       
          if (statement != null) {
       
            try {
       
              statement.close();
            } catch (SQLException e) {
       
              e.printStackTrace();
            }
          }
          try {
       
            connection.close();
          } catch (SQLException e) {
       
            e.printStackTrace();
          }
        }
      }
    
      public static void main(String[] args) {
       
        testSelectUser();
      }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121

    查询结果为:
    在这里插入图片描述

    总结为如下核心步骤:

    1. 加载驱动程序
    2. 获得数据库connection对象
    3. 创建SQL语句
    4. 创建statement对象【如果是prepareStatement还需要进行参数设置】
    5. 使用statement对象执行SQL语句
    6. 处理resultSet集合

    从上面可以看出,第4、5、6步是非常麻烦的,只能针对不同对象的不同操作拼装不同的操作语句然后单独处理返回的结果。一般的项目中对数据库的读写操作是非常频繁的,所以就会产生大量的工作,这个这也是MyBatis重点优化的部分。

    ORM是啥???常用的ORM框架有哪些???【⭐️⭐️⭐️】

    ORM简称Object Relational Mapping,也就是对象关系映射,下面这张图画的很直接:

    在这里插入图片描述

    就比如当前我有一个User对象,对象的字段为:

    public class User {
       
      private Integer id;
      private String userName;.// 驼峰命名
      private Integer userAge;// 驼峰命名
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    有一个User表,表中的字段为:

    CREATE TABLE `user` (
      `id` int NOT NULL AUTO_INCREMENT,
      `user_name` char(255) COLLATE utf8_bin DEFAULT NULL,
      `user_age` int DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=42 DEFAULT CHARSET=utf8mb3 COLLATE=utf8_bin;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    可以看到,user_name、user_age与对象User中的userName、userAge不能一一对应上,所以就使用ORM框架进行自动映射,常见的ORM框架有如下几个:Hibernate、MyBatis、JFinal等

    使用MyBatis进行项目开发的步骤有哪些???【⭐️⭐️⭐️⭐️⭐️】

    创建一个实体对象User:

    public class User {
       
      private Integer id;
      private String name;
      private Integer age;
      省略get、set、toString方法
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    创建一个UserMapper接口:

    public interface UserMapper {
       
      public User getUserByName(String name);
      @Select("select * from user where id = #{userId}")
      public User getUserById(@Param("userId") Integer userId);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在resources文件下创建一个mapper文件夹,并创建UserMapper.xml:

  • 相关阅读:
    王学岗音视频开发(一)—————配置NDK开发环境
    专业硕士招生占比将达到三分之二,那么跟学术硕士有哪些区别?
    【单目3D目标检测】FCOS3D + PGD论文解析与代码复现
    Excel·VBA工作表导出为图片
    6.fs模块的使用
    有序管理SSH Keys,爆击Permission denied
    Rust trait、动态派发和向上转型
    Redis缓存的连环炮面试题
    Stored Procedure && Trigger
    深度学习--全连接层、高阶应用、GPU加速
  • 原文地址:https://blog.csdn.net/qq_41919300/article/details/127695166