mybatis整体流程


DOCTYPE configuration
 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-config.dtd">
 
 
 

    
    
    
    
    
    
    
        
    
    
    
        
        
        
        <package name="com.atguigu.bean"/>
    
    
    
    
    default="mysql">
        
        
            
            
            
            
                
                
                
                
            
        
        
        
            
            
                
                
                
                
            
        
    
    
    
    
        
    

 


DOCTYPE mapper
 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 
 

    
    
    
    

 

package com.atguigu.mapper;

import com.atguigu.bean.User;

public interface UserMapper {
    //和UserMapper.xml中的sql的id对应的
    /**
     * 
    
    
     * @param uid
     * @return
     */
    User getUserByUid(String uid);
    
}

 

package com.atguigu.mapper;

import static org.junit.Assert.*;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

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 com.atguigu.bean.User;

public class TestMybatis {

    @Test
    public void test() throws IOException {
        
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");//org.apache.ibatis.io.Resources
        //InputStream is = TestMybatis.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = 
                new SqlSessionFactoryBuilder().build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //getMapper():会通过动态代理动态生成UserMapper的代理实现类
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        System.out.println(mapper.getClass().getName());//com.sun.proxy.$Proxy4这个为生成的代理对象
        User user = mapper.getUserByUid("1");
        System.out.println(user);
        //user_name ---> userName
    }
    
}

mybatis整体流程_第1张图片

你可能感兴趣的:(mybatis整体流程)