一。解析加载进内存
1、读取xml配置文件转换成内存中的流数据
2、流数据在SqlSessionFactoryBuilder中解析封装成Configuration对象
Configuration对象主要包含 DataSource数据源 和 Map<String,MappedStatement> mappedStatementMap存储 SQL语句
3、SqlSessionFactoryBuilder.build(Configuration)创建SqlSessionFactory对象,并将Configuration传给SqlSessionFactory
4、SqlSessionFactory.openSession SqlSessionFactory将Configuration传给SqlSession
最核心流程:解析配置文件封装成Configuration对象并最终传给SqlSession
二、框架执行过程
使用sqlSession封装的API 和 getMapper动态代理方式
使用sqlSession封装的API方式,sqlSession类有两个 属性Executor和Configuration。
内部使用执行器simpleExecutor.simpleExecutor封装了JDBC操作,simpleExecutor主要流程
1、从DataSource数据源获取连接Connection
2、从 Map<String,MappedStatement> mappedStatementMap获取SQL,解析SQL语句
3、获取预处理对象:preparedStatement
4. 设置参数
5. 执行sql
6. 封装返回结果集
三、源码分析
去除不重要的代码
1、读取配置文件
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
2、构建SqlSessionFactory
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
3、build()方法
public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);
return build(parser.parse());
}
XMLConfigBuilder专门解析mybatis配置文件的类
parser.parse()返回核心配置对象Configuration
4、parse()方法
看下parse方法,注意返回的就是Configuration
public Configuration parse() {
parseConfiguration(parser.evalNode("/configuration"));
return configuration;
}
5、parseConfiguration()方法
这里就是解析封装到Configuration 对象中
private void parseConfiguration(XNode root) {
propertiesElement(root.evalNode("properties"));
Properties settings = settingsAsProperties(root.evalNode("settings"));
loadCustomVfs(settings);
typeAliasesElement(root.evalNode("typeAliases"));
pluginElement(root.evalNode("plugins"));
objectFactoryElement(root.evalNode("objectFactory"));
objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
reflectorFactoryElement(root.evalNode("reflectorFactory"));
settingsElement(settings);
environmentsElement(root.evalNode("environments"));
databaseIdProviderElement(root.evalNode("databaseIdProvider"));
typeHandlerElement(root.evalNode("typeHandlers"));
mapperElement(root.evalNode("mappers"));
}
注意最后一个解析: mapperElement(root.evalNode(“mappers”)),就是解析Mapper.xml文件的SQL,每个SQL封装成一个MappedStatement,看下MappedStatement类:
public final class MappedStatement {
private String resource;
private Configuration configuration;
private String id;
private Integer fetchSize;
private Integer timeout;
private StatementType statementType;
private ResultSetType resultSetType;
private SqlSource sqlSource;
private Cache cache;
private ParameterMap parameterMap;
private List<ResultMap> resultMaps;
private boolean flushCacheRequired;
private boolean useCache;
private boolean resultOrdered;
private SqlCommandType sqlCommandType;
private KeyGenerator keyGenerator;
private String[] keyProperties;
private String[] keyColumns;
private boolean hasNestedResultMaps;
private String databaseId;
private Log statementLog;
private LanguageDriver lang;
private String[] resultSets;
}
核心配置类Configuration 类里有个Map:
public class Configuration {
protected final Map<String, MappedStatement> mappedStatements = new StrictMap<MappedStatement>("Mapped Statements collection");
}
所以Mapper.xml解析的SQL都在这个Map里,key是namespace+sqlID组成,value就是MappedStatement 。
到现在build()将配置文件解析完成,返回Configuration,回到第3步build()方法
public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);
return build(parser.parse());
}
6、build(parser.parse());
parser.parse()解析完成返回的Configuration
public SqlSessionFactory build(Configuration config) {
return new DefaultSqlSessionFactory(config);
}
public DefaultSqlSessionFactory(Configuration configuration) {
this.configuration = configuration;
}
至此,构建完成,返回一个DefaultSqlSessionFactory