迭代器模式在MyBatis源码中的应用
迭代器模式在 MyBatis 中也是必不可少的,下面来看 DefaultCursor 类,源码如下。
继续查看 CursorIterator 类的源码实现,它是 DefaultCursor 的一个内部类,实现了 JDK 中的 Iterator 接口,源码如下。
public class DefaultCursor<T> implements Cursor<T> {
...
private final CursorIterator cursorIterator = new CursorIterator();
...
}
DefaultCursor 实现了 Cursor 接口,且定义了一个成员变量 cursorIterator,其类型为 CursorIterator。继续查看 CursorIterator 类的源码实现,它是 DefaultCursor 的一个内部类,实现了 JDK 中的 Iterator 接口,源码如下。
private class CursorIterator implements Iterator<T> {
T object;
int iteratorIndex;
private CursorIterator() {
this.iteratorIndex = -1;
}
public boolean hasNext() {
if (this.object == null) {
this.object = DefaultCursor.this.fetchNextUsingRowBound();
}
return this.object != null;
}
public T next() {
T next = this.object;
if (next == null) {
next = DefaultCursor.this.fetchNextUsingRowBound();
}
if (next != null) {
this.object = null;
++this.iteratorIndex;
return next;
} else {
throw new NoSuchElementException();
}
}
public void remove() {
throw new UnsupportedOperationException("Cannot remove element from Cursor");
}
}
所有教程
- C语言入门
- C语言编译器
- C语言项目案例
- 数据结构
- C++
- STL
- C++11
- socket
- GCC
- GDB
- Makefile
- OpenCV
- Qt教程
- Unity 3D
- UE4
- 游戏引擎
- Python
- Python并发编程
- TensorFlow
- Django
- NumPy
- Linux
- Shell
- Java教程
- 设计模式
- Java Swing
- Servlet
- JSP教程
- Struts2
- Maven
- Spring
- Spring MVC
- Spring Boot
- Spring Cloud
- Hibernate
- Mybatis
- MySQL教程
- MySQL函数
- NoSQL
- Redis
- MongoDB
- HBase
- Go语言
- C#
- MATLAB
- JavaScript
- Bootstrap
- HTML
- CSS教程
- PHP
- 汇编语言
- TCP/IP
- vi命令
- Android教程
- 区块链
- Docker
- 大数据
- 云计算