oracle 使用mybatis将数据插入带有外键的表中

ezykj2lf  于 12个月前  发布在  Oracle
关注(0)|答案(1)|浏览(79)

我有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和不确定这是否是实现这一点的方式。

ddarikpa

ddarikpa1#

我正在使用Postman发送数据。我发现你在postman中输入的名字作为json数据必须跟在你在@Insert语句中提供的名字后面,而不是数据库。

@Insert("insert into article_model(article_content,article_PublicationDate,article_title,author_id)" +
           "values(#{articleContent},#{articlePublicationDate},#{article_title},#{author_id})")
    Integer newArticle(ArticleModel newArticleModel);

这里是postMapping:

@PostMapping("/articles")
void newArticle(@RequestBody ArticleModel newArticle){
    articleMapper.newArticle(newArticle);
}

下面是JSON数据:

{
    "articleContent": "Hello my friend",
    "articlePublicationDate": "1999-09-05",
    "article_title":"the Begining",
    "author_id":1
}

相关问题