MyBatis运行流程以及四大对象的创建时机

package zhang.mybatis.test;

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

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.apache.ibatis.type.TypeHandler;
import org.junit.Test;

import zhang.mybatis.dao.CustomerMapper;
import zhang.mybatis.entities.Customer;

public class TestMybatisRun {

	public SqlSessionFactory getSqlSessionFactory() throws IOException {
		InputStream inStream = Resources.getResourceAsStream("mybatis-config.xml");
		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inStream);
		return sessionFactory;
	}

	/**
	 * 第一步:获取SqlSessionFactory
	 * 
	 * Mybatis运行流程解析:使用
	 * SqlSessionFactoryBuilder().build(in)方法先解析mybatis-config.xml即
	 * mybatis的全局配置文件,封装成一个Configuration对象,此对象中包含了mybatis的全部的配置信息,其中重要的有
	 * MappedStatement:代表了***mapper.xml文件中的一个增删改查操作.
	 * MapperRegistry:此对象中一个map:CustomerMapplerAnotation=MapperProxyFactory(
	 * MapperProxyFactory:创建代理mapper的工厂)
	 *
	 * 然后通过以上操作得到Configuration:创建new DefaultSqlSessionFactory(config);
	 * 返回SqlSessionFactory(DefaultSqlSessionFactory)
	 *
	 * @throws IOException
	 */
	@Test
	public void testgetsqlsessionfactory() throws IOException {
		SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
		System.out.println(sqlSessionFactory);
	}

	/**
	 * 第二步:调用第一步获取的DefaultSqlSessionFactory对象的openSession方法
	 * 1.实际上会调用DefaultSqlSessionFactory的openSessionFromDataSource(configuration.
	 * getDefaultExecutorType(), null, false);
	 * 2.获取DefaultExecutorType,获取执行器的类型,Simple
	 * 3.在openSessionFromDataSource方法中根据传入的执行的类型创建执行器,如果配置了二级缓存
	 *  if (cacheEnabled) {executor = new CachingExecutor(executor);}
	 * mybatis会将执行器进行包装然后返回
	 * 4.最终将创建的执行器和配置传入创建一个sqlsession对象返回
	 * code: return new DefaultSqlSession(configuration, executor, autoCommit);
	 * @throws IOException
	 */
	@Test
	public void testopensession() throws IOException {
		SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
		SqlSession openSession = sqlSessionFactory.openSession();
		System.out.println(openSession);

	}
	/**
	 * 第三步获取Mapper
	 * 1.调用从第二步获取的DefaultSqlSession.getMapper()方法
	 * 1)调用其中 configuration.<T>getMapper(type, this);
	 * 2)调用configuration中 mapperRegistry.getMapper(type, sqlSession);
	 * 从第一步中的 knownMappers.get(type);获取代理mapper创建的工厂对象(MapperProxyFactory)
	 * 在利用工厂对象创建Mapper的代理对象返回
	 * @throws IOException 
	 */
	
	/**
	 * 第四步执行mapper的方法
	 * 1.首先会执行代理mapper的invoke方法,mapperMethod.execute(sqlSession, args);
	 * 在execute方法中首先会判断sql的类型是select,insert,update,还是delete类型
	 * 2.然后一系列的操作
	 * 创建StatementHandler对象,此对象用于构建preperstatment对象,该对象会经过拦截器链进行一系列的包装
	 * 创建resultsethander对象,此对象用户处理结果集,这两个对象都是用过类型处理器 TypeHandler(Intgeter)来进行
	 * 在床架statementhander的时候回同时创建
	 *  this.parameterHandler = configuration.newParameterHandler(mappedStatement, parameterObject, boundSql);
    	this.resultSetHandler = configuration.newResultSetHandler(executor, mappedStatement, rowBounds, parameterHandler, resultHandler, boundSql);
    	
	 * 最后调用执行器的executor.query()方法查询数据库封装结果返回,
	 * @throws IOException
	 */
	@Test
	public void testgetMapper() throws IOException{
		SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
		SqlSession openSession = sqlSessionFactory.openSession();
		try {
			CustomerMapper mapper = openSession.getMapper(CustomerMapper.class);
			Customer customerById = mapper.getCustomerById(41);
			System.out.println(customerById);
			System.out.println(mapper);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值