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

Mybatis returns map results and analysis of scenario examples used by @ MapKey

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

Share

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

This article introduces the knowledge of "mybatis returns map results and scenario instance analysis used by @ MapKey". In the operation of actual cases, many people will encounter such a dilemma. Next, 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!

Mybatis returns the map result set @ MapKey usage scenario

When the resultType attribute of select is map:

Specify the key value of map through MapKey

Use id as ke@MapKey ("id") Map getUserInfoMap () for map; value for Map is Map, and one record corresponds to a Map@MapKey ("id") Map getUserValueMap (); use name as key@MapKey for map ("name") Map getUserInfoMap ();. Returns a record. Map getUserInfoMapById (); mybatis uses @ MapKey to annotate the background and meaning

Background: today, we use MyBatis to query some records. The data involves the data in two tables and needs to be queried with tables, but the result returned by my MyBatis does not want to create a new DO object, so I use the @ MapKey annotation to return a Map collection.

Meaning: the @ MapKey annotation is used in mapper.xml files. It is generally used to query the results of each field in multiple records and is stored in Map. An example of Map structure is as follows: Map. The paradigm type can be modified.

Map's key: generally stores the primary key of each record, and can also be represented by other values, depending on the fields after the @ MapKey annotation of the Dao layer (such as @ MapKey ("id"))

Map's value: is also a Map, which indicates that the field name and field value of each field in this record are queried.

Concrete example

1. Mapper.xml file

Select ext.id as id, info.produce_area as area, ext.attribute_value as companyName from product_info info left join product_ext ext on info.id=ext.id where info.id=# {id} and ext.attribute_name=# {name}

2. Dao.java

@ MapKey ("id") Map getInfoById (@ Param ("id") Long id, @ Param ("name") String name

3. Manager.java

/ / query, return result: {1 = {area: Shanghai, companyName= West Company}} / / if there are multiple records, return results such as: {1 = {area: Shanghai, companyName: West West Company}, 2 = {area: Nanjing, compantyName: monkey Company}} Map map= productDao.getInfoById (id, "company name"); if (map==null | | map.isEmpty () | |! map.containsKey (id)) {return null } / / get result String area = produceInfoMap.get (id) .get ("area"); String companyName = produceInfoMap.get (productTracInfoId) .get ("companyName"); "mybatis returns map result and scenario instance analysis used by @ MapKey". 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