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 of Multi-layer nesting of association tags in Mybatis

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

The knowledge of this article "how to solve the problem of multi-layer nesting of association tags in Mybatis" is not understood by most people, so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to solve the problem of multi-layer nesting of association tags in Mybatis" article.

Multi-layer nesting of association tags

When the query in mybatis uses nested association tags, it is found that the result of the inner association query is always null.

Investigation

Check the implementation of sql, find that data is returned, and exclude

Check whether the value of property is the same as that in pojo, and exclude

Check whether the value of column corresponds to that of the database, corresponding, exclude

Then it should be that mybatis does not map the data in place. After troubleshooting, the columnPrefix in association is not corresponding.

SELECT ii.Id, ii.model, ii.status, ii.work_time, ui.id AS ui_id, ui.interface_name AS ui_interface_name, ui.interface_type AS ui_interface_type, ui.frequency AS ui_frequency, ui.address AS ui_address, ui.template_or_sql AS ui_template_or_sql, ui.status AS ui_status, sys.id AS sys_id Sys.system_name AS sys_system_name, sys.system_name_en AS sys_system_name_en, sys.belong AS sys_belong, sys.status AS sys_status, ser.id AS ser_id, ser.ftp_ip AS ser_ftp_ip, ser.ftp_port AS ser_ftp_port, ser.ftp_account AS ser_ftp_account, ser.ftp_password AS ser_ftp_password there is nothing wrong with the code.

The reason is that when association performs multi-layer nesting, mybatis combines the columnPrefix value of the outer association with that of the inner layer.

If the outer columnPrefix value bit is sys_, then the inner layer of ui_, cannot be sys.id AS sys_id like this in SQL. You need to add the ui_ prefix to sys.id AS ui_sys_id, so that mybatis will map the data to the corresponding association when matching.

The normal code is as follows: SELECT ii.Id, ii.model, ii.status, ii.work_time, ui.id AS ui_id, ui.interface_name AS ui_interface_name, ui.interface_type AS ui_interface_type, ui.frequency AS ui_frequency, ui.address AS ui_address, ui.template_or_sql AS ui_template_or_sql, ui.status AS ui_status, sys.id AS ui_sys_id Sys.system_name AS ui_sys_system_name, sys.system_name_en AS ui_sys_system_name_en, sys.belong AS ui_sys_belong, sys.status AS ui_sys_status, ser.id AS ui_ser_id, ser.ftp_ip AS ui_ser_ftp_ip, ser.ftp_port AS ui_ser_ftp_port, ser.ftp_account AS ui_ser_ftp_account Ser.ftp_password AS ui_ser_ftp_password

Problem solved!

Association collection nesting

After learning the nesting of the collection of returned values in mybatis's query, check the official website first:

What's the use of returning the collection?

For example, three tables.

Hr_job_department

Hr_job_position

In the third table, only the primary keys of the above two tables are used to indicate departments and positions.

But when querying, you want to show the following results

So the return value is more than one object, so collection nesting is used.

You just need to know:

(1) column represents the database field

(2) property represents the value in Java

And my primary keys here are all id, so there will be duplicate names. In the SQL statement, you have to give an alias to the query before you can distinguish it. The returned result resultMap is shown in the figure above.

SELECT ui.id, ui.name, ui.gender, ui.id_card, ui.is_married, ui.department_id, ui.position_id, ui.phone, ui.priority, ui.entry_time Ui.full_time, ui.created_time, ui.edited_time, jd.id jdid, jd.name jdname, jp.id jpid Jp.name jpname FROM hr_user_info ui INNER JOIN hr_job_department jd ON ui.department_id=jd.id INNER JOIN hr_job_position jp ON ui.position_id=jp. Id and ui.id = # {someone.id} and ui.gender = # {someone.gender} And ui.name = # {someone.name} and ui.id_card = # {someone.idCard} and ui.is_married = # {someone.isMarried} And ui.department_id = # {someone.jobDepartment.id} and ui.position_id = # {someone.jonPosition.id} And ui.phone = # {someone.phone} and ui.entry_time = # {someone.entryTime} and ui.full_time = # {someone.fullTime}

The above picture uses INNER JOIN to query looks very concise, there is a non-concise way to write as follows, although you can get the results, but do not know how the performance comparison

SELECT ui.id, ui.name, ui.gender, ui.id_card, ui.is_married, ui.department_id, ui.position_id, ui.phone, ui.priority, ui.entry_time Ui.full_time, ui.created_time, ui.edited_time, (select id jdid from hr_job_department jd where jd.id=ui.department_id) jdid, (select name jdname from hr_job_department jd where jd.id=ui.department_id) jdname, (select id jpid from hr_job_position jp where jp.id=ui.position_id) jpid (select name jpname from hr_job_position jp where jp.id=ui.position_id) jpname FROM hr_user_info ui The above is about the content of this article on "how to solve the problem of multi-layer nesting of association tags in Mybatis". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge content, please pay attention to the industry information channel.

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: 296

*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