我们的一个mybatis程序
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); //返回的DefaultSqlSessionFactory的实例 SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder .build(ClassLoader.getSystemResourceAsStream("mybatis.xml")); SqlSession sqlSession = sqlSessionFactory.openSession();
SqlSessionFactory是Mybatis的一个核心类负责创建SqlSession
SqlSessionFactory是由SqlSessionFactoryBuilder的build方法创建
public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) { SqlSessionFactory var5; try { XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties); var5 = this.build(parser.parse()); } catch (Exception var14) { throw ExceptionFactory.wrapException("Error building SqlSession.", var14); } finally { ErrorContext.instance().reset(); try { inputStream.close(); } catch (IOException var13) { ; } } return var5; }
内部调用了XMLConfigBuilder parser 看名字 我们可以看出来他是负责解析mybatis.
public Configuration parse() { if (this.parsed) { throw new BuilderException("Each XMLConfigBuilder can only be used once."); } else { this.parsed = true; this.parseConfiguration(this.parser.evalNode("/configuration")); return this.configuration; } }
this.parser.evalNode("/configuration")获得根节点
private void parseConfiguration(XNode root) { try { Properties settings = this.settingsAsPropertiess(root.evalNode("settings")); this.propertiesElement(root.evalNode("properties")); this.loadCustomVfs(settings); this.typeAliasesElement(root.evalNode("typeAliases")); this.pluginElement(root.evalNode("plugins")); this.objectFactoryElement(root.evalNode("objectFactory")); this.objectWrapperFactoryElement(root.evalNode("objectWrapperFactory")); this.reflectionFactoryElement(root.evalNode("reflectionFactory")); this.settingsElement(settings); this.environmentsElement(root.evalNode("environments")); this.databaseIdProviderElement(root.evalNode("databaseIdProvider")); this.typeHandlerElement(root.evalNode("typeHandlers")); this.mapperElement(root.evalNode("mappers")); } catch (Exception var3) { throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + var3, var3); } }
settings|typeHandlers|mappers|properties逐个解析最终封装到Configuration 对象里面
public SqlSessionFactory build(Configuration config) { return new DefaultSqlSessionFactory(config); }
最终将解析好的数据都封装到configuration里面 初始化DefaultSqlSessionFactory (建造者模式)
Configuration属性
TypeAliasRegistry
我们定义的别名
<typeAliases> <!-- 批量别名定义,指定包名,mybatis自动扫描包中的po类,自动定义别名,别名是类名(首字母大写或小写都可以,一般用小写) --> <package name="com.liqiang.entity" /> <package name="com.liqiang.vo" /> </typeAliases>
点开这个类的构造函数可以发现默认注册了很多基本类型的别名,这就是为什么我们int double不用指定全名称的原因
public TypeAliasRegistry() { this.registerAlias("string", String.class); this.registerAlias("byte", Byte.class); this.registerAlias("long", Long.class); this.registerAlias("short", Short.class); this.registerAlias("int", Integer.class); this.registerAlias("integer", Integer.class); this.registerAlias("double", Double.class); this.registerAlias("float", Float.class); this.registerAlias("boolean", Boolean.class); ......}
TypeHandlerRegistry
public TypeHandlerRegistry() { this.register((Class)Boolean.class, (TypeHandler)(new BooleanTypeHandler())); this.register((Class)Boolean.TYPE, (TypeHandler)(new BooleanTypeHandler())); this.register((JdbcType)JdbcType.BOOLEAN, (TypeHandler)(new BooleanTypeHandler())); this.register((JdbcType)JdbcType.BIT, (TypeHandler)(new BooleanTypeHandler())); ..... }
注册我们自定义的TypeHandler,可以看到默认实现了很多,所以我们对于常见的类型不需要再定义typeHandle
mappedStatements
Map<String, MappedStatement> mappedStatements
mybatis会将所有select|update|insert|delete标签解析为MappedStatement对象的形式保存起来
内部主要属性:
ParameterMap 一对多ParametersMapping 保存参数的映射关系
ResultMap 一对多 ResultMapping 保存返回结果的映射关系
我们可以发现刚开始mybatis会将xml里面的每个标签通过configuration封装起来通过他初始化SqlSessionFactory
原文地址:https://www.cnblogs.com/LQBlog/p/9277786.html
时间: 2024-10-08 21:52:22