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

ezykj2lf  于 2023-10-16  发布在  Oracle
关注(0)|答案(1)|浏览(178)

我有2个表,表1有一个ID列是自动生成的。我将这个ID作为表2的外键,我使用mybatis将数据插入表中。我卡在我需要发送外键来动态插入命令的地方。
表1

  1. CLM_ID (PK) AUTO GENERATED
  2. CLM_VALUE NN UNIQUE

表2

  1. CLM_ID (FK)
  2. CML_VALUE1 (NN)
  3. CML_VALUE2 (NN)
  4. CML_VALUE3 (NN)

根据请求,我将数据存储到表1中,ID将自动生成。当我尝试在表2中存储数据时,如何获取{ID}
如果我知道相应列的值,我就可以获得与该列关联的ID。但是如何动态地传递列名呢?
示例Map器我有。

  1. public class Address {
  2. private Integer address_ID; //PK auto generated
  3. private String name;
  4. // getters and setters
  5. }
  6. public class Home {
  7. private Integer addressID; //FK
  8. private String Name;
  9. }
  10. public interface HomeMapper {
  11. String INSERT_ADDRESS = "INSERT INTO HOMES(ADDRESS_ID, NAME) VALUES ( {addressID}, #{name})";
  12. @Insert(INSERT_ADDRESS)
  13. @SelectKey(**statement="SELECT ADDRESS_ID FROM ADDRESSES WHERE NAME='Mintu'**", keyProperty = "addressID", before=true, resultType=int.class)
  14. public void insertRecord(Home homeName);
  15. }

如何将值动态发送到语句?
有人能帮我处理这个问题吗?我是新的mybatis和不确定这是否是实现这一点的方式。

ddarikpa

ddarikpa1#

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

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

这里是postMapping:

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

下面是JSON数据:

  1. {
  2. "articleContent": "Hello my friend",
  3. "articlePublicationDate": "1999-09-05",
  4. "article_title":"the Begining",
  5. "author_id":1
  6. }
展开查看全部

相关问题