Mybatis-ParameterType=Map时的占位符问题

问题重现

需要将以下map的value插入到sql的占位符当中:

map = {
	"Product2.Product2" : "Garden Li"
}

数据:

idname
26Garden Li

mapper interface 如下:

    Student selectByMap(@Param("paramsMap") Map<String, Object> map);

mapper.xml如下:

    <select id="selectByMap" parameterType="java.util.Map" resultMap="BaseResultMap">
        select
        *
        from student
        where name = #{paramsMap.Product2.Product2}
    </select>

入参情形

	Map<String, Object> map = new HashMap<>();
	map.put("Product2.Product2", "Garden Li");
	Student student = studentMapper.selectByMap(map);
	System.out.println(student);

日志结果

==>  Preparing: select * from student where name = ?
==> Parameters: null
<==      Total: 0

结论:异常,并未达到预期结果。

排查过程

根据Mybatis底层实现,优先找到参数处理的类,即ParameterHandler接口的默认实现DefaultParameterHandler:

/**
 * @author Clinton Begin
 * @author Eduardo Macarron
 */
public class DefaultParameterHandler implements ParameterHandler

debug到参数解析类PropertyTokenizer的解析方法:

  public PropertyTokenizer(String fullname) {
    int delim = fullname.indexOf('.');
    if (delim > -1) {
      name = fullname.substring(0, delim);
      children = fullname.substring(delim + 1);
    } else {
      name = fullname;
      children = null;
    }
    indexedName = name;
    delim = name.indexOf('[');
    if (delim > -1) {
      index = name.substring(delim + 1, name.length() - 1);
      name = name.substring(0, delim);
    }
  }

判断出Mybatis不能处理带多个【.】的占位符入参

解决方案

配合Mybatis的原理实现,构造嵌套的map进行正常处理:

	Map<String, Object> innerMap = new HashMap<>();
	innerMap.put("Product2", "Garden Li");
	Map<String, Object> map = new HashMap<>();
	map.put("Product2", innerMap);
	Student student = studentMapper.selectByMap(map);
	System.out.println(student);

日志结果:

==>  Preparing: select * from student where name = ?
==> Parameters: Garden Li(String)
<==    Columns: id, name
<==        Row: 26, Garden Li
<==      Total: 1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值