您现在的位置是:首页 > 文章详情

Mybatis工作流程及其原理与解析

日期:2019-01-01点击:320

Mybatis简介:

MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。本文将通过debug的方式来了解其工作原理。

Mybatis核心类:

SqlSessionFactory:每个基于 MyBatis 的应用都是以一个 SqlSessionFactory 的实例为中心的。SqlSessionFactory 的实例可以通过 SqlSessionFactoryBuilder 获得。而 SqlSessionFactoryBuilder 则可以从 XML 配置文件或通过Java的方式构建出 SqlSessionFactory 的实例。SqlSessionFactory 一旦被创建就应该在应用的运行期间一直存在,建议使用单例模式或者静态单例模式。一个SqlSessionFactory对应配置文件中的一个环境(environment),如果你要使用多个数据库就配置多个环境分别对应一个SqlSessionFactory。

SqlSession:SqlSession是一个接口,它有2个实现类,分别是DefaultSqlSession(默认使用)以及SqlSessionManager。SqlSession通过内部存放的执行器(Executor)来对数据进行CRUD。此外SqlSession不是线程安全的,因为每一次操作完数据库后都要调用close对其进行关闭,官方建议通过try-finally来保证总是关闭SqlSession。

Executor:Executor(执行器)接口有两个实现类,其中BaseExecutor有三个继承类分别是BatchExecutor(重用语句并执行批量更新),ReuseExecutor(重用预处理语句prepared statements),SimpleExecutor(普通的执行器)。以上三个就是主要的Executor。通过下图可以看到Mybatis在Executor的设计上面使用了装饰者模式,我们可以用CachingExecutor来装饰前面的三个执行器目的就是用来实现缓存。

img_99027fb483a2651a94a79cfeea8af290.jpe

MappedStatement:MappedStatement就是用来存放我们SQL映射文件中的信息包括sql语句,输入参数,输出参数等等。一个SQL节点对应一个MappedStatement对象。

Mybatis工作流程:

img_b06ac6a09b23d60faf14a75ddfe33a74.jpe

阅读全文有惊喜哦!!!

下面将通过debug方式对Mybatis进行一步步解析。首先贴出我的mybatis-config.xml文件以及Mapper.xml文件。


PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">


PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

select * from user

User where id = #{id}

insert into User (username,birthday,sex,address)

values (#{name},#{birthday},#{sex},#{address})

update User set username = #{username},birthday = #{birthday},

sex = #{sex},address = #{address} where id = #{id}

delete from User where id = #{id}

select * from User where sex = #{param1}

and username like #{param2}

and address = #{parma3}

select count(*) from user where username like #{username}

username like #{pattern}

and sex = #{sex}

and address = #{address}

where id in

#{id}

img_526fcc46e932b2d2b9a3ba67e761c6b5.jpe

第一步通过SqlSessionFactoryBuilder创建SqlSessionFactory:

首先在SqlSessionFactoryBuilder的build()方法中可以看到MyBatis内部定义了一个类XMLConfigBuilder用来解析配置文件mybatis-config.xml。针对配置文件中的每一个节点进行解析并将数据存放到Configuration这个对象中,紧接着使用带有Configuration的构造方法发返回一个DefautSqlSessionFactory。

public SqlSessionFactory build(InputStream inputStream) {

return build(inputStream, null, null);

}

public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {

try {

XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);

//解析mybatis-config.xml

return build(parser.parse());

} catch (Exception e) {

throw ExceptionFactory.wrapException("Error building SqlSession.", e);

} finally {

ErrorContext.instance().reset();

try {

inputStream.close();

} catch (IOException e) {

// Intentionally ignore. Prefer previous error.

}

}

}

//返回SqlSessionFactory,默认使用的是实现类DefaultSqlSessionFactory

public SqlSessionFactory build(Configuration config) {

return new DefaultSqlSessionFactory(config);

}

public Configuration parse() {

if (parsed) {

throw new BuilderException("Each XMLConfigBuilder can only be used once.");

}

parsed = true;

//获取根节点configuration

parseConfiguration(parser.evalNode("/configuration"));

return configuration;

}

//开始解析mybatis-config.xml,并把解析后的数据存放到configuration中

private void parseConfiguration(XNode root) {

try {

//保存mybatis-config.xml中的标签setting,本例中开启全局缓存cacheEnabled,设置默认执行器defaultExecutorType=REUSE

Properties settings = settingsAsPropertiess(root.evalNode("settings"));

//issue #117 read properties first

//解析是否配置了外部properties,例如本例中配置的jdbc.propertis

propertiesElement(root.evalNode("properties"));

//查看是否配置了VFS,默认没有,本例也没有使用

loadCustomVfs(settings);

//查看是否用了类型别名,减少完全限定名的冗余,本例中使用了别名User代替了com.ctc.Model.User

typeAliasesElement(root.evalNode("typeAliases"));

//查看是否配置插件来拦截映射语句的执行,例如拦截Executor的Update方法,本例没有使用

pluginElement(root.evalNode("plugins"))

//查看是否配置了ObjectFactory,默认情况下使用对象的无参构造方法或者是带有参数的构造方法,本例没有使用

objectFactoryElement(root.evalNode("objectFactory"));

//查看是否配置了objectWrapperFatory,这个用来或者ObjectWapper,可以访问:对象,Collection,Map属性。本例没有使用

objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));

//查看是否配置了reflectorFactory,mybatis的反射工具,提供了很多反射方法。本例没有使用

reflectorFactoryElement(root.evalNode("reflectorFactory"));

//放入参数到configuration对象中

settingsElement(settings);

// read it after objectFactory and objectWrapperFactory issue #631

//查看数据库环境配置

environmentsElement(root.evalNode("environments"));

//查看是否使用多种数据库,本例没有使用

databaseIdProviderElement(root.evalNode("databaseIdProvider"));

//查看是否配置了新的类型处理器,如果跟处理的类型跟默认的一致就会覆盖。本例没有使用

typeHandlerElement(root.evalNode("typeHandlers"));

//查看是否配置SQL映射文件,有四种配置方式,resource,url,class以及自动扫包package。本例使用package

mapperElement(root.evalNode("mappers"));

} catch (Exception e) {

throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);

}

}

第二步通过SqlSessionFactory创建SqlSession:

@Override

public SqlSession openSession() {

return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);

}

private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {

Transaction tx = null;

try {

//拿到前文从mybatis中解析到的数据库环境配置

final Environment environment = configuration.getEnvironment();

final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);

//拿到jdbc的事务管理器,有两种一种是jbc,一种的managed。本例使用的是JdbcTransaction

tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);

//从mybatis配置文件可以看到本例使用了REUSE,因此返回的是ReuseExecutor并把事务传入对象中

final Executor executor = configuration.newExecutor(tx, execType);

return new DefaultSqlSession(configuration, executor, autoCommit);

} catch (Exception e) {

closeTransaction(tx); // may have fetched a connection so lets call close()

throw ExceptionFactory.wrapException("Error opening session. Cause: " + e, e);

} finally {

ErrorContext.instance().reset();

}

}

public Executor newExecutor(Transaction transaction, ExecutorType executorType) {

executorType = executorType == null ? defaultExecutorType : executorType;

executorType = executorType == null ? ExecutorType.SIMPLE : executorType;

Executor executor;

if (ExecutorType.BATCH == executorType) {

executor = new BatchExecutor(this, transaction);

} else if (ExecutorType.REUSE == executorType) {

executor = new ReuseExecutor(this, transaction);

} else {

executor = new SimpleExecutor(this, transaction);

}

if (cacheEnabled) {

executor = new CachingExecutor(executor);

}

executor = (Executor) interceptorChain.pluginAll(executor);

return executor;

}

//返回一个SqlSession,默认使用DefaultSqlSession

public DefaultSqlSession(Configuration configuration, Executor executor, boolean autoCommit) {

this.configuration = configuration;

this.executor = executor;

this.dirty = false;

this.autoCommit = autoCommit;

}

第三步通过SqlSession拿到Mapper对象的代理:

@Override

public T getMapper(Class type) {

return configuration.getMapper(type, this);

}

public T getMapper(Class type, SqlSession sqlSession) {

//前文解析Mybatis-config.xml的时候,在解析标签mapper就是用configuration对象的mapperRegistry存放数据

return mapperRegistry.getMapper(type, sqlSession);

}

@SuppressWarnings("unchecked")

public T getMapper(Class type, SqlSession sqlSession) {

//knownMapper是一个HashMap在存放mapperRegistry的过程中,以每个Mapper对象的类型为Key, MapperProxyFactory 为value保存。

//例如本例中保存的就是Key:com.ctc.mapper.UserMapper,value就是保存了key的MapperProxyFactory对象

final MapperProxyFactory mapperProxyFactory = (MapperProxyFactory) knownMappers.get(type);

if (mapperProxyFactory == null) {

throw new BindingException("Type " + type + " is not known to the MapperRegistry.");

}

try {

return mapperProxyFactory.newInstance(sqlSession);

} catch (Exception e) {

throw new BindingException("Error getting mapper instance. Cause: " + e, e);

}

}

public T newInstance(SqlSession sqlSession) {

//生成一个mapperProxy对象,这个对象实现了InvocationHandler, Serializable。就是JDK动态代理中的方法调用处理器

final MapperProxy mapperProxy = new MapperProxy(sqlSession, mapperInterface, methodCache);

return newInstance(mapperProxy);

}

public MapperProxy(SqlSession sqlSession, Class mapperInterface, Map methodCache) {

this.sqlSession = sqlSession;

this.mapperInterface = mapperInterface;

this.methodCache = methodCache;

}

@SuppressWarnings("unchecked")

protected T newInstance(MapperProxy mapperProxy) {

//通过JDK动态代理生成一个Mapper的代理,在本例中的就是UserMapper的代理类,它实现了UserMapper接口

return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);

}

第四步通过MapperProxy调用Maper中相应的方法:

@Override

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

//判断当前调用的method是不是Object中声明的方法,如果是的话直接执行。

if (Object.class.equals(method.getDeclaringClass())) {

try {

return method.invoke(this, args);

} catch (Throwable t) {

throw ExceptionUtil.unwrapThrowable(t);

}

}

final MapperMethod mapperMethod = cachedMapperMethod(method);

return mapperMethod.execute(sqlSession, args);

}

//把当前请求放入一个HashMap中,一旦下次还是同样的方法进来直接返回。

private MapperMethod cachedMapperMethod(Method method) {

MapperMethod mapperMethod = methodCache.get(method);

if (mapperMethod == null) {

mapperMethod = new MapperMethod(mapperInterface, method, sqlSession.getConfiguration());

methodCache.put(method, mapperMethod);

}

return mapperMethod;

}

public Object execute(SqlSession sqlSession, Object[] args) {

Object result;

switch (command.getType()) {

case INSERT: {

Object param = method.convertArgsToSqlCommandParam(args);

result = rowCountResult(sqlSession.insert(command.getName(), param));

break;

}

case UPDATE: {

Object param = method.convertArgsToSqlCommandParam(args);

result = rowCountResult(sqlSession.update(command.getName(), param));

break;

}

case DELETE: {

Object param = method.convertArgsToSqlCommandParam(args);

result = rowCountResult(sqlSession.delete(command.getName(), param));

break;

}

case SELECT:

if (method.returnsVoid() && method.hasResultHandler()) {

executeWithResultHandler(sqlSession, args);

result = null;

} else if (method.returnsMany()) {

result = executeForMany(sqlSession, args);

} else if (method.returnsMap()) {

result = executeForMap(sqlSession, args);

} else if (method.returnsCursor()) {

result = executeForCursor(sqlSession, args);

} else {

//本次案例会执行selectOne

Object param = method.convertArgsToSqlCommandParam(args);

result = sqlSession.selectOne(command.getName(), param);

}

break;

case FLUSH:

result = sqlSession.flushStatements();

break;

default:

throw new BindingException("Unknown execution method for: " + command.getName());

}

if (result == null && method.getReturnType().isPrimitive() && !method.returnsVoid()) {

throw new BindingException("Mapper method '" + command.getName()

+ " attempted to return null from a method with a primitive return type (" + method.getReturnType() + ").");

}

return result;

}

@Override

public <T> T selectOne(String statement, Object parameter) {

// Popular vote was to return null on 0 results and throw exception on too many.

List<T> list = this.<T>selectList(statement, parameter);

if (list.size() == 1) {

return list.get(0);

} else if (list.size() > 1) {

throw new TooManyResultsException("Expected one result (or null) to be returned by selectOne(), but found: " + list.size());

} else {

return null;

}

}

@Override

public <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds) {

try {

MappedStatement ms = configuration.getMappedStatement(statement);

return executor.query(ms, wrapCollection(parameter), rowBounds, Executor.NO_RESULT_HANDLER);

} catch (Exception e) {

throw ExceptionFactory.wrapException("Error querying database. Cause: " + e, e);

} finally {

ErrorContext.instance().reset();

}

}

//这边调用的是CachingExecutor类的query,还记得前文解析mybatis-config.xml的时候我们指定了REUSE但是因为在配置文件中开启了缓存

//所以ReuseExecutor被CachingExecotur装饰,新增了缓存的判断,最后还是会调用ReuseExecutor

@Override

public <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException {

BoundSql boundSql = ms.getBoundSql(parameterObject);

CacheKey key = createCacheKey(ms, parameterObject, rowBounds, boundSql);

return query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);

}

@Override

public <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql)

throws SQLException {

Cache cache = ms.getCache();

if (cache != null) {

flushCacheIfRequired(ms);

if (ms.isUseCache() && resultHandler == null) {

ensureNoOutParams(ms, parameterObject, boundSql);

@SuppressWarnings("unchecked")

List<E> list = (List<E>) tcm.getObject(cache, key);

if (list == null) {

//如果缓存中没有数据则查询数据库

list = delegate.<E> query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);

//结果集放入缓存

tcm.putObject(cache, key, list); // issue #578 and #116

}

return list;

}

}

return delegate.<E> query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);

}

这里给大家推荐一个java架构师的学习路线(技术块)

在互联网寒冬季,更需要加强学习,防止被离职!

依次:开源框架解析-架构师筑基-高性能架构-微服务架构-团队协作开发-B2C商城实战

脑图和相关资料获取方式

可以直接加群960439918获取免费架构资料

一、开源框架解析:

阅读、分析源码是程序员最基本的码代码能力也是码农的根本所在,学习经典源码中所用到的经典设计思想及常用设计模式,能够帮你了解大牛是如何写代码的,从而吸收大牛的代码功力。在阿里面试中,MyBatis,Spring等框架的底层原理是经常会被问到的。

img_fb64fa5109a7788fe88aec3b561f863d.jpe

二、架构师筑基:

百丈高楼平地起,基础也是非常重要的,基础不牢,自然不用谈架构。

img_f1bf21ed895c5240d01798638b4814b1.jpe

三、高性能架构

性能一直是让程序员比较头疼的问题。当系统架构变得复杂而庞大之后,性能方面就会下降,特别是阿里巴巴这样的一线互联网公司最为注重,因此想进入阿里,性能优化一定是要去深入学习与理解的一环

img_52e4ba1d785d42055efa17df985b081f.jpe

四、微服务架构

关于微服务架构的取舍

微服务是现在互联网架构技术中最火热的话题之一,也是我目前正在学习研究的方向。在面试过程中,面试官很少会问到关于微服务相关的问题。但作为一名开发者,一名有技术梦想的程序员微服务架构是现在必须要去了解的主流技术:

img_3eb484c2fc1e848485ce8713378e99f0.jpe

五、团队协作:

开发工具工程化

通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具。程序员的战斗,往往不是一个人的战斗,我们如何在一个平台下高效的去重,进行代码review,对功能进行调整,debug,做到在统一的规划下步步为营,混乱的堆代码的过程中找到自己的记录。这一切都依赖于有效的工具。

img_fdcdd088425a0e92a5cdc5938f54c2de.jpe

六、B2C项目实战

项目实战

要想立足于互联网公司,且能在互联网浪潮中不被淹没,对于项目的开发实战演练是不必可少的技能,也是对自身能力的一个衡量,有多少的量对等于获得多少的回报。看似简单的一个项目需求图谱,其中的底层原理,实现原理又能知道多少?你搭建一个完整的B2C项目平台到底需要多少知识?这一切都是需要我们考量的。

img_55174c87a33f3856711ca6d14d9bed41.jpe

以上是小编自己目前在互联网公司用到的java核心技术总结出知识体系思维导。学习是一个复杂的过程,当你拥有了学习的方向和学习的方法时,你缺的只是时间,时间是自己积累出来的,而不是我想学习时说“好像没空”这些借口。不要让今天的借口变成了明天的迷茫!

针对上面的六大技术知识模块我总结一些架构资料和面试题免费分享给大家,希望能帮助到那些工作了的朋友在学习能提供一些帮助。有需要这些免费架构资料和面试题资料的可以加群:960439918获取哦!点击链接加入群聊【java高级架构交流群】:https://jq.qq.com/?_wv=1027&k=5fozFzF

img_700ceedf7eec29ba94b89a5b31ff73df.jpe
img_d2cdd6b67a0f95486c3c33a8e3abd264.jpe
原文链接:https://yq.aliyun.com/articles/685254
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章