mybatis gets the primary key auto-increment id value

 

need:

  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

@Autowired

RepoSeqMapper repoSeqMapper;

@Override

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())

</insert>

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

Key: replace into

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326438265&siteId=291194637