package com.cesgroup.mybatiswork;

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

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;



/** 
* @Description: 测试类
* @Param:  
* @return:  
* @Author: Wanjianbin
* @Date: 2019/12/24 
*/
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(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.setName("万建斌");
//		user.setInfo(userInfObject);

        Map user = new HashMap();
        user.put("id", 99);
        user.put("name", "万建斌");
        user.put("info", userInfObject);

		int a=sqlSession.insert("MyMapper.insert",user);
		sqlSession.commit();
		System.out.println("影响的行数:"+a);	
	
	}
}
