为什么使用mapper接口能对数据库进行访问?
(理论回答/画图(流程图、时序图、类图等))
debug开始
获取sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
创建SqlSession的步骤
1、首先从配置项中获取Environment对象,主要环境信息(数据源、事务工厂)
2、创建事务工厂
TransactionManager type =“JDBC” dataSource type =“POOLED”
3、创建事务对象
4、从Configuration对象中获取Executor执行器
5、new DefaultSqlSession()
获取对应mapper
TUserMapper mapper = sqlSession.getMapper(TUserMapper.class);
执行查询语句并返回单条数据
TUser user = mapper.selectByPrimaryKey(1);
SqlSessionManager(应用于多线程环境)
1、与DefaultSqlSessionFactory的行为相同,同一个线程
2、使用ThreadLocal变量使得当前线程循环使用SqlSession
Mybatis两种编程模型
1、mapper接口编程
2、使用sqlsession对外提供数据库