In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this article, the editor introduces in detail "how to use Colum and property of resultMap in Mybatis". The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to use Colum and property of resultMap in Mybatis" can help you solve your doubts.
Colum and property attributes of resultMap 1: resultMap tag
When our database fields are inconsistent with the attributes of the entity class, we need to use this tag for one-to-one mapping.
2: usage 2.1 simple query. Colum corresponds to the fields of the database, and property corresponds to the attributes of the entity class.
He said what rules are used to map the data one by one, such as:
Select * from book where number=# {number}
The above search results are mapped according to the corresponding rules of the resultMap you gave.
2.2 one to one
It should be noted that the column name found in your sql statement is an alias, so you have to fill in the alias, which is not the column name of your database.
Returned result: User
User contains an one-to-many relationship with a collection of Role attributes
JavaType is used to specify the type of attribute in pojo
2.3 one-to-many
You also need to pay attention to column names as well as one-on-one.
Returned result: User
User contains an one-to-many relationship with a collection of Role attributes
JavaType is used to specify the type of attribute in pojo
OfType specifies the type that maps to pojo in the properties of the list collection
Where the collection of Role attributes is List
Pojo
Public class User implements Serializable {private Integer id; private String username; private String password; private String salt; private Integer status; / / user status 0: normal 1: disable 2: lock / / role collection private List roles
Role table
User table
Mapping table of user and role
ResultMap's understanding of column and property
First of all, take a look at this picture and see if you can see it at once:
The select element has a lot of attributes (more frequently used here)
Id: the namespace is uniquely identified and can be used to reference this statement
ParameterType: the fully qualified name or alias of the parameter class that will be passed in this statement
ResultType: the fully qualified name or alias of the class of the desired type to be returned from this statement (note here that the collection type should be a type that the collection can contain, not the collection itself), important: use resultType or resultMap, but not both.
ResultMap: the naming refers to the external resultMap, whose name matches the ID name of the external resultMap element, and is used to map its results to the entity class specified object.
When do we know to use resultMap and when do we use resultType again?
① can use resultType when going to select a table. In these cases, MyBatis automatically creates a ReusltMap behind the scenes and maps it to the JavaBean attribute based on the attribute name.
So when using resultMap, ② must write the xml corresponding to resultMap.
③ in order to facilitate our development errors, in the absence of special requirements, the column name can be completely consistent with the property name, otherwise, when we do not have a corresponding, the database can not match, so-and-so parameter value will be changed into uppercase, resulting in we originally get the lowercase parameter name, the result is not obtained, the background error report, the loss outweighs the gain!
Finally, I would like to say
In sql, for example, there is andm.id = # {id, jdbcType=VARCHAR} to indicate why jdbcType?
Explanation:
If you map a javabean,MyBatis, you can usually determine the type (a lot of things are written in the project, and I don't know why). However, if you map to, for example, HashMap, you should specify a jdbcType to guarantee the desired behavior.
After reading this, the article "how to use Colum and property of resultMap in Mybatis" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it. If you want to know more about the article, please follow 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.