import com.mapper.AccountMapper;
import com.pojo.entity.AccountEntity;
import com.pojo.entity.AccountRealEntity;
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.Test;

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

public class Test1 {

    @Test
    public void test() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("mybatisConfig.xml");
        SqlSessionFactory sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactoryBuilder.openSession(true);
        // 查询
    //    List<AccountEntity> objects = sqlSession.selectList("com.mapper.AccountMapper.findAll");
        // 插入
//        AccountEntity accountEntity = new AccountEntity();
//        accountEntity.setUser_name("这");
//        accountEntity.setBalance(70.4);
//        sqlSession.insert("com.mapper.AccountMapper.insertOne",accountEntity);
        AccountEntity updateEntity = new AccountEntity();
        updateEntity.setId(9);
        updateEntity.setUser_name("我是12");
        updateEntity.setBalance(100.0);
         // namespace + functionName
        sqlSession.update("com.mapper.AccountMapper.updateOne",updateEntity);
        // mybatis 如果执行了更新操作需要提交事务
//        sqlSession.commit();
        sqlSession.close();
    }
    // 使用代理的模式
    @Test
    public void useProxy() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("mybatisConfig.xml");
        SqlSessionFactory sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactoryBuilder.openSession(true);
        AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
        List<AccountEntity> all = mapper.findAll(9);
    }
    // 测试resultMap
    @Test
    public void testResultMap() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("mybatisConfig.xml");
        SqlSessionFactory sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactoryBuilder.openSession(true);
        AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
//        AccountRealEntity byResultMap = mapper.findByResultMap();
    }
}
