mybatis源码第1天------SqlSession
好久没有写日记了。。。。。。。。今天难得心情不错,写篇吧。哈哈、
前几天,一直 部署SpringCloud,,部署了五天,没有部署起来。。。哈哈哈,,,,笨死+笑死自己了。
屁话不多放了。给室友带了花生毛豆,赶紧写完回去。。。。
---------------------------------------------------------------------------------------------------------------
1. 整理一下看Mybatis源码的顺序。
2.注意下Mybatis的编码风格,试着总结下。
3.注意编码过程中使用了 什么设计模式。有什么好处。。
4.总结下自己没遇到的知识点,顺便学习,掌握。
----------------------------------------------------------------------------------------------------------------
好,Mybatis基于3.4.7版本。
附上gitee地址:https://gitee.com/fangjiaxiaobai/learm-mybatis-3,如果存在问题,欢迎大家提Issues。
网上也有很多大哥翻译了,这个是我自己学习,想试着自己翻译学习的。欢迎喷子。
俗气的开始吧,首先介绍下Mybatis的定位,
Mybatis是一款优秀的持久层框架,它支持定制化Sql,存储过程以及高级映射,Mybatis避免了几乎所有的JDBC代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。
为什么要说这个呢,因为我想看源码的时候,跟着这个思路去看,它是怎么实现像定制sql,到数据库执行,怎么定制存储过程,怎么获取结果集,怎么实现的XML或者注解配置,等等。
首先我们看一下Mybatis的组件。
+ SqlSessionFactoryBuilder(构造器):它会根据配置信息或者代码生成SqlSessionFactory(工厂接口)
+ SqlSessionFactory:依靠工厂来生成SqlSession。
+ SqlSession:是一个既可以发送SQL去执行并返回结果的,也可以获取Mapper接口,通过Mapper接口查询并封装数据。
+ SQL Mapper:它是MyBatis新设计的组件,它是由一个Java接口和XML文件(或者注解)构成的,需要给出对应的SQL和映射规则。它负责发送SQL去执行,并返回结果。
上面的图片就是说明了这四个核心组件的工作流程。
Mybatis的核心入口是SqlSession。由于第一次看Mybatis的源码。不知道先看什么后看什么,我就先看了SqlSession这个接口。掐头去尾,看了个中间的。真的服自己。SqlSession接口,定义了一些我们对数据库CRUD操作和一些必要的方法。后面我会附上源码。
SqlSession这个接口有什么作用?
我们可以通过这个接口来执行对数据库的命令,获取映射器和管理事务。(等会看到了源码就知道了)
接下来就附上源码了。
package org.apache.ibatis.session;
import org.apache.ibatis.cursor.Cursor;
import org.apache.ibatis.executor.BatchResult;
import java.io.Closeable;
import java.sql.Connection;
import java.util.List;
import java.util.Map;
/**
*
*The primary Java interface for working with MyBatis.
* java程序使用mybatis的主要接口。
*Through this interface you can execute commands, get mappers and manage transactions.
* 通过这个接口执行命令,获取映射器,管理事务
*
* 要说一下:Closeable是一个数据的来源或者目的地是可以被关闭掉的。
* 调用close方法,来释放对象所在的资源。比如打开文件。
*
* @author Clinton Begin
* @date 2018-06-07
*/
public interface SqlSession extends Closeable {
/**
* 根据声明的key检索出一行
* 也就是说根据你传入的sql语句查出来对应的元组(关系型数据库)。
* 比如: select * from t_table_name where id='1'
* Retrieve a single row mapped from the statement key
* @param <T> the returned object type
* @param statement
* @return Mapped object
*/
<T> T selectOne(String statement);
/**
* 也就是说根据你传入的sql语句查出来对应的元组(关系型数据库)。
* 比如:select * from t_table_name where id=?,1
* Retrieve a single row mapped from the statement key and parameter.
* @param <T> the returned object type
* @param statement Unique identifier matching the statement to use.
* @param parameter A parameter object to pass to the statement.
* @return Mapped object
*/
<T> T selectOne(String statement, Object parameter);
/**
* Retrieve a list of mapped objects from the statement key and parameter.
* 根据sql检索出符合条件的元组列表
*
* @param <E> the returned list element type
* @param statement Unique identifier matching the statement to use.
* @return List of mapped object
*/
<E> List<E> selectList(String statement);
/**
* Retrieve a list of mapped objects from the statement key and parameter.
* 根据sql检索出符合条件的元组列表
* 比如:select * from t_table_name where column1=? and column2=?, parameter1 , parameter2
* @param <E> the returned list element type
* @param statement Unique identifier matching the statement to use.
* @param parameter A parameter object to pass to the statement.
* @return List of mapped object
*/
<E> List<E> selectList(String statement, Object parameter);
/**
* Retrieve a list of mapped objects from the statement key and parameter,
* within the specified row bounds.
* 在指定的行界中 检索符合条件的元组列表
* 比如:select * from t_table_name where column1=? and column2=? limit offset,limit
*
* @param <E> the returned list element type
* @param statement Unique identifier matching the statement to use.
* @param parameter A parameter object to pass to the statement.
* @param rowBounds Bounds to limit object retrieval
* @return List of mapped object
*/
<E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds);
/**
* The selectMap is a special case in that it is designed to convert a list
* of results into a Map based on one of the properties in the resulting
* objects.
* 它被设计为转换结果的列表到基于结果对象中一个属性的map中.
* 获取多条记录,,, 存入Map
* Eg. Return a of Map[Integer,Author] for selectMap("selectAuthors","id")
* @param <K> the returned Map keys type
* @param <V> the returned Map values type
* @param statement Unique identifier matching the statement to use.
* @param mapKey The property to use as key for each value in the list.
* @return Map containing key pair data.
*/
<K, V> Map<K, V> selectMap(String statement, String mapKey);
/**
* The selectMap is a special case in that it is designed to convert a list
* of results into a Map based on one of the properties in the resulting
* objects.
*
* 它被设计为转换结果的列表到基于结果对象中一个属性的map中.
* 获取多条记录,,, 存入Map
*
* @param <K> the returned Map keys type
* @param <V> the returned Map values type
* @param statement Unique identifier matching the statement to use.
* @param parameter A parameter object to pass to the statement.
* @param mapKey The property to use as key for each value in the list.
* @return Map containing key pair data.
*/
<K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey);
/**
* The selectMap is a special case in that it is designed to convert a list
* of results into a Map based on one of the properties in the resulting
* objects.
*
* 它被设计为转换结果的列表到基于结果对象中一个属性的map中.
* 获取多条记录,,, 存入Map, 可以分页
*
* @param <K> the returned Map keys type
* @param <V> the returned Map values type
* @param statement Unique identifier matching the statement to use.
* @param parameter A parameter object to pass to the statement.
* @param mapKey The property to use as key for each value in the list.
* @param rowBounds Bounds to limit object retrieval
* @return Map containing key pair data.
*/
<K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds);
/**
* A Cursor offers the same results as a List, except it fetches data lazily using an Iterator.
* 游标提供与列表相同的结果,不同之处在于它使用迭代器懒散地提取数据。
* @param <T> the returned cursor element type.
* @param statement Unique identifier matching the statement to use.
* @return Cursor of mapped objects
*/
<T> Cursor<T> selectCursor(String statement);
/**
* A Cursor offers the same results as a List, except it fetches data lazily using an Iterator.
* 游标提供与列表相同的结果,不同之处在于它使用迭代器懒散地提取数据。
* @param <T> the returned cursor element type.
* @param statement Unique identifier matching the statement to use.
* @param parameter A parameter object to pass to the statement.
* @return Cursor of mapped objects
*/
<T> Cursor<T> selectCursor(String statement, Object parameter);
/**
* A Cursor offers the same results as a List, except it fetches data lazily using an Iterator.
* @param <T> the returned cursor element type.
* @param statement Unique identifier matching the statement to use.
* @param parameter A parameter object to pass to the statement.
* @param rowBounds Bounds to limit object retrieval
* @return Cursor of mapped objects
*/
<T> Cursor<T> selectCursor(String statement, Object parameter, RowBounds rowBounds);
/**
* Retrieve a single row mapped from the statement key and parameter
* 根据sql获取一个元组,并转交给ResultHandler处理
*
* using a {@code ResultHandler}.
* @param statement Unique identifier matching the statement to use.
* @param parameter A parameter object to pass to the statement.
* @param handler ResultHandler that will handle each retrieved row
*/
void select(String statement, Object parameter, ResultHandler handler);
/**
* 根据sql获取一个元组,并转交给ResultHandler处理
*
* Retrieve a single row mapped from the statement
* using a {@code ResultHandler}.
* @param statement Unique identifier matching the statement to use.
* @param handler ResultHandler that will handle each retrieved row
*/
void select(String statement, ResultHandler handler);
/**
* 根据sql获取一个元组,并转交给ResultHandler处理,可以分页。
* Retrieve a single row mapped from the statement key and parameter
* using a {@code ResultHandler} and {@code RowBounds}
* @param statement Unique identifier matching the statement to use.
* @param rowBounds RowBound instance to limit the query results
* @param handler ResultHandler that will handle each retrieved row
*/
void select(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler);
/**
* 插入,执行insert语句。insert into t_table_name value('1','2',3);
* Execute an insert statement.
* @param statement Unique identifier matching the statement to execute.
* @return int The number of rows affected by the insert.
*/
int insert(String statement);
/**
* 用给定参数对象执行INSERT语句, 任何生成的
* autoincrement值或selectKey条目将修改给定的参数
* 对象属性。返回受影响的行数。
* Execute an insert statement with the given parameter object. Any generated
* autoincrement values or selectKey entries will modify the given parameter
* object properties. Only the number of rows affected will be returned.
* @param statement Unique identifier matching the statement to execute.
* @param parameter A parameter object to pass to the statement.
* @return int The number of rows affected by the insert.
*/
int insert(String statement, Object parameter);
/**
* 执行UPDATE语句。返回受影响的行数。
* Execute an update statement. The number of rows affected will be returned.
* @param statement Unique identifier matching the statement to execute.
* @return int The number of rows affected by the update.
*/
int update(String statement);
/**
* 根据给定的参数,执行UPDATE语句。返回受影响的行数。
* Execute an update statement. The number of rows affected will be returned.
* @param statement Unique identifier matching the statement to execute.
* @param parameter A parameter object to pass to the statement.
* @return int The number of rows affected by the update.
*/
int update(String statement, Object parameter);
/**
* 执行DELETE语句。返回受影响的行数。
* Execute a delete statement. The number of rows affected will be returned.
* @param statement Unique identifier matching the statement to execute.
* @return int The number of rows affected by the delete.
*/
int delete(String statement);
/**
* 根据给定的参数执行DELETE语句。返回受影响的行数
* Execute a delete statement. The number of rows affected will be returned.
* @param statement Unique identifier matching the statement to execute.
* @param parameter A parameter object to pass to the statement.
* @return int The number of rows affected by the delete.
*/
int delete(String statement, Object parameter);
/**
* 提交事务。
* 刷新批量语句,并提交数据库的连接。
* Flushes batch statements and commits database connection.
* 如果没有没有update,delete,insert调用的时候,不会提交连接。
* Note that database connection will not be committed if no updates/deletes/inserts were called.
* 如果要强制提交,调用commit(true);
* To force the commit call {@link SqlSession#commit(boolean)}
*/
void commit();
/**
* 提交事务。
* 刷新批量语句,并提交数据库的连接
* Flushes batch statements and commits database connection.
* @param force forces connection commit
*/
void commit(boolean force);
/**
* 回滚事务。
*放弃挂起的批处理语句并将数据库连接回滚。
* Discards pending batch statements and rolls database connection back.
* 请注意,如果没有更新/删除/插入被调用,数据库连接将不会回滚。
* Note that database connection will not be rolled back if no updates/deletes/inserts were called.
* 强制回滚调用{@link SqlSession#rollback(boolean)}
* To force the rollback call {@link SqlSession#rollback(boolean)}
*/
void rollback();
/**
* 强制调用回滚
* Discards pending batch statements and rolls database connection back.
* Note that database connection will not be rolled back if no updates/deletes/inserts were called.
* @param force forces connection rollback
*/
void rollback(boolean force);
/**
* 刷新批处理语句。
* Flushes batch statements.
* @return BatchResult list of updated records
* @since 3.0.6
*/
List<BatchResult> flushStatements();
/**
* Closes the session
*/
@Override
void close();
/**
* 清空本地会话缓存
* Clears local session cache
*/
void clearCache();
/**
* 检索当前的配置
* Retrieves current configuration
* @return Configuration
*/
Configuration getConfiguration();
/**
* 检索一个Mapper(映射器)
* Retrieves a mapper.
* @param <T> the mapper type
* @param type Mapper interface class
* @return a mapper bound to this SqlSession
*/
<T> T getMapper(Class<T> type);
/**
* 检索内部数据库连接
* Retrieves inner database connection
* @return Connection
*/
Connection getConnection();
}
最后
如果你觉得写的还不错,就关注下公众号呗,关注后,有点小礼物回赠给你。
你可以获得5000+电子书,java,springCloud,adroid,python等各种视频教程,IT类经典书籍,各种软件的安装及破解教程。
希望一块学习,一块进步!