package com.lc.mybatis.test;

import com.lc.mybatis.entity.User;
import com.lc.mybatis.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;

public class TestUserResultMap {




    @Test
    public void test01() throws IOException {
        //构建sqlSessionfactory
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory  = new SqlSessionFactoryBuilder().build(is);
        //获取 SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //获取接口  jdk动态代理 UserMapper 代理类 反射技术
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        User user = mapper.getUserById(3);

        System.out.println("用户信息："+user);

        //释放
        sqlSession.close();
    }



    @Test
    public void test02() throws IOException {
        //构建sqlSessionfactory
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory  = new SqlSessionFactoryBuilder().build(is);
        //获取 SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //获取接口  jdk动态代理 UserMapper 代理类 反射技术
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        User user = mapper.getUserByIdStep(3);

        System.out.println("用户信息："+user);

        //释放
        sqlSession.close();
    }


    @Test
    public void test03() throws IOException {
        //构建sqlSessionfactory
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory  = new SqlSessionFactoryBuilder().build(is);
        //获取 SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //获取接口  jdk动态代理 UserMapper 代理类 反射技术
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        User user = mapper.getUserByIdStep(3);

        System.out.println("用户名："+user.getUsername());

        System.out.println("==================");

        System.out.println("部门名："+user.getDept().getDname());
        //释放
        sqlSession.close();
    }
}
