package com.atguigu.day03_mybaits.test;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class Test {
public static void test1(){
///加载mybatis的配置文件(它也加载关联的映射文件)
String str="conf.xml";
InputStream is=Test.class.getClassLoader().getResourceAsStream(str);
//构建sqlSession的工厂
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(is);
SqlSession session=factory.openSession();
//映射sql的标识字符串,是在影射文件中找到namespace+“”+select中的id
String statement="com.atguigu.day03_mybaits.userMapper.getUser";
//执行查询返回一个唯一user对象的sql
User user=session.selectOne(statement, 1);
System.out.println(user);
}
public static void test2() throws IOException{
///加载mybatis的配置文件(它也加载关联的映射文件)
String resource = "conf.xml";
//加载mybatis的配置文件(它也加载关联的映射文件)
Reader reader = Resources.getResourceAsReader(resource);
//构建sqlSession的工厂
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(reader);
SqlSession session=factory.openSession();
//映射sql的标识字符串,是在影射文件中找到namespace+“”+select中的id
String statement="com.atguigu.day03_mybaits.userMapper.getUser";
//执行查询返回一个唯一user对象的sql
User user=session.selectOne(statement, 2);
System.out.println(user);
}
public static void main(String[] args) throws IOException {
test1();
test2();
}
}