MyBatis源码解析(一):从一个简单的MyBatis查询开始

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u011976388/article/details/88903126

一,简单的MyBatis用例

    1,Maven依赖引入

 <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.2</version>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.6</version>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.16.22</version>
    </dependency>
  </dependencies>

    2,MyBatis核心配置文件 -- mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<!-- 注意填写自己的mysql用户名和密码, 还有连接的url要加上"?serverTimezone=GMT%2B8"不然会报错 -->
<configuration>

    <properties resource="jdbc.properties" />
    
    <settings>
        <setting name="logImpl" value="LOG4J"/>
    </settings>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}" />
                <property name="url" value="${jdbc.url}" />
                <property name="username" value="${jdbc.username}" />
                <property name="password" value="${jdbc.password}" />
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="mapper/UserMapper.xml" />
    </mappers>

</configuration>

    3,Mapper.java

package com.gupao.mapper;

import com.gupao.domain.UserVO;
import org.apache.ibatis.annotations.Param;

/**
 * @author pj_zhang
 * @create 2019-03-25 22:35
 **/
public interface UserMapper {

    UserVO selectUserById(@Param("id") Integer id);

}

    4,Mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.gupao.mapper.UserMapper">

    <select id="selectUserById" resultType="com.gupao.domain.UserVO">
      SELECT
        ID AS id,
        USERNAME AS username,
        PASSWORD AS password,
        AGE AS age
      FROM
        USER_T
      WHERE
        ID = #{id, jdbcType=NUMERIC}
    </select>

</mapper>

    5,MyBatis直连代码

package com.gupao.test;

import com.gupao.domain.UserVO;
import com.gupao.mapper.UserMapper;
import lombok.extern.log4j.Log4j;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author pj_zhang
 * @create 2019-03-25 22:39
 **/
@Log4j
public class MyBatisTest {

    public static void main(String[] args) throws IOException {
        // mybatis基础配置文件路径及文件名
        String resource = "mybatis-config.xml";
        // 获取配置文件输入流
        InputStream inputStream = Resources.getResourceAsStream(resource);
        // 通过文件输入流进行XML文件解析, 添加配置信息到Configuration
        // Configuration.addMapper()通过调用
        // 直接返回DefaultSqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        // openSession, 获取DefaultSqlSession
        // 在内部生成Executor, 并同Configuration一同注入到DefaultSqlSession中
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try {
            // 根据Mapper名称从SqlSession中获取Mapper代理对象
            // 内部通过Configuration.mapperRegistry.knownMappers调用链获取Mapper对象
            // 并通过动态代理获取该Mapper对象的代理对象, MapperProxy
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            // 通过动态代理invoke()调用
            // 底层通过Executor.query()执行SQL语句获取结果
            // ORM映射通过ResultSetHandler完成
            UserVO userVO = mapper.selectUserById(1);
            System.out.println(userVO);
        } catch (Exception e) {
           log.info(e);
        }
    }

}

    6,各阶段相关注释在代码中已经注释完成,源码分段解析会根据执行顺序分模块进行大概解析

    7,源码解析只会了解MyBatis底层实现原理和流程,说明问题即可,不追求每一个方法都进行详细分析

二,MyBatis源码分段解析

    * 初始化配置文件,并获取SqlSessionFactory

        链接:https://blog.csdn.net/u011976388/article/details/88903330

    * 通过SqlSessionFactory获取SqlSession

        链接:https://blog.csdn.net/u011976388/article/details/88904251

    * 通过SqlSession获取Dao层接口的执行Mapper

        链接:https://blog.csdn.net/u011976388/article/details/88907538

    * 通过Mapper执行Statement

        链接:https://blog.csdn.net/u011976388/article/details/88919193

    * 通过ResultSetHandler实现ORM映射

        链接:https://blog.csdn.net/u011976388/article/details/88922016

    * 手写简易MyBatis

        链接:https://blog.csdn.net/u011976388/article/details/88927897

展开阅读全文

没有更多推荐了,返回首页