package com.cesgroup.mybatiswork;

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

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 com.alibaba.fastjson.JSONObject;
import com.cesgroup.mybatiswork.entity.User;



/**
 * @author chuwanshun
 *
 */
public class AppTest 
{	
	
	private SqlSession sqlSession=null;
	private SqlSessionFactory sqlSessionFactory;
	@Before
    public void configMybatis() {
    	 // 指定全局配置文件
        String resource = "mybatis-config.xml";
        // 读取配置文件
        InputStream inputStream;
       
		try {
			inputStream = Resources.getResourceAsStream(resource);
	
        // 构建sqlSessionFactory
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        // 获取sqlSession
        // sqlSession= sqlSessionFactory.openSession(ExecutorType.BATCH,false); 
        sqlSession= sqlSessionFactory.openSession(false);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(sqlSession!=null) {
				//sqlSession.close();
			}  
        }
	}
	
	/**
	 * 
	 * 自定义解析器查询测试
	 * 
	 * json串字段为  info
	 */
	@Test
	public void testSelectUser() {	
		User user = sqlSession.selectOne("MyMapper.selectUser", 2);
        System.out.println(user);
        sqlSession.close();
	}
	/**
	 * 
	 * 自定义类型解析器插入测试
	 */
	@Test
	public void testInsertJsonObject() {
		
		JSONObject userInfObject = new JSONObject();
		userInfObject.put("address", "上海市静安区");
		userInfObject.put("telphone", "13823541230");
		User user = new User();
		user.setId(3);
		user.setName("万建斌");
		user.setInfo(userInfObject);
		int a=sqlSession.insert("MyMapper.insert",user);
		sqlSession.commit();
		System.out.println("影响的行数:"+a);	
	
	}
	
	/**
	 * 批量插入方法一（经测试该方法较快）
	 * 通过list方式批量插入
	 * 500条 378ms
	 * 5000条615ms
	 * 10000条897ms
	 */
	@Test
	public void testInsertBatch() {
		long start = System.currentTimeMillis();
	    List<User> list = new ArrayList<>();
		for (int i = 1; i < 10000; i++) {
			JSONObject userInfObject = new JSONObject();
			userInfObject.put("address", "上海市静安区");
			userInfObject.put("telphone", "13823541230");
			User user = new User();
			user.setId(i);
			user.setName("万建斌"+i);
			user.setInfo(userInfObject);
			list.add(user);
		}
		
		int a=sqlSession.insert("MyMapper.insertBatch",list);
		sqlSession.commit();
		long end = System.currentTimeMillis();
	    System.out.println("---------------" + "批量插入耗时一："+(end - start) + "---------------");
	}
	
	/**
	 * 批量插入方法二
	 * 通过改变executor类型
	 *   
	 *  500条 约375ms
	 *  5000条约912ms
	 *  10000条 1527ms
	 */
	@Test
	public void testInsertBatchTwo() {
		sqlSession= sqlSessionFactory.openSession(ExecutorType.BATCH,false);
		
		long start = System.currentTimeMillis();
		for (int i = 1; i < 10000; i++) {
			JSONObject userInfObject = new JSONObject();
			userInfObject.put("address", "上海市静安区");
			userInfObject.put("telphone", "13823541230");
			User user = new User();
			user.setId(i);
			user.setName("万建斌"+i);
			user.setInfo(userInfObject);
			int a=sqlSession.insert("MyMapper.insert",user);
		}
		sqlSession.commit();
		long end = System.currentTimeMillis();
	    System.out.println("---------------" + "批量插入耗时二："+(end - start) + "---------------");
	}
	
}
