mybatis gets the primary key auto-increment id value



  The business ID is automatically generated and cannot be edited. The generation rules are as follows:

  Business type code + current date + serial number generated on the day, for example: if the business was created for the first time on December 6, the business number is: 112061


Technology used:

  mybatis (actually using mybatis-plus)

  Use replace into to replace the current eligible record, replace requires a primary key or a unique index

  Then, you can set date as a unique index

  Table Design:

  id primary key auto increment

  date date type, unique index



controller layer implementation

int seq = repoSeqService.getRepoSeq ();


service layer


RepoSeqMapper repoSeqMapper;


public int getRepoSeq () {


RepoSeq seq = new RepoSeq();

seq.setDate(new Date());

//The entity must be passed, otherwise the parameters cannot be obtained

int count = repoSeqMapper.insertRepoSeq(seq);

if(count > 0){

return seq.getId(); //taken from the incremented primary key


return 0;



in mapper

int insertRepoSeq(RepoSeq seq);


In mapper.xml

<insert id="insertRepoSeq" parameterType="com.repobase.entity.RepoSeq"

useGeneratedKeys="true" keyProperty="id">

replace into t_repo_seq(date) values(CURRENT_DATE())


必须写:useGeneratedKeys="true" keyProperty="id">

Key: replace into




Guess you like