MybatisPlus--常用注解

1.@TableName
用于指定实体类对应的数据库表名,适用于实体类名与数据库表名不一致,或者实体类名不是数据库表名的驼峰写法时

import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
/*指定实体类User对应数据库表名为user*/
@Data
@TableName("user")
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

此外,还可以通过配置文件为数据库表名设置同一前缀

mybatis-plus:
  configuration:
    # 配置MyBatis日志
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  global-config:
    db-config:
    # 配置MyBatis-Plus操作表的默认前缀
    table-prefix: t_

2.@TableId

/*@TableId用于解决主键字段可能与数据库表中的主键列名不一致的问题*/
@TableId(value = "user_id")
private Long id;
/*@TableId 注解的 type 属性可以指定主键的生成策略,包括:
  IdType.AUTO:数据库自增主键。
  IdType.ASSIGN_ID (默 认):雪花算法的策略生成数据id,与数据库id是否设置自增无关
*/
@TableName("user")
public class User {
    @TableId(type = IdType.AUTO) // 指定主键生成策略为数据库自增
    private Integer id;

    private String name;
    private Integer age;

    // Getter 和 Setter 方法
}

3.@TableField

/*@TableField 注解的 value 属性用于指定数据库表中的字段名称。如果实体类的字段名与数据库表字段名不一致,可以通过 value 属性进行映射。*/
import com.baomidou.mybatisplus.annotation.TableField;

public class User {
    @TableField("username")
    private String name;
}

4.@TableLogic

/*#TableLogic用于实现逻辑删除功能*/
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;

@TableName("user")
public class User {
    private Long id;
    private String name;
    private Integer age;

    @TableLogic(value = "0", delval = "1")
    private Integer isDeleted;
}
posted @ 2025-03-26 22:31  茴香儿  阅读(18)  评论(0)    收藏  举报