mybatis部分源码解析
程序员文章站
2022-03-26 08:43:44
...
当执行INSERT,UPDATE,DELETE类型的sql语句时,其执行结果都需要经过MapperMethod.rowCountResult()方法处理。sqlSession中的inert(),update(),delete()等方法返回的是int值,rowCountResult()方法会将该int值转换为Mapper接口中对应方法的返回值,其具体实现如下:
private Object rowCountResult(int rowCount) {
Object result;
if (this.method.returnsVoid()) {
result = null;
} else {
Object result;
if ((Integer.class.equals(this.method.getReturnType())) || (Integer.TYPE.equals(this.method.getReturnType()))) {
result = Integer.valueOf(rowCount);
} else {
Object result;
if ((Long.class.equals(this.method.getReturnType())) || (Long.TYPE.equals(this.method.getReturnType()))) {
result = Long.valueOf(rowCount);
} else {
Object result;
if ((Boolean.class.equals(this.method.getReturnType())) || (Boolean.TYPE.equals(this.method.getReturnType())))
result = Boolean.valueOf(rowCount > 0);
else
throw new BindingException("Mapper method '" + this.command.getName() + "' has an unsupported return type: " + this.method.getReturnType());
}
}
}
Object result;
return result;
}
异常信息:Expected one result (or null) to be returned by selectOne(), but found:2,原因:返回数据类型为单个对象,但查询结果查到不止一个。
if (SqlCommandType.SELECT == this.command.getType()) {
Object result;
if ((this.method.returnsVoid()) && (this.method.hasResultHandler())) {
//如果Mapper接口中方法准备使用ResultHandler处理查询结果集,则...(当使用ResultHandler处理结果集时必须指定ResultMap或ResultType)
executeWithResultHandler(sqlSession, args);
result = null;
} else {
Object result;
//如果Mapper接口中对应方法的返回值为数组或Collection接口实现类,则通过MapperMethod.executeForMany()方法处理
if (this.method.returnsMany()) {
result = executeForMany(sqlSession, args);
} else {
Object result;
//如果Mapper接口中对应方法的返回值为Map类型,则..
if (this.method.returnsMap()) {
result = executeForMap(sqlSession, args);
} else {
Object param = this.method.convertArgsToSqlCommandParam(args);
result = sqlSession.selectOne(this.command.getName(), param);
}
}
}
}
public <T> T selectOne(String statement, Object parameter) {
List list = selectList(statement, parameter);
if (list.size() == 1)
return list.get(0);
if (list.size() > 1) {
throw new TooManyResultsException("Expected one result (or null) to be returned by selectOne(), but found: " + list.size());
}
return null;
}
绑定Mapper接口
- XMLMapperBuilder.bindMapperForNamespace()方法完成了映射配置文件与对应Mapper接口的绑定。
- 每个配置文件的命名空间可以绑定一个Mapper接口,并注册到MapperRegistry中。
private void bindMapperForNamespace() {
//获取映射配置文件的命名空间
String namespace = this.builderAssistant.getCurrentNamespace();
if (namespace != null) {
Class boundType = null;
try {
//解析命名空间对应的类型
boundType = Resources.classForName(namespace);
} catch (ClassNotFoundException e) {
}
//是否已经加载了boundType接口
if ((boundType == null) || (this.configuration.hasMapper(boundType))) {
return;
}
//追加namespace前缀,并添加到configuration.LoadedResource集合中保存
this.configuration.addLoadedResource("namespace:" + namespace);
//调用MapperRegistry.addMapper()方法,注册boundType接口
this.configuration.addMapper(boundType);
}
}
上一篇: C#操作XML通用方法汇总