mybatis_plus实体类设置双主键
发布人:shili8
发布时间:2025-03-11 02:57
阅读次数:0
**MyBatis Plus 实体类设置双主键**
在实际开发中,我们经常会遇到需要使用双主键的场景。例如,一个订单表可能需要同时使用订单号和用户 ID 作为主键。在 MyBatis Plus 中,可以通过以下方式来实现双主键。
###1. 使用 @TableId 注解首先,我们需要在实体类中定义两个字段分别作为主键,然后使用 `@TableId` 注解指定它们的类型为 `id` 和 `auto`,表示这两个字段都是主键,并且其中一个是自增的。
javaimport com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.IdType; public class Order { @TableId(type = IdType.AUTO) private Long orderId; // 自增主键 @TableId(value = "user_id") private Long userId; // 非自增主键 // 其他字段}
###2. 使用 @TableField 注解如果我们需要指定某个字段的名称不一样,则可以使用 `@TableField` 注解来进行设置。
javaimport com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; public class Order { @TableId(type = IdType.AUTO) private Long orderId; // 自增主键 @TableId(value = "user_id") private Long userId; // 非自增主键 @TableField("order_name") // 指定字段名称 private String orderName; // 其他字段}
###3. 使用 @TableLogic 注解如果我们需要使用逻辑删除功能,则可以使用 `@TableLogic` 注解来进行设置。
javaimport com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableLogic; public class Order { @TableId(type = IdType.AUTO) private Long orderId; // 自增主键 @TableId(value = "user_id") private Long userId; // 非自增主键 @TableField("order_name") // 指定字段名称 private String orderName; @TableLogic // 逻辑删除字段 private Integer deleted; }
###4. 使用 @TableComment 注解如果我们需要对某个字段进行注释,则可以使用 `@TableComment` 注解来进行设置。
javaimport com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableComment; public class Order { @TableId(type = IdType.AUTO) private Long orderId; // 自增主键 @TableId(value = "user_id") private Long userId; // 非自增主键 @TableField("order_name") // 指定字段名称 private String orderName; @TableComment("订单备注") // 对字段进行注释 private String remark; }
###5. 使用 @TableName 注解如果我们需要对某个表进行注释,则可以使用 `@TableName` 注解来进行设置。
javaimport com.baomidou.mybatisplus.annotation.TableName; public class Order { @TableId(type = IdType.AUTO) private Long orderId; // 自增主键 @TableId(value = "user_id") private Long userId; // 非自增主键 @TableField("order_name") // 指定字段名称 private String orderName; @TableComment("订单备注") // 对字段进行注释 private String remark; }
###6. 使用 @TableId 注解的其他属性除了 `type` 和 `value` 之外,`@TableId` 注解还有其他几个属性可以使用。
* `strategy`: 表示自增策略,可以是 `AUTO_INCREMENT` 或 `IDENTITY`。
* `generator`: 表示自增生成器,可以是 `UUID` 或 `SNOWFLAKE`。
* `column`: 表示自增列的名称,可以是任意字符串。
javaimport com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.IdType; public class Order { @TableId(type = IdType.AUTO, strategy = "AUTO_INCREMENT") private Long orderId; // 自增主键 @TableId(value = "user_id", generator = "UUID") private Long userId; // 非自增主键}
###7. 使用 @TableField 注解的其他属性除了 `value` 之外,`@TableField` 注解还有其他几个属性可以使用。
* `exist`: 表示该字段是否存在,可以是 `true` 或 `false`。
* `updateable`: 表示该字段是否可更新,可以是 `true` 或 `false`。
* `insertable`: 表示该字段是否可插入,可以是 `true` 或 `false`。
javaimport com.baomidou.mybatisplus.annotation.TableField; public class Order { @TableField(value = "order_name", exist = true) private String orderName; // 指定字段名称 @TableField(value = "remark", updateable = false) private String remark; // 对字段进行注释}
###8. 使用 @TableLogic 注解的其他属性除了 `value` 之外,`@TableLogic` 注解还有其他几个属性可以使用。
* `exist`: 表示逻辑删除字段是否存在,可以是 `true` 或 `false`。
* `updateable`: 表示逻辑删除字段是否可更新,可以是 `true` 或 `false`。
javaimport com.baomidou.mybatisplus.annotation.TableLogic; public class Order { @TableLogic(value = "deleted", exist = true) private Integer deleted; // 逻辑删除字段 @TableLogic(updateable = false) private String remark; // 对逻辑删除字段进行注释}
###9. 使用 @TableComment 注解的其他属性除了 `value` 之外,`@TableComment` 注解还有其他几个属性可以使用。
* `exist`: 表示该字段是否存在,可以是 `true` 或 `false`。
* `updateable`: 表示该字段是否可更新,可以是 `true` 或 `false`。
javaimport com.baomidou.mybatisplus.annotation.TableComment; public class Order { @TableComment(value = "order_name", exist = true) private String orderName; // 指定字段名称 @TableComment(updateable = false) private String remark; // 对字段进行注释}
###10. 使用 @TableName 注解的其他属性除了 `value` 之外,`@TableName` 注解还有其他几个属性可以使用。
* `exist`: 表示该表是否存在,可以是 `true` 或 `false`。
* `updateable`: 表示该表是否可更新,可以是 `true` 或 `false`。
javaimport com.baomidou.mybatisplus.annotation.TableName; public class Order { @TableName(value = "order", exist = true) private String orderName; // 指定表名称 @TableName(updateable = false) private String remark; // 对表进行注释}
以上就是 MyBatis Plus 实体类设置双主键的方法和示例代码。