Do not add is prefix to any Boolean variable in Java class

Preface: In the Ali development manual, there is a development specification that is mandatory: any Boolean variable in the POJO class should not be prefixed with is.

Also in the Alibaba development manual, in the first item of the table creation agreement in the MySQL specification, the variable expressing yes or no adopts the naming method of is_xxx.

Many people are confused. Mybatis or mybatis-plus integrated in our production project, if the properties of the pojo entity object are different from the table fields,

How can it be mapped? Then why can't any Boolean variable be prefixed with is? Let's talk about the reasons below.

Define a pojo class
package com.example.demo.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("user")
@Builder
public class User implements Serializable {
    
    

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;
    private String name;
    private Integer age;
    private String addr;
    private Integer isModify; //是否被修改
    private Boolean isDelete; //是否被删除

}

See that the User object has two variable attributes, one is isModify and the other is isDelete. Although both variables start with is, the corresponding data types are different. The data type corresponding to isModify is Integer, and the corresponding data type isDelete The data type is Boolean.

@Data: I believe everyone will use this annotation in production projects. This is an annotation in the lombok framework, which can save us a lot of getter and setter code in the project. The following is the definition of @Data, you can see that it is composed of Getter, Setter, RequiredArgsConstructor, ToString and EqualsAndHashCode.

package lombok;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 
 * @see Getter
 * @see Setter
 * @see RequiredArgsConstructor
 * @see ToString
 * @see EqualsAndHashCode
 * @see lombok.Value
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
public @interface Data {
    
    
	String staticConstructor() default "";
}

Annotated by @Data, the Getter and Setter methods of isModify and isDelete of the generated User object are shown below. You can see that the Getter and Setter methods corresponding to the Boolean variable isDelete will omit the is prefix, which will result in not getting isDelete Attributes. Because it is defined as an attribute of the basic data type Boolean isDelete, and its method is also isDelete(), when the framework reverse parses, it "mistakenly thinks" that the corresponding attribute name is delete, resulting in that the attribute cannot be obtained.

	public Integer getIsModify() {
        return isModify;
    }

    public void setIsModify(Integer isModify) {
        this.isModify = isModify;
    }

    public Boolean getDelete() {
        return isDelete;
    }

    public void setDelete(Boolean delete) {
        isDelete = delete;
    }

This problem also exists if you do not use the @Data annotation, but use the development tool IDEA to automatically generate Getter and Setter methods. To solve this problem, we need to manually write Getter and Setter methods. Obviously, this method is very heavy and undesirable. It not only increases the readability of the code, but also increases the workload and cost of development.

	public Integer getIsModify() {
        return isModify;
    }

    public void setIsModify(Integer isModify) {
        this.isModify = isModify;
    }

    public Boolean getIsDelete() {
        return isDelete;
    }

    public void setIsDelete(Boolean isDelete) {
        this.isDelete = isDelete;
    }
Then if the table creation field uses the naming method of is_xxx, how should the corresponding entity POJO class be defined? How to map with the persistence layer framework?

Method 1: The is_xxx variable corresponding to the table field uses the Integer data type instead of the Boolean data type. As shown in the above User object, isModify of the Integer data type does not have the problem that the reverse analysis cannot obtain the isModify variable.

Method 2: The is_xxx variable corresponding to the table field still uses the Boolean data type, but the POJO class variable removes the is prefix, and then sets the mapping relationship from is_xxx to xxx in the xml configuration file.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.dao.UserMapper">
    <resultMap type="com.example.demo.entity.User" id="userMap">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="age" column="age"/>
        <result property="name" column="name"/>
        <result property="addr" column="addr"/>
        <result property="isModify" column="is_modify"/>
        <result property="delete" column="is_delete"/>
    </resultMap>
</mapper>

Guess you like

Origin blog.csdn.net/qq798867485/article/details/131392842