问题1:数据库表字段与实际开发中的字段不同步
//此时使用 @TableField(value = "") 注解便可以使字段统一
//value 设置的是数据库表中的字段
@TableField(value = "pwd")
private String password;
问题2:实际开发中添加了数据库表中未定义的属性
//exist: 设置属性在数据库表字段中是否存在,默认为true。此属性无法与value合并使用
@TableField(exist = false) //表示这个字段在数据库中不存在
private Integer online;
问题3:采用默认查询开放了更多的字段查看权限
在查询过程中把密码暴露,有风险,因此需要把 pwd 不参与查询
@TableField(value = "pwd",select = false)
private String password;
问题4:数据库表名与在实际开发中使用的表名不同
解决方法 ① :在yml文件中添加配置 table-prefix:
#my-plus相关配置
mybatis-plus:
global-config:
db-config:
table-prefix: tbl_ #如果数据库名字有下滑线需要声明以便于能查到泛型,类似于tbl_user
id-type: auto #数据库的自增操作
configuration: #打开my-plus的数据库操作日志,以标准输出的格式
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
解决方法 ② :添加注解 @TableName("xxx"),对应数据库中的表名
@TableName("tbl_user")
public class User {
private Long id;
private String name;
@TableField(value = "pwd",select = false)
private String password;
private Integer age;
private String tel;
@TableField(exist = false)
private Integer online;
}