不同的表对应不同的id生成策略
日志:自增
购物订单:特殊规则(FQ23324AK443)
外卖单:关联地区日期等信息( 10 04 20200314 34 91)
关系表:可省略id
..........
@TableId :模型类中用于表示主键的属性定义上方---用来设置当前类中主键属性的生成策略
(1) id自增:@TableId(type = IdType.AUTO)
package com.itheima.domain; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; @Data @TableName("tbl_user") public class User { @TableId(type = IdType.AUTO) 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; }
(2)存储数据时必须自己指定id,在数据库表中关闭id自增功能,在id上添加:@TableId(type = IdType.INPUT)
package com.itheima.domain; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; @Data @TableName("tbl_user") public class User { @TableId(type = IdType.INPUT) 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; }
(3)雪花算法生成id,在id上添加:@TableId(type = IdType.ASSIGN_ID)
package com.itheima.domain; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; @Data @TableName("tbl_user") public class User { @TableId(type = IdType.ASSIGN_ID) 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; }
若要所有得实体类id都用同一个策略生成,可在配置文件中设置好相应的格式,不必每个类的id属性都添加字段控制
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/mybatisplus_db?serverTimezone=UTC username: root password: 130740 type: com.alibaba.druid.pool.DruidDataSource main: banner-mode: off mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl global-config: banner: false db-config: id-type: assign_id
实体类名与表明不同也可通过全局配置
package com.itheima.domain; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; @Data //@TableName("tbl_user") public class User { // @TableId(type = IdType.ASSIGN_ID) 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; }
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/mybatisplus_db?serverTimezone=UTC username: root password: 130740 type: com.alibaba.druid.pool.DruidDataSource main: banner-mode: off mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl global-config: banner: false db-config: id-type: assign_id table-prefix: tbl_
标签:mybatisplus,MyBatisPlus,private,-------,baomidou,import,com,id From: https://www.cnblogs.com/fxzm/p/17133478.html