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 implement Mybatis ResultMap Multi-table Mapping DTO

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces "how to realize Mybatis ResultMap multi-table mapping DTO". In daily operation, I believe many people have doubts about how to realize Mybatis ResultMap multi-table mapping DTO. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to realize Mybatis ResultMap multi-table mapping DTO". Next, please follow the editor to study!

Steps:

1: build DT

Package com.steak.system.pojo.dto;public class ApplyDTO {the ID of the application form belongs to the application form (sys_apply) private String selfIntroduction; / / self-introduction belongs to the application form (sys_apply) private String applyTime; / / the application time belongs to the application form (sys_apply) private String userName; / / the applicant, and belongs to the user form (sys_user) private String collegeName / / the name of the second-level college, which belongs to the second-level college form (sys_college) private String recruitName; / / the title of the recruitment form, belongs to the recruitment form (sys_recruit), private String status; / / the application status belongs to the application form (sys_apply})

2: create mybatis Mapper.xml

Select a.applyroomidrect a.selfintroducing introducing time.userroomnamerec. Accountgeaccounnamememe a.status.statusther.statusplayname from sys_recruit r.statusdepartment r apply a language syscraper user u.m.syscraftcollege c where a.recruit_id = r.recruit_id and a.apply_people_id = u.user_id and r.apply_depatment = c.college_id and release_people_id = # {userId,jdbcType=VARCHAR}

Create interface Mapper

List getApplyDTO (String userId)

Conversion between entities (write less code)

For example, my encapsulated DTO contains a field of int type status, which is usually represented by numbers in the database, but when we return to the foreground, we should use a field that others can understand (for example, to be audited, approved), so we need to define a VO (return the front-end model, and the fields in it should be the same as the fields to be converted in DTO. Note, it needs to be converted. I can also define other things in VO. When DTO is converted, only the corresponding ones in VO are converted, and then the values are converted to VO by set. In fact, if there are fewer data fields, you can manually set. If there are 50 fields, you can set one field by field (in fact, I only need to convert one status field to OK, of course, you can leave it to the front end, but sometimes the front end doesn't know what it represents. So we try to parse the front end well, so others don't have to guess.), it is laborious and time-consuming, and the code is not elegant, so it is recommended to use some entity conversion tools. I use Apache's BeanUtils, in which there are conversion methods, such as converting DTO to VO, as follows

BeanUtils.copyProperties (VO,DTO)

In that case, I will transfer all the data to VO. We set the status field in VO to String, and then take the status field in DTO and semantically change it into a comprehensible field. Why not take the field in VO, but take DTO? as I said earlier, we can only convert the corresponding to ourselves, because the status in DTO is int, and the status in VO is String, so the status in VO is null. Therefore, you need to take the status in DTO and assign it to the status in VO, such as: 1 = to be audited 2 = audited

At this point, the study on "how to implement Mybatis ResultMap multi-table mapping DTO" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Internet Technology

Wechat

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

12
Report