Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem that mybatis one-to-many query resultMap only returns one record

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "how to solve the mybatis one-to-many query resultMap only returned a record problem". In the operation of the actual case, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Problem description: because of a requirement of the leader, I need to use resultMap. I haven't used it for a long time, except for some surprises. Just write down this question.

Prepare two classes: author (author) and book (book). The database creates the corresponding author- > book one-to-many data.

@ Datapublic class Author {private Integer id; private String name; private String phone; private String address; private List books;} @ Datapublic class Book {private Integer id; private String name; private String press; private BigDecimal price; private Integer authorId;}

The initial Mapper.xml file

Select T1 inner join book T2 on t1.id=t2.author_id where t1.id=# {id}

Use postman to perform to view the results:

{"code": "200"," msg ":" success "," data ": {" id ": 1," name ":" extrajudicial fanatic Zhang San "," phone ": null," address ": null," books ": [{" id ": 1 "name": "extrajudicial fanatic Zhang San", "press": "people's Publishing House", "price": 10.00, "authorId": 1}]}}

Problem found: originally author has two records corresponding to book, but only one record is returned in books.

The cause of the problem: the primary keys of both tables are called id, so the results cannot be displayed correctly.

Solution: 1. The primary key uses unused field names. 2. Use aliases when querying sql

1. The primary key uses an unused field name, which involves changing the database. You only need to change one of them. Here is a demonstration of changing the id of book to book_id

Select T1 inner join book T2 on t1.id=t2.author_id where t1.id=# {id}

2. Use aliases when querying sql. Here is a demonstration of changing the alias of id to bookId when querying book

Select t1.recording as bookId t2.id as bookId, t2.* from author T1 inner join book T2 on t1.id=t2.author_id where t1.id=# {id} "how to solve the problem of mybatis one-to-many query resultMap returned only one record" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report