import com.mapper.AccountMapper;
import com.mapper.UserMapper;
import com.pojo.dto.AccountVO;
import com.pojo.entity.AccountRealEntity;
import com.pojo.entity.UserEntity;
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.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 动态sql查询
 */

public class Test3 {
    UserMapper userMapper;
    UserMapper userMapper2;
    AccountMapper accountMapper;
    SqlSession sqlSession;
    SqlSession sqlSession2;
    @Before
    public void before() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("mybatisConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        sqlSession = build.openSession(true);
        sqlSession2 = build.openSession(true);
        userMapper = sqlSession.getMapper(UserMapper.class);
        accountMapper = sqlSession.getMapper(AccountMapper.class);
    }
    @Test
    public void testCondition()  {
        UserEntity userEntity = new UserEntity();
//        userEntity.setId(1);
        userEntity.setName("name");
        userEntity.setAge(13);
        userEntity.setAddress("address");
        List<UserEntity> userListByCondition = userMapper.findUserListByCondition(userEntity);
    }
    @Test
    public void testForEach() {
        ArrayList<Integer> integers = new ArrayList<>();
//        integers.add(1);
//        integers.add(2);
        List<UserEntity> userForEach = userMapper.findUserForEach(integers);
    }

    /**
     * 测试一级缓存
     */
    @Test
    public void testCache() {
        userMapper2 = sqlSession.getMapper(UserMapper.class);
        ArrayList<Integer> integers = new ArrayList<>();
//        integers.add(1);
//        integers.add(2);
        List<UserEntity> userForEach1 = userMapper.findUserForEach(integers);
        List<UserEntity> userForEach2 = userMapper2.findUserForEach(integers);
    }

    /**
     * 二级缓存 sqlSessionFactory级别
     */
    @Test
    public void testTwoCache() {
        ArrayList<Integer> integers = new ArrayList<>();
        UserMapper mapper1 = sqlSession.getMapper(UserMapper.class);
        UserMapper mapper2 = sqlSession2.getMapper(UserMapper.class);
        List<UserEntity> userForEach1 = mapper1.findUserForEach(integers);
        sqlSession.close();
        List<UserEntity> userForEach2 = mapper2.findUserForEach(integers);
        accountMapper = sqlSession2.getMapper(AccountMapper.class);
        AccountVO vo = accountMapper.findVO(16);

    }
    @Test
    public void testFindStep() {
        AccountVO byStep = accountMapper.findByStep(16);
        System.out.println(byStep.getUserName());
        return;
    }
    @Test
    public void testAllMap() {
        Map<Integer, AccountRealEntity> allMap = accountMapper.findAllMap();

    }


    @Test
    public void delete() {
        userMapper.deleteOne(2);
    }
    @After
    public void after() {
        sqlSession.close();
    }

}
