package com.sf.mybatisTest;


import com.sf.entity.Tea;
import com.sf.mapper.TeaMapper;
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;
import java.util.List;

/**
 * @author WanJl
 * @version 1.0
 * @title TeaMapperTest
 * @description
 * @create 2025/10/12
 */
public class TeaMapperTest {
    @Test
    public void testSelectAllTea() throws IOException {
        //1、加载mybatis的核心配置文件mybatis-config.xml，并且生成数据流
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        //2、使用SqlSessionFactory的构建器通过【流】获取SqlSessionFactory
        SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
        //3、获取SqlSession对象 SQL会话对象
        SqlSession sqlSession=sessionFactory.openSession();
        //4、通过会话对象获取Mapper接口的代理对象。实际上是mybatis通过TeaMapper接口帮我们创建了一个TeaMapper的子类对象(代理对象)。
        TeaMapper teaMapper = sqlSession.getMapper(TeaMapper.class);
        //5、执行teaMapper里面的方法，比如查询所有茶叶信息，比如新增茶叶信息，比如修改茶叶信息
        List<Tea> teaList = teaMapper.selectAll();
        //输出结果
        //System.out.println(teaList);
        for (Tea tea : teaList) {
            System.out.println(tea);
        }
        //6、释放资源
        sqlSession.close();
    }
}
