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

Methods of inheritance, reuse and nesting of Mybatis resultMap tags

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article focuses on "Mybatis resultMap tag inheritance, reuse, nesting methods", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn the "Mybatis resultMap tag inheritance, reuse, nesting method"!

ResultMap tag inheritance, reuse, nesting

Record and demonstrate the use of resultMap tag inheritance, reuse (including cross-file) and multi-layer nesting in Mybatis

Inheritance: inherits existing resultMap tags for extension

Reuse: referencing existing resultMap tags across mapper files

Nesting: a method of mapping JavaBean and resultMap with multi-layer nesting

Definition table and entity class table

Create three tables group member score

Score and member one-to-one, associated through score.id

Group and member one-to-many, associated through group.id

Create table `score` (`id`int comment 'key', `math`float comment 'math score', `history`float comment', primary key (`id`)) create table `member` (`id`int comment 'primary key', `name`varchar comment 'name', `group_ id`int comment 'group group table id', `score_ id`int comment' score Score table id', primary key (`id`)) create table `group` (`id`int comment 'primary key') `name` varchar comment 'group name', primary key (`id`) entity class

Create three entity classes Group Member Score

Objects of the Score class are member variables of the Member class

The collection of objects of the Member class is a member variable of the Group class

/ * * score category * / public class Score {private Integer id; / * * Mathematics score * / private Float math; / * * Historical score * / private Float hitory;... getter And setter...} / * * member category * / public class Member {private Integer id; / * name * / private String name; / * * score object * / private Score score ... getter And setter...} / * * group class * / public class Group {private Integer id; / * * group name * / private String groupName; / * * member * / private List members;... getter And setter...} defines the resultMap that is mapped to the table

Define the most basic resultMap tags that map to database table fields in BeanMapper.xml

Inheritance, reuse, nesting

Create DemoMapper.xml to demonstrate the inheritance, reuse and nesting of tags

If you reuse existing tags in the same mapper file, you can directly use the id attribute of resultMap to reference. When cross-file, you need to specify the namespace attribute to reference normally.

Extends: inheritance, which can be inherited and extended from other resultMap

Association: reuse existing resultMap, which is suitable for specifying Java type using javaType when the corresponding attribute is a single JavaBean.

Collection: reuses an existing resultMap, which is suitable for specifying the Java type using ofType when the corresponding property is a JavaBean collection

ColumnPrefix: only the attribute with the specified prefix of this attribute is assigned to the current resultMap. When there are multiple layers of nesting, the prefix of this layer will be truncated from each layer.

As shown in the following mapper file, the outer fullMemberMap prefix is member_,. After this screening, member_score_id-> score_id

The scoreMap prefix of the inner layer is score_,. After this screening, score_id-> id is finally assigned to Score.id.

Therefore, only fields like member_score_id will eventually enter the value range of scoreMap.

If it is simply nested without reuse, the three classes can be directly written into a resultMap tag to implement.

Select g.id, g.name, m.id member_id, m.name member_name, s.id member_score_id, s.math member_score_math, s.history member_score_history from `group`g left join `member`m on m.group_id = g.id left join `score`s on s.id = m.score_id where g.id = # {id JdbcType=INTEGER} what you should pay attention to when using resultmap

Today, sql is mainly written according to the requirements. When you review and reuse in mybatis, you must see if there is this class. Today, there are several dto and timestamp conversion. The timestamp in java is calculated in milliseconds.

At this point, I believe that everyone on the "Mybatis resultMap tag inheritance, reuse, nesting methods" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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