Mybatis笔记

mybatis用了有一段时间了,也看过几次源码,但好记性不如烂笔头,这次准备记录一下源码分析过程。
这里准备根据mybatis的helloWord程序,以调试模式跟踪源码进行分析

1.准备工作

1.1新建maven工程,引入相关依赖



    4.0.0

    com.pww
    mybatis-source
    0.0.1-SNAPSHOT
    
        
            junit
            junit
            4.12
        

        
            org.mybatis
            mybatis
            3.5.1
        
        
            mysql
            mysql-connector-java
            5.1.47
        
    

1.2创建mybatis配置文件




    
        
            
            
                
                
                
                
            
        
    
    
    
        
    

1.3映射文件




    

1.4映射接口

package mapper;

import bean.User;

/**
 * Created by bitsino_pww on 2019/8/7.
 */
public interface UserMapper {
    User getUserInfoByID(Long id);
}

1.5实体类

package bean;

/**
 * Created by bitsino_pww on 2019/8/7.
 */
public class User {
    private Long id;
    private String username;
    private String password;
    private Integer gender;
    private Integer age;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getGender() {
        return gender;
    }

    public void setGender(Integer gender) {
        this.gender = gender;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", gender=" + gender +
                ", age=" + age +
                '}';
    }
}

1.6测试代码

import bean.User;
import mapper.UserMapper;
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 org.junit.Test;

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

/**
 * Created by bitsino_pww on 2019/8/8.
 */
public class MybatisSourceTest {
    @Test
    public void testMybatisSource() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.getUserInfoByID(1L);
        System.out.println(user);
        //查询结果 User{id=1, username='樱木花道', password='123', gender=1, age=22}
    }
}

1.7工程结构

image.png

1.8数据库一张表

image.png

2.开始跟踪代码

2.1首先是根据mybatis的配置文件的配置,进行SqlSessionFactory的创建

image.png

2.2进入SqlSessionFactoryBuilder

image.png

2.3看看configuration对象会将我们的配置文件,映射文件的信息都进行保存

image.png

2.4初始化SqlSessionFactory小结

image.png

2.5SqlSessionFactory创建完成后,就可以通过openSession()创建与数据库的连接(SqlSession)

image.png

image.png

2.7创建sqlSession小结

image.png

2.8接下来会通过 sqlSession.getMapper(UserMapper.class)创建UserMapper的代理实现对象

时序图

image.png

image.png

image.png

2.9跟踪代码,看如何实现查询,最终是通过Executor调用StatementHandler的query方法,传入prepareStatement和ResultHandler参数对象实现的查询,得到结果,到此mybatis的四大对象我们都见到了

image.png

2.10查询过程总结

image.png

你可能感兴趣的:(Mybatis笔记)