从MyBatis3.4.5中的PropertyNamer看属性名称定义

MyBatis属性名映射陷阱解析

先来看代码:

(mybatis-3.4.5.jar)

/**
 *    Copyright 2009-2015 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.apache.ibatis.reflection.property;

import java.util.Locale;

import org.apache.ibatis.reflection.ReflectionException;

/**
 * @author Clinton Begin
 */
public final class PropertyNamer {

  private PropertyNamer() {
    // Prevent Instantiation of Static Class
  }

  public static String methodToProperty(String name) {
    if (name.startsWith("is")) {
      name = name.substring(2);
    } else if (name.startsWith("get") || name.startsWith("set")) {
      name = name.substring(3);
    } else {
      throw new ReflectionException("Error parsing property name '" + name + "'.  Didn't start with 'is', 'get' or 'set'.");
    }

    if (name.length() == 1 || (name.length() > 1 && !Character.isUpperCase(name.charAt(1)))) {
      name = name.substring(0, 1).toLowerCase(Locale.ENGLISH) + name.substring(1);
    }

    return name;
  }

  public static boolean isProperty(String name) {
    return name.startsWith("get") || name.startsWith("set") || name.startsWith("is");
  }

  public static boolean isGetter(String name) {
    return name.startsWith("get") || name.startsWith("is");
  }

  public static boolean isSetter(String name) {
    return name.startsWith("set");
  }

}

PropertyNamer.methodToProperty(String name)对应的便是setter-getter方法名到属性名的对应转换。

细读methodToProperty代码可知:

  • 对于is开头的方法(通过针对boolean类型),直接取is后面的部分;
  • 对于set/get开头的方法,直接substring(3),即取set/get后面的部分;
  • 如果name长度等于1或者(长度大于1且第二个字母是小写的),将第一个字母转换为小写并拼接后面的部分作为属性名;
  • 其他情况直接返回substring(2)-针对is开头或substring(3)-针对set/get开头;

下面看一个UserInfo类的定义:

package com.example.demo.utils;

import lombok.Data;

@Data
public class UserInfo {
    private String name;
    private String email;
    private Integer uType;

}

可以看到,这里用到了lombok的@Data注解,它生成的uTyper的setter/getter方法长这样:

没错,这就是我们常看到的样子,预期似乎也应该长这样:set/get+第一个字母转换为大写。

这里的重点便是uType这个属性!!!

再通过PropertyNamer.methodToProperty方法的实现可以发现:

getUType/setUType这两个方法返回的属性名是:UType因为第2个字母大写了~~~

    if (name.length() == 1 || (name.length() > 1 && !Character.isUpperCase(name.charAt(1)))) {// 长度大于1且第二个字母小写的情况下进行拼接
      name = name.substring(0, 1).toLowerCase(Locale.ENGLISH) + name.substring(1);
    }

    return name;

这,就是我们用MyBatis写数据库时,明明通过debug发现属性是有值的,但写入数据库时某些字段却总是null的原因:属性名的第一个字母大写+使用了lombok生成的setter/getter方法,导致对应的属性名获取不到!!!

不过解决方法也简单:

1、修改属性名称,第2个字母改为小写,或者要求所有属性的前两个字母必需小写;

2、针对这种特殊的属性,使用idea自带的功能生成setter、getter方法

生成的代码如下:

package com.example.demo.utils;

import lombok.Data;

@Data
public class UserInfo {
    private String name;
    private String email;
    private Integer uType;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Integer getuType() {
        return uType;
    }

    public void setuType(Integer uType) {
        this.uType = uType;
    }

}

这里明显可以看到setuType/getuType和lombok生成的是不一样的(setUType/getUType)。

已踩坑~~~

记录+分享一下:)

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jack_abu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值