maybatis MapperMethod(接口类型方法参数转换)

public static class MethodSignature {


    private final boolean returnsMany;
    private final boolean returnsMap;
    private final boolean returnsVoid;
    private final Class<?> returnType;
    private final String mapKey;
    private final Integer resultHandlerIndex;
    private final Integer rowBoundsIndex;
    private final SortedMap<Integer, String> params;
    private final boolean hasNamedParameters;


    public MethodSignature(Configuration configuration, Method method) {
      this.returnType = method.getReturnType();
      this.returnsVoid = void.class.equals(this.returnType);
      this.returnsMany = (configuration.getObjectFactory().isCollection(this.returnType) || this.returnType.isArray());
      this.mapKey = getMapKey(method);
      this.returnsMap = (this.mapKey != null);
      this.hasNamedParameters = hasNamedParams(method);//如果方法中有一个@Param();就返回true
      this.rowBoundsIndex = getUniqueParamIndex(method, RowBounds.class);
      this.resultHandlerIndex = getUniqueParamIndex(method, ResultHandler.class);
      this.params = Collections.unmodifiableSortedMap(getParams(method, this.hasNamedParameters));
      //得到 方法中的形参组成的map; 有如果hasNamedParameters为true,那么对全部都按照 true的逻辑去处理Map
     //,但是如果真的找到了@Param(”name“);  这个map的key为参数的下标(0、1....),value为 "name";
      //找不到的话 value等于key;举例:
      //ps:List User select(@Param('sex') String sex,Integer age)  这个map就为:
        //{
        //  0:"sex",
        //  1:1
         //}
    }


    public Object convertArgsToSqlCommandParam(Object[] args) {
    //一下三种情况:
    //1.入参为null或没有时,参数转换为null
    //2.没有使用@Param 注解并且只有一个参数时,返回这一个参数
    //3.使用了@Param 注解或有多个参数时,将参数转换为Map1类型,并且还根据参数顺序存储了key为param1,param2的参数。
      final int paramCount = params.size();
      if (args == null || paramCount == 0) {
        return null;
      } else if (!hasNamedParameters && paramCount == 1) {
        return args[params.keySet().iterator().next().intValue()];
      } else {
        final Map<String, Object> param = new ParamMap<Object>();
        int i = 0;
        for (Map.Entry<Integer, String> entry : params.entrySet()) {
          param.put(entry.getValue(), args[entry.getKey().intValue()]);
          // issue #71, add param names as param1, param2...but ensure backward compatibility
          final String genericParamName = "param" + String.valueOf(i + 1);
          if (!param.containsKey(genericParamName)) {
            param.put(genericParamName, args[entry.getKey()]);
          }
          i++;
        }
        return param;
      }
    }
Mybatis可以使用`foreach`标签来实现传入list参数更新多个字段的操作。首先,你需要在`mapper.xml`文件编写一个`update`语句,例如: ```xml <update id="updateFields" parameterType="java.util.List"> update my_table <set> <foreach collection="list" item="item" separator=","> ${item.field} = #{item.value} </foreach> </set> where id = #{id} </update> ``` 在这个`update`语句,我们使用了`foreach`标签来遍历传入的list参数。`collection`属性指定了传入的list参数的名称,`item`属性指定了每个元素在`foreach`块的别名。`${item.field}`表示要更新的字段名称,`#{item.value}`表示要更新的字段值。`separator`属性指定了每个字段之间的分隔符。 在Java代码,你可以将要更新的字段和对应的值封装为一个`List`对象,例如: ```java List<Map<String, Object>> fields = new ArrayList<>(); Map<String, Object> field1 = new HashMap<>(); field1.put("field", "name"); field1.put("value", "张三"); fields.add(field1); Map<String, Object> field2 = new HashMap<>(); field2.put("field", "age"); field2.put("value", 20); fields.add(field2); ``` 然后,你可以调用Mybatis的`update`方法执行更新操作,例如: ```java SqlSession sqlSession = sqlSessionFactory.openSession(); try { MyMapper mapper = sqlSession.getMapper(MyMapper.class); mapper.updateFields(fields, 1); sqlSession.commit(); } finally { sqlSession.close(); } ``` 这个例子,我们传入了要更新的字段列表`fields`和要更新的记录的ID。Mybatis会将`fields`传递给`updateFields`语句的`list`参数,并根据ID更新对应的记录的字段值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值