我有2个表,表1有一个ID列是自动生成的。我将这个ID作为表2的外键,我使用mybatis将数据插入表中。我卡在我需要发送外键来动态插入命令的地方。
表1
CLM_ID (PK) AUTO GENERATED
CLM_VALUE NN UNIQUE
表2
CLM_ID (FK)
CML_VALUE1 (NN)
CML_VALUE2 (NN)
CML_VALUE3 (NN)
根据请求,我将数据存储到表1中,ID将自动生成。当我尝试在表2中存储数据时,如何获取{ID}
如果我知道相应列的值,我就可以获得与该列关联的ID。但是如何动态地传递列名呢?
示例Map器我有。
public class Address {
private Integer address_ID; //PK auto generated
private String name;
// getters and setters
}
public class Home {
private Integer addressID; //FK
private String Name;
}
public interface HomeMapper {
String INSERT_ADDRESS = "INSERT INTO HOMES(ADDRESS_ID, NAME) VALUES ( {addressID}, #{name})";
@Insert(INSERT_ADDRESS)
@SelectKey(**statement="SELECT ADDRESS_ID FROM ADDRESSES WHERE NAME='Mintu'**", keyProperty = "addressID", before=true, resultType=int.class)
public void insertRecord(Home homeName);
}
如何将值动态发送到语句?
有人能帮我处理这个问题吗?我是新的mybatis和不确定这是否是实现这一点的方式。
1条答案
按热度按时间ddarikpa1#
我正在使用Postman发送数据。我发现你在postman中输入的名字作为json数据必须跟在你在
@Insert
语句中提供的名字后面,而不是数据库。这里是postMapping:
下面是JSON数据: