Mybatis源码分析之SqlSessionFactory(一)

更新时间:2023-05-21 12:54:46 阅读: 评论:0

Mybatis源码分析之SqlSessionFactory(⼀)
⽬录
简介
MyBatis的前⾝叫iBatis,本是apache的⼀个开源项⽬, 2010年这个项⽬由apache software foundation 迁移到了google code,并且改名为MyBatis。MyBatis是⽀持普通SQL查询,存储过程和⾼级映射的优秀持久层框架。MyBatis消除了⼏乎所有的JDBC代码和参数的⼿⼯设置以及结果集的检索。MyBatis使⽤简单的XML或注解⽤于配置和原始映射,将接⼝和Java的POJOs(Plan Old Java Objects,普通的Java对象)映射成数据库中的记录。
实验代码
public static void main(String[] args) throws Exception {
SqlSessionFactory ssionFactory = null;
String resource = "l";
ssionFactory = new SqlSessionFactoryBuilder().ResourceAsReader(resource));
SqlSession sqlSession = ssionFactory.openSession();
UrMapper urMapper = Mapper(UrMapper.class);
System.out.println(urMapper.findUrById(1));
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-////DTD Config 3.0//EN"
"/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 指定properties配置⽂件,我这⾥⾯配置的是数据库相关 -->
<properties resource="resource.properties"></properties>
<!-- 指定Mybatis使⽤log4j -->
<ttings>
<tting name="logImpl" value="LOG4J" />
</ttings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<!-- 上⾯指定了数据库配置⽂件,配置⽂件⾥⾯也是对应的这四个属性 -->
<property name="driver" value="${jdbc.driverClass}" />
<property name="url" value="${jdbc.url}" />
<property name="urname" value="${jdbc.urName}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
</environments>
<!-- 映射⽂件,mybatis精髓,后⾯才会细讲 -->
<mappers>
<mapper resource="l"/>
</mappers>
</configuration>
resource.properties
jdbc.sql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?uUnicode=true&characterEncoding=utf-8
jdbc.urName=root
jdbc.password=root
SqlSessionFactoryBuilder
更加上⾯demo的main⽅法我们先来看下 SqlSessionFactoryBuilder,他的主要⽬的是创建⼀个SqlSessionFactory 看下⾯源码:
/**空姐培训学校
* Builds {@link SqlSession} instances.
*
* @author Clinton Begin
*/
public class SqlSessionFactoryBuilder {
public SqlSessionFactory build(Reader reader) {  //Reader读取mybatis配置⽂件 "l";
return build(reader, null, null);
}齿圈
public SqlSessionFactory build(Reader reader, String environment) {
return build(reader, environment, null);
}
public SqlSessionFactory build(Reader reader, Properties properties) {
capitel
return build(reader, null, properties);
}
/
环保演讲稿
/通过XMLConfigBuilder解析mybatis配置,然后创建SqlSessionFactory对象
public SqlSessionFactory build(Reader reader, String environment, Properties properties) {
try {
XMLConfigBuilder parr = new XMLConfigBuilder(reader, environment, properties);
batistareturn build(parr.par());
} catch (Exception e) {
throw ExceptionFactory.wrapException("Error building SqlSession.", e);
} finally {
ErrorContext.instance().ret();
try {
reader.clo();
} catch (IOException e) {
// Intentionally ignore. Prefer previous error.
}
韩国语培训
}
}
public SqlSessionFactory build(InputStream inputStream) {
return build(inputStream, null, null);
}
public SqlSessionFactory build(InputStream inputStream, String environment) {
return build(inputStream, environment, null);
}
public SqlSessionFactory build(InputStream inputStream, Properties properties) {
return build(inputStream, null, properties);
}
public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
try {
XMLConfigBuilder parr = new XMLConfigBuilder(inputStream, environment, properties);
return build(parr.par());
} catch (Exception e) {
throw ExceptionFactory.wrapException("Error building SqlSession.", e);
} finally {
ErrorContext.instance().ret();
try {
inputStream.clo();
} catch (IOException e) {
// Intentionally ignore. Prefer previous error.
}
}
}
public SqlSessionFactory build(Configuration config) {
return new DefaultSqlSessionFactory(config);
pegasus}
}
通过源码,我们可以看到SqlSessionFactoryBuilder有不同的参数的build,通过XMLConfigBuilder 去解析我们传⼊的mybatis的配置⽂件,转化成Configuration,通过SqlSessionFactory build(Configuration config)
最终创建DefaultSqlSessionFactory,下⾯就接着看看 XMLConfigBuilder 部分源码
// mybatis 配置⽂件解析
public XMLConfigBuilder(Reader reader, String environment, Properties props) {
this(new XPathParr(reader, true, props, new XMLMapperEntityResolver()), environment, props);
}
private XMLConfigBuilder(XPathParr parr, String environment, Properties props) {
super(new Configuration());
ErrorContext.instance().resource("SQL Mapper Configuration");
this.pard = fal;
caitlin
this.parr = parr;
}
//读取XMLConfigBuilder返回⼀个Configuration对象
public Configuration par() {
if (pard) {
throw new BuilderException("Each XMLConfigBuilder can only be ud once.");
}
pard = true;
parConfiguration(parr.evalNode("/configuration"));//xml的根节点
return configuration;
}
//configuration下⾯能配置的节点为以下11个节点 reflectionFactory是后⾯版本新增的
//不同⼦节点进⾏Element处理
shawshankprivate void parConfiguration(XNode root) {
try {
Properties ttings = ttingsAsPropertiess(root.evalNode("ttings"));
//issue #117 read properties first
propertiesElement(root.evalNode("properties"));
loadCustomVfs(ttings);
typeAliasElement(root.evalNode("typeAlias"));
pluginElement(root.evalNode("plugins"));
objectFactoryElement(root.evalNode("objectFactory"));
lookforwardobjectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
reflectionFactoryElement(root.evalNode("reflectionFactory"));
ttingsElement(ttings);
// read it after objectFactory and objectWrapperFactory issue #631
environmentsElement(root.evalNode("environments"));
databaIdProviderElement(root.evalNode("databaIdProvider"));
typeHandlerElement(root.evalNode("typeHandlers"));
mapperElement(root.evalNode("mappers"));
} catch (Exception e) {
throw new BuilderException("Error parsing SQL Mapper Configuration. Cau: " + e, e);
}
}
通过以上源码,我们就能看出,在mybatis的配置⽂件中:
1. configuration节点为根节点。
2. 在configuration节点之下,我们可以配置11个⼦节点, 分别为:properties、typeAlias、plugins、objectFactory、
objectWrapperFactory、ttings、environments、databaIdProvider、typeHandlers、mappers、reflectionFactory(后⾯版本新增的)
如下图:
下⾯我们看下⼦节点的源码
1:properties(相关配置读取)
private void propertiesElement(XNode context) throws Exception {
if (context != null) {
Properties defaults = ChildrenAsProperties();
String resource = StringAttribute("resource");
String url = StringAttribute("url");
if (resource != null && url != null) { //如果resource和url都为空就抛出异常
throw new BuilderException("The properties element cannot specify both a URL and a resource bad property file reference.  Plea specify one or the oth      }
if (resource != null) {
defaults.ResourceAsProperties(resource));
} el if (url != null) {
defaults.UrlAsProperties(url));
}
Properties vars = Variables();
if (vars != null) {
defaults.putAll(vars);
}
//把Properties defaults 设置到 parr.tVariables
parr.tVariables(defaults);
//把Properties defaults 设置到 configuration.tVariables
configuration.tVariables(defaults);
}
}
2:ttings 全局性的配置

本文发布于:2023-05-21 12:54:46,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/117080.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:配置   数据库   节点   源码   映射   结果
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图