MyBatis-plus中的 @TableField(exist = false)注解

应用场景:

当往实体类加入新属性,希望basemapper的基础查询可以忽略该属性

例子:

有实体类Permission

public class Permission implements Serializable {
    private static final long serialVersionUID = 1L;
    @TableId(value = "permission_id", type = IdType.ID_WORKER_STR)
    private String permissionId;
    private String permissionUrl;
    private String permissionCode;
    private String permissionDesc;
    private String permissionStatus;
    private String parentId;
}

一一映射数据库中的字段

当往实体类中添加数据库中没有的属性时
public class Permission implements Serializable {
    private static final long serialVersionUID = 1L;
    @TableId(value = "permission_id", type = IdType.ID_WORKER_STR)
    private String permissionId;
    private String permissionUrl;
    private String permissionCode;
    private String permissionDesc;
    private String permissionStatus;
    private String parentId;

    //额外添加的两个字段
    private List children;
    private Integer level;
}

使用basemapper查询

List permissionList = baseMapper.selectList(wrapper);
报错,因为数据库中没有多出来的这两个字段,sql语句语句中可以看到多出来了children,与level字段,由于数据库中没有这两个字段,所以报错。
Error querying database.  Cause: java.sql.SQLSyntaxErrorException: Unknown column 'children' in 'field list'
The error may exist in xyz/lande/demo/mapper/PermissionMapper.java (best guess)
The error may involve defaultParameterMap
The error occurred while setting parameters
SQL: SELECT  permission_id,permission_url,permission_code,permission_desc,permission_status,parent_id,children,level  FROM permission  WHERE  1=1 ORDER BY permission_id DESC
Cause: java.sql.SQLSyntaxErrorException: Unknown column 'children' in 'field list'
; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: Unknown column 'children' in 'field list'] with root cause
现在加上注解 @TableField(exist = false)
public class Permission implements Serializable {
    private static final long serialVersionUID = 1L;
    @TableId(value = "permission_id", type = IdType.ID_WORKER_STR)
    private String permissionId;
    private String permissionUrl;
    private String permissionCode;
    private String permissionDesc;
    private String permissionStatus;
    private String parentId;

    //额外添加的两个字段
    @TableField(exist = false)
    private List children;
    @TableField(exist = false)
    private Integer level;
}
运行后再次测试
 Preparing: SELECT permission_id,permission_url,permission_code,permission_desc,permission_status,parent_id FROM permission WHERE 1=1 ORDER BY permission_id DESC 

发现sql语句中语句去除了这两个字段。

你可能感兴趣的:(MyBatis-plus中的 @TableField(exist = false)注解)