mybatis部分源码解析

当执行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接口

  1. XMLMapperBuilder.bindMapperForNamespace()方法完成了映射配置文件与对应Mapper接口的绑定。
  2. 每个配置文件的命名空间可以绑定一个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);
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本方法。编译原理不仅是计算机科学理论的重要组成部分,也是实现高效、可靠的计算机程序设计的关键。本文将对编译原理的基本概念、发展历程、主要内容和实际应用进行详细介绍编译原理是计算机专业的一门核心课程,旨在介绍编译程序构造的一般原理和基本

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值