package com.csnf;

import com.csnf.entity.Student;
import com.csnf.mapper.StudentMapper;
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;

public class StudentMapperTest {
    @Test
    public void test1Select() throws Exception {
        try {
            InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
            System.out.println(studentMapper.selectAll());

        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
    @Test
    public void test2Select() throws Exception {
        try {
            InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
            List<Student> students = studentMapper.selectByName("张三", "123456789");

        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
    @Test
    public void testInsert() throws Exception {
        try {
            InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
            Student student = new Student();
            student.setName("张三");
            student.setSex(1);
            student.setPhone("123456789");
            studentMapper.insert(student);
            sqlSession.commit();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
    @Test
    public void testUpdate() throws Exception {
        try {
            InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
            Student student = new Student();
            student.setName("张三");
            student.setSex(1);
            student.setPhone("123456789");
            int i = studentMapper.updateByPrimaryKey(student);
            sqlSession.commit();

        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

    @Test
    public void testDelete() throws Exception {
        try {
            InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
            studentMapper.deleteByPrimaryKey(1);

        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

}

