public class MyBatisTest {
/**
* mybatis执行流程
* 1.获取配置文件
* 2.通过SqlSessionFactoryBuilder().build()方法
* 解析配置文件并保存到SqlSessionFactory.Configuration中, 对应的xml的sql,mapper保存到Configuration对象中
* 3.通过SqlSessionFactory.openSession()方法
* 创建Executor执行器并返回DefaultSqlSession对象
* 1基本类型 SIMPLE/RESULT/BATCH,)
* 2二级缓存包装
* 3拦截器包装
* 4.通过 getMapper()方法,根据JDK动态代理返回MapperProxy对象
* 5.执行mapper.selectById(1)方法
* 代理对象调用execute()进行sql的执行
* @throws IOException
*/
@Test
public void testMerchantSelect() throws IOException {
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sqlSessionFactory.openSession();
try {
MerchantMapper mapper = session.getMapper(MerchantMapper.class);
Merchant merchant = mapper.selectById(1);
System.out.println(merchant);
} finally {
session.close();
}
}
}