zoukankan      html  css  js  c++  java
  • mybatis查询语句的背后之参数解析

     转载请注明出处。。。

    一、前言

    通过前面我们也知道,通过getMapper方式来进行查询,最后会通过mapperMehod类,对接口中传来的参数也会在这个类里面进行一个解析,随后就传到对应位置,与sql里面的参数进行一个匹配,最后获取结果。对于mybatis通常传参(这里忽略掉Rowbounds和ResultHandler两种类型)有几种方式。

    1、javabean类型参数

    2、非javabean类型参数

    注意,本文是基于mybatis3.5.0版本进行分析。

    1、参数的存储

    2、对sql语句中参数的赋值

    下面将围绕这这两方面进行

    二、参数的存储

    先看下面一段代码

     1     @Test
     2     public void testSelectOrdinaryParam() throws Exception{
     3         SqlSession sqlSession = MybatisUtil.getSessionFactory().openSession();
     4         UserMapper mapper = sqlSession.getMapper(UserMapper.class);
     5         List<User> userList = mapper.selectByOrdinaryParam("张三1号");
     6         System.out.println(userList);
     7         sqlSession.close();
     8     }
     9     List<User> selectByOrdinaryParam(String username);  // mapper接口
    10     <select id="selectByOrdinaryParam" resultMap="BaseResultMap">
    11         select
    12         <include refid="Base_Column_List"/>
    13         from user
    14         where username = #{username,jdbcType=VARCHAR}
    15     </select>

     或许有的人会奇怪,这个mapper接口没有带@Param注解,怎么能在mapper配置文件中直接带上参数名呢,不是会报错吗,

    在mybatis里面,对单个参数而言,直接使用参数名是没问题的,如果是多个参数就不能这样了,下面我们来了解下,mybatis的解析过程,请看下面代码,位于MapperMehod类的内部类MethodSignature构造函数中

     1 public MethodSignature(Configuration configuration, Class<?> mapperInterface, Method method) {
     2       Type resolvedReturnType = TypeParameterResolver.resolveReturnType(method, mapperInterface);
     3       if (resolvedReturnType instanceof Class<?>) {
     4         this.returnType = (Class<?>) resolvedReturnType;
     5       } else if (resolvedReturnType instanceof ParameterizedType) {
     6         this.returnType = (Class<?>) ((ParameterizedType) resolvedReturnType).getRawType();
     7       } else {
     8         this.returnType = method.getReturnType();
     9       }
    10       this.returnsVoid = void.class.equals(this.returnType);
    11       this.returnsMany = configuration.getObjectFactory().isCollection(this.returnType) || this.returnType.isArray();
    12       this.returnsCursor = Cursor.class.equals(this.returnType);
    13       this.returnsOptional = Optional.class.equals(this.returnType);
    14       this.mapKey = getMapKey(method);
    15       this.returnsMap = this.mapKey != null;
    16       this.rowBoundsIndex = getUniqueParamIndex(method, RowBounds.class);
    17       this.resultHandlerIndex = getUniqueParamIndex(method, ResultHandler.class);
    18      // 参数解析类
    19       this.paramNameResolver = new ParamNameResolver(configuration, method);
    20     }

     参数的存储解析皆由ParamNameResolver类来进行操作,先看下该类的构造函数

     1 /**
     2  * config 全局的配置文件中心
     3  * method 实际执行的方法,也就是mapper接口中的抽象方法
     4  * 
     5  */
     6 public ParamNameResolver(Configuration config, Method method) {
     7     // 获取method中的所有参数类型
     8     final Class<?>[] paramTypes = method.getParameterTypes();
     9     // 获取参数中含有的注解,主要是为了@Param注解做准备
    10     final Annotation[][] paramAnnotations = method.getParameterAnnotations();
    11     final SortedMap<Integer, String> map = new TreeMap<>();
    12     // 这里实际上获取的值就是参数的个数。也就是二维数组的行长度
    13     int paramCount = paramAnnotations.length;
    14     // get names from @Param annotations
    15     for (int paramIndex = 0; paramIndex < paramCount; paramIndex++) {
    16       // 排除RowBounds和ResultHandler两种类型的参数
    17       if (isSpecialParameter(paramTypes[paramIndex])) {
    18         // skip special parameters
    19         continue;
    20       }
    21       String name = null;
    22       // 如果参数中含有@Param注解,则只用@Param注解的值作为参数名
    23       for (Annotation annotation : paramAnnotations[paramIndex]) {
    24         if (annotation instanceof Param) {
    25           hasParamAnnotation = true;
    26           name = ((Param) annotation).value();
    27           break;
    28         }
    29       }
    30       // 即参数没有@Param注解
    31       if (name == null) {
    32         // 参数实际名称,其实这个值默认就是true,具体可以查看Configuration类中的该属性值,当然也可以在配置文件进行配置关闭
    33         // 如果jdk处于1.8版本,且编译时带上了-parameters 参数,那么获取的就是实际的参数名,如methodA(String username)
    34         // 获取的就是username,否则获取的就是args0  后面的数字就是参数所在位置
    35         if (config.isUseActualParamName()) {
    36           name = getActualParamName(method, paramIndex);
    37         }
    38         // 如果以上条件都不满足,则将参数名配置为 0,1,2../
    39         if (name == null) {
    40           // use the parameter index as the name ("0", "1", ...)
    41           // gcode issue #71
    42           name = String.valueOf(map.size());
    43         }
    44       }
    45       map.put(paramIndex, name);
    46     }
    47     names = Collections.unmodifiableSortedMap(map);
    48   }

     这个构造函数的作用就是对参数名称进行一个封装,得到一个  “参数位置-->参数名称 “ 的一个map结构,这样做的目的是为了替换参数值,我们也清楚,实际传过来的参数就是一个一个Object数组结构,我们也可以将它理解为map结构。即 index --> 参数值,此就和之前的 map结构有了对应,也就最终可以得到一个 参数名称  --->  参数值 的一个对应关系。

     1 public Object execute(SqlSession sqlSession, Object[] args) {
     2     Object result;
     3     switch (command.getType()) {
     4       // 其它情况忽略掉
     5       case SELECT:
     6         // 这里参数中含有resultHandler,暂不做讨论
     7         if (method.returnsVoid() && method.hasResultHandler()) {
     8           executeWithResultHandler(sqlSession, args);
     9           result = null;
    10         } else if (method.returnsMany()) {// 1、 返回结果为集合类型或数组类型,这种情况适用于大多数情况
    11           result = executeForMany(sqlSession, args);
    12         } else if (method.returnsMap()) {// 返回结果为Map类型
    13           result = executeForMap(sqlSession, args);
    14         } else if (method.returnsCursor()) {
    15           result = executeForCursor(sqlSession, args);
    16         } else {// 2、返回结果javabean类型,或普通的基础类型及其包装类等  
    17           Object param = method.convertArgsToSqlCommandParam(args);
    18           result = sqlSession.selectOne(command.getName(), param);
    19           // 对java8中的optional进行了支持
    20           if (method.returnsOptional() &&
    21               (result == null || !method.getReturnType().equals(result.getClass()))) {
    22             result = Optional.ofNullable(result);
    23           }
    24         }
    25         break;
    26       default:
    27         throw new BindingException("Unknown execution method for: " + command.getName());
    28     }
    29     if (result == null && method.getReturnType().isPrimitive() && !method.returnsVoid()) {
    30       throw new BindingException("Mapper method '" + command.getName()
    31           + " attempted to return null from a method with a primitive return type (" + method.getReturnType() + ").");
    32     }
    33     return result;
    34   }

    这里主要分析1情况。对于2情况也就是接下来要说的参数赋值情况,不过要先介绍下method.convertArgsToSqlCommandParam这代码带来的一个结果是怎么样的

     1 public Object convertArgsToSqlCommandParam(Object[] args) {
     2       return paramNameResolver.getNamedParams(args);
     3     }
     4 
     5 public Object getNamedParams(Object[] args) {
     6     final int paramCount = names.size();
     7     if (args == null || paramCount == 0) {
     8       return null;
     9     } else if (!hasParamAnnotation && paramCount == 1) {// 1
    10       return args[names.firstKey()];
    11     } else {
    12       final Map<String, Object> param = new ParamMap<>();
    13       int i = 0;
    14       for (Map.Entry<Integer, String> entry : names.entrySet()) {
    15         param.put(entry.getValue(), args[entry.getKey()]);
    16         // add generic param names (param1, param2, ...)
    17         final String genericParamName = GENERIC_NAME_PREFIX + String.valueOf(i + 1);
    18         // ensure not to overwrite parameter named with @Param
    19         if (!names.containsValue(genericParamName)) {
    20           param.put(genericParamName, args[entry.getKey()]);
    21         }
    22         i++;
    23       }
    24       return param;
    25     }
    26   }

    可以很清楚的知道最后又调用了ParamNameResolver类的getNamedPaams方法,这个方法的主要作用就是,将原来的参数位置 -->  参数名称  映射关系转为  参数名称 --->参数值 ,并且新加一个参数名和参数值得一个对应关系。即

    param1  ->参数值1

    param2 -->参数值2 

    当然如果只有一个参数,如代码中的1部分,若参数没有@Param注解,且只有一个参数,则不会加入上述的一个对象关系,这也就是前面说的,对于单个参数,可以直接在sql中写参数名就ok的原因。下面回到前面

     1 private <E> Object executeForMany(SqlSession sqlSession, Object[] args) {
     2     List<E> result;
     3     // 获取对应的一个映射关系,param类型有可能为map或null或参数实际类型
     4     Object param = method.convertArgsToSqlCommandParam(args);
     5     if (method.hasRowBounds()) {
     6       RowBounds rowBounds = method.extractRowBounds(args);
     7       result = sqlSession.<E>selectList(command.getName(), param, rowBounds);
     8     } else {
     9       result = sqlSession.<E>selectList(command.getName(), param);
    10     }
    11     // 如果返回结果类型和method的返回结果类型不一致,则进行转换数据结构
    12     // 其实就是result返回结果不是List类型,而是其他集合类型或数组类型
    13     if (!method.getReturnType().isAssignableFrom(result.getClass())) {
    14       if (method.getReturnType().isArray()) {// 为数组结果
    15         return convertToArray(result);
    16       } else {// 其他集合类型
    17         return convertToDeclaredCollection(sqlSession.getConfiguration(), result);
    18       }
    19     }
    20     return result;
    21   }

    代码也不复杂,就是将得到的参数对应关系传入,最终获取结果,根据实际需求进行结果转换。

    3、对sql语句中参数的赋值

     其实前面一篇博客中也有涉及到。参数赋值的位置在DefaultParameterHandler类里面,可以查看前面一篇博客,这里不做过多介绍,传送门  mybatis查询语句的背后之封装数据

    ---------------------------------------------------------------------------------------------------------------------------------------分界线--------------------------------------------------------------------------------------------------------

    若有不足或错误之处,还望指正,谢谢!

  • 相关阅读:
    hdu 4496 D-City 并查集
    hdu 4493 Tutor 水题
    codeforces 377A. Puzzles 水题
    hdu 1257 小希的迷宫 并查集
    图论500题
    cdoj 93 King's Sanctuary 傻逼几何题
    cdoj 题目简单分类
    cdoj 80 Cube 水题
    cdoj 71 I am Lord Voldemort 水题
    cdoj 65 CD Making 水题
  • 原文地址:https://www.cnblogs.com/qm-article/p/10658527.html
Copyright © 2011-2022 走看看