In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Xiaobian to share with you how to use mybatis-plus resultMap, I believe most people do not know how to use it, so share this article for your reference, I hope you have a lot of harvest after reading this article, let's find out together!
If the query result is not consistent, then the corresponding data used to receive the query result will be Null. If resultMap is not used, then in order to avoid null properties corresponding to pojo objects, aliases in SQL statements will be used to treat the fields in the query database as pojo object properties. Moreover, resultMap supports delayed loading.
But in order to avoid sql statements looking bloated, we use resultMap.
simple use
1. Select the model of the resultMap you want to map. The following is the model I want to map. Model=====>Category.java [Pojo class for resultMap]
package com.atguigu.gulimall.product.vo;import com.baomidou.mybatisplus.annotation.TableLogic;import com.baomidou.mybatisplus.annotation.TableName;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;import lombok.ToString;import lombok.experimental.Accessors;@Data@AllArgsConstructor@NoArgsConstructor@ToStringpublic class Category { private Long catId; private String name; private Long parentCid; private Integer catLevel; private Integer status; private Integer sort; private String iconImage; private String userName; private String unit; private Integer count; private Integer isDelete;}
note
Do not add the @ Accessories (chain=true) chain programming annotation of lombok to the pojo class you want to map to resultMap, because the attributes in resultMap will be red [but still usable], as shown below
2. Write resultMap in Mapper.xml, and specify resultMap label and its name in sql query statement to be used
select * from pms_category where cat_id = #{cId}
Each label in the resultMap represents the meaning
type: resultMap java object finally mapped, you can use alias [if using resultMap actual returned object type] id: resultMap unique identifier [random] result: mapping definition for common name property: type The property name in the returned pojo object is written as the property name in category column: the field to be queried in the database [column name] Write the field name corresponding to property column in the database table to be mapped to form the corresponding relationship between fields and pojo attributes in the database
note
If the resultMap is in another nameSpace at this time, that is, in other mapper files, then the resultMap indicates the namespace when used across the mapper.
3. Testing, running projects directly
If the number of fields queried in the database is less than or more than resultMap, then the mapping can still be successful, and the corresponding field is mapped. Because the name is not mapped or the attribute corresponding to the field does not exist in pojo, null and no such field are returned directly in the returned pojo object.
The above is "mybatis-plus resultMap how to use" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to 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: 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.