简体   繁体   中英

how to insert data in 2 tables at a time using selectKey in myBatis

I need to store data in 2 tables using myBatis select key and Java,can anyone please help how to do this.My table structures are:

     Temp                                          Sect
id name created_at                           sid  sectName  duration   priorty

now i need to insert name,sectName,duration,priority in Temp and Sect tables,the code which i wrote was:

@Insert("insert into Temp (name) values(#{name})")
@SelectKey(statement="call identity()", keyProperty="id", before=false, 
resultType=int.class)
public int insertTemp(Name name);

@Insert("insert into sect (name,duration.priority) values(#{name}, #
{duration},#{priority})")
@SelectKey(statement="call next value for TestSequence", 
keyProperty="nameId", before=true, resultType=int.class)
public int insertSect(sectName name);

And my POJO classes are:

Public Temp{
  private int id;
  private String name;
  private int creat_at;
  //setters getters
}

Public Temp{
  private int sid;
  private String sectName;
  private int duration;
  private int priority;
  //setters getters
}

Please someone tell me how to write that query and the command which i wrote was correct or wrong?

Your model is slightly confusing. I'll assume that f second Temp was supposed to be named "Sect"

You actually use flag before = true in insertSect. You can use this like that:

//First insert into db
@Insert("insert into sect (**id**, name,duration.priority) values(**#{id}**, #{name}, #
{duration},#{priority})")
(...)
public int insertSect(sectName name);

//second insert
@Insert("insert into Temp (**id**, name) values(**#{id}**, #{name})")
//there's no select key anymore
public int insertTemp(**@Param("name")** Name name, **@Param("id") id**);

//and calling it
mapper.insertSect(sect)
sect.getId() //will return ID returned by "call next value for TestSequence"
mapper.insertTemp(name, sect.getId())

The '**' character used to bring attention to changes in your code. Unfortunately SO don't bold text in code section.

It is also possible to use one insert if you don't need to have created ID in more that one object after insert.

@Insert("insert into Temp(id, name) values (#{sect.id}, #{name});
insert into Sect(id, name, ...) values (#{sect.id}, #{sect.name}, ...)")
@SelectKey(statement="call next value for TestSequence", 
keyProperty="sect.id", before=true, resultType=int.class)
public void insert(@Param("sect") Sect sect, @Param("name") String name)

If you need to use two different keys, the best way will be to use one method, which will call two inserts and just annotate it as @Transcational, so failure on second insert will rollback second, fe

@Transactional
void insert(...) {
    mapper.firstInsert(...)
    mapper.secondInsert(...)
}

If it isn't what you wanted, please clarify - for now your question (and model) is a little confusing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM