IT袋

当前位置:主页 > 经验教程 > 建站编程 >

lombok详解

lombok详解 深入解析Lombok框架(3)

时间:2023-12-31 10:59:52 来源:IT袋 作者:马勇
导读:lombok详解,【步骤四】:使用POST发送请求 GET http://localhost:8899/user [ { "id": 1740047068249387009, "name": "zbbmeta", "age": 18, "sname": null }, { "id": 1740047068350050306, "name": "springbootIT袋

lombok详解

【步骤四】:使用POST发送请求 GEThttp://localhost:8899/user

[
    {
        "id": 1740047068249387009,
        "name": "zbbmeta",
        "age": 18,
        "sname": null
    },
    {
        "id": 1740047068350050306,
        "name": "springbootIT袋网",
        "age": 20,
        "sname": null
    },
    {
        "id": 1740047068350050307,
        "name": "springbootIT袋网2",
        "age": 22,
        "sname": null
    }
]

lombok详解

4.lombok存在问题

发现uese类的sName属性没有进行赋值

原因

Lombok对于第一个字母小写,第二个字母大写的属性生成的get-set方法和Mybatis以及想法还是说是Java官方认可的get-set方法生成的不一样

lombok生成的User对象

public class User
    implements Serializable
{
    private Long id;
    private String name;
    private Integer age;
    private String sName;
    private static final long serialVersionUID = 1L;
    
    public void setId(Long id) {
        this.id = id; } 
 public void setName(String name) { this.name = name; } 
 public void setAge(Integer age) { this.age = age; } 
 public void setSName(String sName) { this.sName = sName; }
     public Long getId() {
        return this.id;
    } public String getName() {
        return this.name;
    } public Integer getAge() {
        return this.age;
    } public String getSName() {
        return this.sName;
    }

发现sName的set-get方法如下setSName和getSName不是我们想的setsName和getsName

Mybatis(3.4.6版本)解析get-set方法获取属性名字的源码:

/**
 *    Copyright 2009-2019 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) {
    //is开头的一般是bool类型,直接从第二个(索引)开始截取(简单粗暴)
    if (name.startsWith("is")) {
      name = name.substring(2);
    } else if (name.startsWith("get") || name.startsWith("set")) {//set-get的就从第三个(索引)开始截取
      name = name.substring(3);
    } else {
      throw new ReflectionException("Error parsing property name '" + name + "'.  Didn't start with 'is', 'get' or 'set'.");
    }
 //下面这个判断很重要,解释如下
            //第一句话:name.length()==1
            //       对于属性只有一个字母的,例如private int x;
            //          对应的get-set方法是getX();setX(int x);
            //第二句话:name.length() > 1 && !Character.isUpperCase(name.charAt(1)))
            //属性名字长度大于1,并且第二个(代码中的charAt(1),这个1是数组下标)字母是小写的
            //      如果第二个char是大写的,那就直接返回name
    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 isGetter(name) || isSetter(name);
  }
  public static boolean isGetter(String name) {
    return (name.startsWith("get") && name.length() > 3) || (name.startsWith("is") && name.length() > 2);
  }
  public static boolean isSetter(String name) {
    return name.startsWith("set") && name.length() > 3;
  }
}

相关阅读

  • 个域网 PAN的特点有哪些

    个域网 PAN的特点有哪些

    小编为大家说一说个域网 PAN的特点有哪些的电脑方面的小经验,具体介绍如下: 个域网(Personal Area Network)是最小范围的网络类型,通常仅涵盖个人设备的连接。 特点 极小范围: PAN覆盖的范

  • 什么是双机热备? 双机热备是什么意思?

    什么是双机热备? 双机热备是什么意思?

    全面的为大家介绍什么是双机热备的相关介绍,如有不对的地方欢迎指正! 双机热备是一种通过在网络设备之间建立 冗余 的、 实时同步 的备份系统,以实现在主设备故障时无缝切换到备用设

  • QOS三种服务模型 QOS模型有哪几种

    QOS三种服务模型 QOS模型有哪几种

    您可能不了解QOS三种服务模型的方法内容,相关内容具体如下: 在网络中实施QOS时,有三种模型可供参考,这三种模型并不是QOS技术,而是用来指导在各种需求下,如何实施QOS,分为以下三种

  • 数据库系统有哪些特点 简单数据库设计案例

    数据库系统有哪些特点 简单数据库设计案例

    小编为你介绍数据库系统有哪些特点和简单数据库设计案例方面的讲解,一起来看看吧! 数据库系统概论(第5版)第1版于1983年出版,至今已修订至第5版。《数据库系统概论(第5版)》系统