SpringBoot + Mybatis Plus + Oracle increment primary keys configuration

 

Since Oracle like SQL Server and MySQL have a primary key increment of design, the need to achieve a way to create a sequence or trigger primary key growth sequence can be used directly in SQL statements when using Mybatis, if we use Mybatis Plus Universal Mapper, not write your own SQL, in this case to achieve the primary key increment will need to do some additional configuration of:

 

1. Increase in application.yml profile

mybatis-plus:
  global-config:
    #主键类型  0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID";
    id-type: 1
    # Sequence序列接口实现类配置
    key-generator: com.baomidou.mybatisplus.incrementer.OracleKeyGenerator

Id-type wherein the value 1 indicates "the user enters the ID", equivalent to a user input using a sequence ID 

 

2. increase @KeySequence entity class notes, @ TableId increase type = IdType.INPUT

@Data
@Accessors(chain = true)
@TableName("yjk_user")
@KeySequence(value = "SEQ_YJK_USER", clazz = Integer.class)
public class User extends Model<User> {

    private static final long serialVersionUID = 1L;

    @TableId(value = "user_id", type = IdType.INPUT)
    private Integer userId;

    @TableField("phone_number")
    private String phoneNumber;

    @TableField("password")
    private String password;

}

Where the value of the corresponding sequence name you created this table, the value clazz is the primary key and type table corresponding @KeySequence notes of value, if the table's primary key is varchar2 type but need values ​​from sequence can be directly clazz value is set String.class

Also note type = Idtype.Input must not use type = Idtype.Auto

 

 

Published 119 original articles · won praise 24 · views 50000 +

Guess you like

Origin blog.csdn.net/zhang33565417/article/details/104068032