网站首页 > 无锡民生> 文章内容

Mybatis是如何执行一条SQL命令_1

※发布时间:2021-1-28 14:50:49   ※发布作者:habao   ※出自何处: 
 

Mybatis中的Sql命令,在枚举类SqlCommandType中定义的。

public enum SqlCommandType {

UNKNOWN, INSERT, UPDATE, DELETE, SELECT, FLUSH;

}

下面,我们以Mapper接口中的一个方法作为例子,看看Sql命令的执行完整流程。

public interface StudentMapper {

List<Student> findAllStudents(Map<String, Object> map, RowBounds rowBounds, ResultSetHandler rh);

}

参数RowBounds跟ResultSetHandler是可选参数,表现分页对象和自定义成果集处置器,个别不须要。

一个完整的Sql命令,其执行的完整流程图如下:

对上面的流程图,假如看过前面的文章的话,大局部对象咱们都比拟熟习了,Base64编码/解码是网络上最常见的用于传输8Bit字节码的编码方式之一,Base64就是一种基于64个可打印字符来表示二进制数据的方法。可查看RFC2045~RFC2049,上面有MIME的详细规范。一个图,就完全展现了其履行流程。

MapperProxy的功能:

1. 由于Mapper接口不能直接实例化,MapperProxy的作用,就是应用JDK动态代办功能,间接实例化Mapper的proxy对象。可参看系列的第二篇。

2. 缓存MapperMethod对象。

private final Map<Method, MapperMethod> methodCache;

@Override

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

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);

}

// 缓存MapperMethod

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,JS代码压缩是一种具有函数优先的轻量级,解释型或即时编译型的编程语言。虽然它是作为开发Web页面的脚本语言而出名,但是它也被用到了很多非浏览器环境中,JavaScript 基于原型编程、多范式的动态脚本语言,并且支持面向对象、命令式和声明式(如函数式编程)风格;

}

MapperMethod的功效:

1. 解析Mapper接口的方式,并封装成MapperMethod对象。

2. 将Sql命令,准确路由到适当的SqlSession的办法上。

public class MapperMethod {

// 保留了Sql命令的类型和键id

private final SqlCommand command;

// 保存了Mapper接口方法的解析信息

private final MethodSignature method;

public MapperMethod(Class<?> mapperInterface, Method method,JavaScript代码格式化是一种属于网络的高级脚本语言,已经被广泛用于Web应用开发,常用来为网页添加各式各样的动态功能,为用户提供更流畅美观的浏览效果。通常JavaScript脚本是通过嵌入在HTML中来实现自身的功能的, Configuration config) {

this.command = new SqlCommand(config, mapperInterface, method);

this.method = new MethodSignature(config, method);

}

// 依据解析结果,路由到恰当的SqlSession方法上

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

Object result;

if (SqlCommandType.INSERT == command.getType()) {

Object param = method.convertArgsToSqlCommandParam(args);

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

} else if (SqlCommandType.UPDATE == command.getType()) {

Object param = method.convertArgsToSqlCommandParam(args);

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

} else if (SqlCommandType.DELETE == command.getType()) {

Object param = method.convertArgsToSqlCommandParam(args);

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

} else if (SqlCommandType.SELECT == command.getType()) {

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 {

Object param = method.convertArgsToSqlCommandParam(args);

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

}

} else if (SqlCommandType.FLUSH == command.getType()) {

result = sqlSession.flushStatements();

} else {

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() + ").");

}

 
关键词:
相关阅读
  • 没有资料