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 use the @ ResultMap,@Results,@Result annotation of Mybatis

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

Share

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

This article focuses on "how to use Mybatis's @ ResultMap,@Results,@Result annotation". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor learn how to use the @ ResultMap,@Results,@Result annotation of Mybatis.

Mybatis comments @ Results, @ Result, @ ResultMap questions

When using mybatis, it is found that mybatis can automatically match fields with the same entity name and database field name. What should I do when the entity name is different from the field name of the database?

The column name corresponding to the table of the database:

The entity classes built in the springboot project are:

Public class MapModel {private Long key; private String value; / / omit getter, setter method} method one

Give the query field another name corresponding to the name of the entity class:

@ Select ("SELECT province_id as key, province_name as value FROM `jposition`") public List provinceName (); method 2

Use @ Results, @ Result, @ ResultMap annotations:

@ Select ("SELECT province_id, province_name FROM `jjposition`") @ Results (id= "resultMap1", value = {@ Result (property = "key", column = "province_id"), @ Result (property = "value", column = "province_name")}) public List provinceName ()

The defined id= "resultMap1" can be used

@ ResultMap ("resultMap1) @ Select (" SELECT province_id, province_name FROM `j_ position` where province_name=# {name} ") public List provinceName (String name)

Method 2 is recommended!

Mybatis annotations use annotations corresponding to resultMap, and corresponding annotations Results, Result, One, Many

Some of them are built on my last blog, and mybatis comments are used (single table query). If you don't understand there, I suggest you take a look at my last blog (all configuration files, interfaces, and database creation are described).

Corresponding comments:

1.@Results comments

Instead of labels.

You can use a single @ Result annotation or a @ Result collection in this annotation

@ Results ({@ Result (), @ Result ()}) or @ Results (@ Result ())

Note: use annotations if you report org.apache.ibatis.binding.BindingException:Invalid bound statement (not found): interface full class name. Method name

Maybe you forgot to use the @ Results annotation when using the @ Select annotation.

2.@Resutl comments

Instead of labels and tags

Attributes in @ Result:

Column name of the column database

The attribute name that Property needs to assemble

@ One annotations required by one (@ Result (one=@One) ()

@ Many annotations required by many (@ Result (many=@many) ()

3.@One comments (one-to-one)

Instead of tags, it is the key to multi-table queries and is used in annotations to specify that subqueries return a single object.

@ One comment attribute introduction:

Select specifies the sqlmapper used for multi-table queries

FetchType overrides the global configuration parameter lazyLoadingEnabled.

Use format:

@ Result (column= "", property= "", one=@One (select= "")) 4.@Many comments (many to one)

Instead of tags, it is the key to multi-table queries and is used in annotations to specify that subqueries return a collection of objects.

Note: aggregate elements are used to handle "one-to-many" relationships. You need to specify the attribute of the mapped Java entity class, the javaType of the attribute (usually ArrayList), but it may not be defined in the annotation.

Use format:

@ Result (property= "", column= "", many=@Many (select= ")) so far, I believe you have a better understanding of" how to use Mybatis's @ ResultMap,@Results,@Result annotation ". You might as well do it in practice! 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