mybatis使用记录 --lombok--springboot

138 阅读3分钟

新人一枚,最近在使用springboot期间整合mybatis和mybatis-plus总是出现各种各样的问题,还有lombok,特花费时间记录,如果有帮到其他兄弟,也是我的荣幸,不说废话,直接开搞

一.lombok+mybatis getset方法不同的问题

数据库

image.png

实体类

image.png

因为怕出现意外情况,特地用的驼峰,也专门开启了驼峰命名设置在yml里,

但是还是在将数据发送至前端时,pId和pName却拿不到数据,其他的数据都有,

通过conslo.log发现两个数据名都变成pid和pname,

查找了好多原因后,发现 Lombok对于第一个字母小写,第二个字母大写的属性生成的get-set方法 和Mybatis以及idea或者说是Java官方认可的get-set方法生成的不一样:

Lombok生成的Get-Set方法

@Data
public class NMetaVerify {
    private Long id;
    private NMetaType nMetaType;
    private Date createTime;
    
    public void lombokFound(){
        NMetaVerify nMetaVerify = new NMetaVerify();
        nMetaVerify.setNMetaType(NMetaType.TWO); //注意:nMetaType的
        nMetaVerify.getNMetaType();              //set方法为setNMetaType,
                                                 //第一个n字母大写了,
                                                 //getxxxx方法也是大写
    }
}

idea,Mybatis,Java官方默认的行为为:

public class NMetaVerify {
    private Long id;
    private NMetaType nMetaType;
    private Date createTime;


    public Long getId() {
        return id;
    }


    public void setId(Long id) {
        this.id = id;
    }


    public NMetaType getnMetaType() {//注意:nMetaType属性的第一个字母小写
        return nMetaType;
    }


    public void setnMetaType(NMetaType nMetaType) {//注意:nMetaType属性的第一个字母小写
        this.nMetaType = nMetaType;
    }


    public Date getCreateTime() {
        return createTime;
    }


    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
  

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

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")) {//is开头的一般是bool类型,直接从第二个(索引)开始截取(简单粗暴)
          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 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");

Mybatis解析get-set方法为属性名字测试

@Testpublic void foundPropertyNamer() 
{String isName = "isName";
String getName = "getName";
String getnMetaType = "getnMetaType";
String getNMetaType = "getNMetaType";
Stream.of(isName,getName,getnMetaType,getNMetaType)
.forEach(methodName->System.out.println("方法名字是:"+methodName+" 属性名字:"+ PropertyNamer.methodToProperty(methodName)));}  

输出结果如下:

方法名字是:isName 属性名字:name方法名字是:getName 属性名字:name方法名字是:getnMetaType 属性名字:nMetaType //这个以及下面的属性第二个字母都是大写,所以直接返回name
方法名字是:getNMetaType 属性名字:NMetaType

解决方案

  • 1.修改属性名字,让第二个字母小写,或者说是规定所有的属性的前两个字母必须小写
  • 2.如果数据库已经设计好,并且前后端接口对接好了,不想修改,那就专门为这种特殊的属性使用idea生成get-set方法复制代码

具体内容转自Lombok踩坑_lombok.config 首字母小写、第二个字母大写-CSDN博客

绝对尊重原版,只是记录,如有冒犯,请及时提醒

二.mybatis-plus+mybatis 整合的问题

我用的是springboot3,添加两个jar包时,如果图方便,只加mybatis-plus,它自带的默认mybatis jar包的版本是2.0.7

image.png

会报出以下错误

image.png

可以自己配置一个mybati或者配置成springboot2