上一篇: 【Java8】一、Java8新特性
文章目录
一、源码构建
- 1、官方网站:https://blog.mybatis.org/
- 2、官方文档:https://mybatis.org/mybatis-3/
- 3、源码下载:https://github.com/mybatis
- 4、源码构建
从Github下载下来的源码导入到IDEA后,通过maven compile,会产生如下错误,将代码中的license.txt文件删除即可
- 5、需要mybatis带注释的源码可以留言
- 6、IDEA安装Statistic插件,可以统计项目代码行数;mybatis一共10多万行
二、纯MyBatis代码开发步骤
- 第一步:读取mybatis-config.xml配置文件;
- 第二步:构建SqlSessionFactory;
- 第三步:打开SqlSession;
- 第四步:获取Mapper接口对象;
- 第五步:调用Mapper接口对象的方法操作数据库;
具体代码实现:
//第一步:读取mybatis-config.xml配置文件
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
//第二步:构建SqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//第三步:打开SqlSession
SqlSession session = sqlSessionFactory.openSession();
//第四步:获取Mapper接口对象
UUserInfoMapper uUserInfoMapper = session.getMapper(UUserInfoMapper.class);
//第五步:调用Mapper接口对象的方法操作数据库;
UUserInfo uUserInfo = uUserInfoMapper.selectByPrimaryKey(1);
//第六步:业务处理
log.info("查询结果: " + uUserInfo.getId() + "--" + uUserInfo.getPhone());
三、源码分析
第一步:读取mybatis-config.xml配置文件
//第一步:读取mybatis-config.xml配置文件
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
通过Resources工具类,调用ClassLoader读取classpath下的mybatis-config.xml配置文件,得到一个输入流;
第二步:构建SqlSessionFactory
//第二步: (有关配置文件的解析与处理都在该步骤中)
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
1、创建一个SqlSessionFactoryBuilder对象:
new SqlSessionFactoryBuilder();
2、调用SqlSessionFactoryBuilder对象的build方法:
build(InputStream inputStream)
3、创建XMLConfigBuilder 对象:
XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);
4、创建XMLMapperEntityResolver对象:
new XMLMapperEntityResolver()
5、创建XPathParser对象:
new XPathParser(inputStream, true, props, new XMLMapperEntityResolver())
6、创建XPath对象:
XPathFactory factory = XPathFactory.newInstance();
this.xpath = factory.newXPath();
7、创建Document对象:(DOM解析)
//JDK提供的文档解析工厂对象
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//创建一个DocumentBuilder对象
DocumentBuilder builder = factory.newDocumentBuilder();
//解析输入源的xml数据为一个Document文件
return builder.parse(inputSource);
8、创建Configuration对象
new Configuration()
注册类型别名
初始化Configuration对象的一些成员变量的默认值
9、通过XPath方法解析mybatis-config.xml文件:
parseConfiguration(parser.evalNode("/configuration"));
将解析出来的信息封装到Configuration对象中;
10、通过持有Configuration对象创建一个DefaultSqlSessionFactory对象:
new DefaultSqlSessionFactory(configuration);
第三步:打开SqlSession
/第三步:打开SqlSession
Session session = sqlSessionFactory.openSession();
1、从configuration配置对象中获取环境environment信息;
2、根据环境environment信息获取事务工厂TransactionFactory;
3、根据环境信息中创建一个事务对象Transaction;
4、根据configuration和Transaction创建一个执行器Executor对象;
5、根据configuration和Executor创建一个默认的SqlSession对象;
第四步:获取Mapper接口对象
//第四步:获取Mapper接口对象
UUserInfoMapper uUserInfoMapper = session.getMapper(UUserInfoMapper.class);
1、从configuration中获取Mapper接口对象;
2、从mapperRegistry中获取Mapper接口对象;
3、从knownMappers这个Map中获取MapperProxyFactory代理工厂;
4、通过MapperProxyFactory代理工厂创建MapperProxy实例;
5、JDK动态代理生成Mapper接口的代理对象;
第五步:调用Mapper接口对象的方法操作数据库
//第五步:调用Mapper接口对象的方法操作数据库;
UUserInfo uUserInfo = uUserInfoMapper.selectByPrimaryKey(1);
总结:
MyBatis解析配置文件的本质就是为了获得Configuration对象;
Configuration 对象可以理解是mybatis的XML配置文件在程序中的化身,是MyBatis非常重要的一个对象,里面封装了MyBatis的整个配置信息;
以上即为MyBatis框架源码分析的上半部分;