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;



/**
 * 
 * @author Zero
 *
 */
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();
			}  
        }
	}
	
	@Test
	public void testSelectUser() {
		User user = sqlSession.selectOne("User.select", 2);
        System.out.println(user);
        sqlSession.close();
	}
	
	@Test
	public void testInsertJsonObject() {
		
		JSONObject userInfObject = new JSONObject();
		userInfObject.put("hobby", "音乐");
		userInfObject.put("speciality", "篮球");

        Map user = new HashMap();
        user.put("id", 11);
        user.put("name", "测试");
        user.put("info", userInfObject);

		int a=sqlSession.insert("User.insert",user);
		sqlSession.commit();
	}
}
