package com.cesgroup.Interceptor;

import com.cesgroup.pojo.User;
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.Before;
import org.junit.Test;

import java.io.InputStream;

public class MybatisLogInterceptorTest {

    private SqlSession sqlSession=null;
    private SqlSessionFactory sqlSessionFactory;

    @Before
    public void configMybatis() {
        String resource = "mybatis-config.xml";
        InputStream inputStream;
        try {
            inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            sqlSession= sqlSessionFactory.openSession(false);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(sqlSession!=null) {
                sqlSession.close();
            }
        }
    }
    @Test
    public void testSelectUser() {
        User user = sqlSession.selectOne("UserMapper.selectUser", 2);
        System.out.println(user);
        sqlSession.close();
    }
}