package com;

import cn.hutool.core.io.IoUtil;
import cn.hutool.json.JSONObject;
import com.cesgroup.mybatisHomework.entity.Person;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.ExecutorType;
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;
import java.util.ArrayList;
import java.util.List;

public class TestOne {
  private SqlSessionFactory sqlSessionFactory = null;
  private SqlSession sqlSession = null;

  @Before
  public void configMybatis() {
    // 指定全局配置文件
    String resource = "mybatis-config.xml";
    // 读取配置文件
    InputStream inputStream = null;
    try {
      inputStream = Resources.getResourceAsStream(resource);
      sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
      sqlSession = sqlSessionFactory.openSession(false);
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      IoUtil.close(inputStream);
    }
  }

  /**
   * TypeHandler查询
   */
  @Test
  public void selectTest() {
    Person person = sqlSession.selectOne("MyMapper.selectPerson", 2);
    System.out.println(person);
    sqlSession.close();
  }

  /**
   * TypeHandler新增
   */
  @Test
  public void insertTest() {
    Person person = new Person();
    person.setId("aaa");
    person.setName("张三");
    JSONObject userInfObject = new JSONObject();
    userInfObject.put("sex", "男");
    userInfObject.put("birthday", "1999-12-12");
    person.setInfo(userInfObject);
    int a = sqlSession.insert("MyMapper.insert", person);
    sqlSession.commit();
    System.out.println("影响的行数:" + a);

  }

  /**
   * 批量插入1
   */
  @Test
  public void batchInsert1() {
    long begin = System.currentTimeMillis();
    List<Person> list = new ArrayList<>();
    for (int i = 1; i < 10000; i++) {
      Person person = new Person();
      person.setId("a" + i);
      person.setName("李四" + i);
      JSONObject userInfObject = new JSONObject();
      userInfObject.put("sex", "男");
      userInfObject.put("birthday", "1999-12-12");
      person.setInfo(userInfObject);
      list.add(person);
    }
    sqlSession.insert("MyMapper.insertBatch", list);
    sqlSession.commit();
    long end = System.currentTimeMillis();
    System.out.println("##############耗时：" + (end - begin) + " 毫秒");
  }

  /**
   * 批量插入2
   */
  @Test
  public void batchInsert2() {
    sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
    long begin = System.currentTimeMillis();
    for (int i = 1; i < 10000; i++) {
      Person person = new Person();
      person.setId("b" + i);
      person.setName("王五" + i);
      JSONObject userInfObject = new JSONObject();
      userInfObject.put("sex", "男");
      userInfObject.put("birthday", "1999-12-12");
      person.setInfo(userInfObject);
      int a = sqlSession.insert("MyMapper.insert", person);
    }
    sqlSession.commit();
    long end = System.currentTimeMillis();
    System.out.println("##############耗时：" + (end - begin) + " 毫秒");
  }

}
