mybatis源码学习整理

1. 整体类图

mybatis源码学习整理_第1张图片
mybatis整体关键类图

2. 整体流程

2.1 代码

2.1.1 main类

public static void main(String[] args) {
        String resource = "mybatis-config.xml";
        InputStream inputStream =  Resources.getResourceAsStream(resource);
        SqlSession sqlSession  = new SqlSessionFactoryBuilder().build(inputStream);
        RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class);
        Role role = new Role();
        role.setRoleName("testName");
        role.setNote("testNote");
        roleMapper.insertRole(role);
        roleMapper.deleteRole(1L);
        sqlSession.commit();
    }

2.1.2 RoleMapper接口

public interface RoleMapper {
    public Role getRole(Long id);
    public int deleteRole(Long id);
    public int insertRole(Role role);
}

2.1.3 Role类(省去getter和setter方法)

public class Role {
    private Long id;
    private String roleName;
    private String note;
  // 省去getter和setter方法
}

2.1.4 mybatis-config.xml




    
        
    
    
        
            
                
            
            
                
                
                
                
            
        
    
    
        
    

2.1.5 roleMapper.xml




    
    
        insert into t_role(role_name,note) values (#{roleName},#{note})
    
    
        delete from t_role where id = #{id}
    

2.2 构建sqlSession和getMapper方法对应的流程

mybatis源码学习整理_第2张图片
构建sqlSession和getMapper方法对应的流程

2.3 对应Mapper接口中的方法执行流程

mybatis源码学习整理_第3张图片
对应Mapper接口中方法执行流程

参考

mybatis源码:https://github.com/mybatis/mybatis-3.git

你可能感兴趣的:(mybatis源码学习整理)