In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "how to solve the problem of inconsistency between mybatis mapping and actual types". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to solve the problem of inconsistency between mybatis mapping and actual types".
The mybatis mapping is inconsistent with the actual type
The project has a problem today. It defines a query in dao, the return value of the method is map and defines that the generics are all String types, but there are other types in the return value of the method.
/ / API DAO queries the return type Map Map dealerInfo (String userId,String brandId)
The actual return type still has data that is not a String type.
Cause analysis
1. Generics act in the compilation phase, only to prevent type confusion, and type conversion exceptions.
2. Reflection is used when mybatis result sets encapsulate bean, which is carried out at run time.
Make a brief summary
Generics match our return value type to a specific type during the compilation phase, while the interface of the DAO layer has no specific return value information, so it can be passed during the compilation phase, that is to say, the return value generic of the interface we define in the DAO layer does not work, and the specific type still depends on the return value type defined in the mapper.xml file.
Solution method
Just convert the data to the corresponding type in the query sql.
SELECT CONVERT (23 Char); will convert 23 to string mybatis mapper Mapper (result mapping and resolution of column name inconsistencies) result mapping: (resultMap, resultType)
The resultMap element is the most important and powerful element in MyBatis. It frees you from 90% of the JDBC ResultSets data extraction code and in some cases allows you to do something that JDBC does not support. In fact, when writing mapping code for complex statements such as joins, a resultMap can replace thousands of lines of code that does the same thing. The design idea of ResultMap is that there is no need to configure explicit result mappings at all for simple statements, but only to describe their relationships for more complex statements.
The resultType property can specify the type of the result set, which holds the base type and entity class type (JavaBean or POJO (Plain Old Java Objects, plain old Java object))
It is important to note that it is similar to parameterType. If you have registered a type alias, you can alias it directly. Those who have not registered must have the class name fully qualified. For example, our entity class must be a fully qualified class name at this time
At the same time, when the entity class name is, there is another requirement that the attribute name in the entity class must remain consistent with the column name in the query statement, otherwise the method will achieve encapsulation.
1. ResultType configuration result type
This takes into account the fact that the attributes of the entity class and the column names of the database tables are not identical.
JavaBean:
Public class User {private int id; private String username; private String hashedPassword; public int getId () {return id;} public void setId (int id) {this.id = id;} public String getUsername () {return username;} public void setUsername (String username) {this.username = username;} public String getHashedPassword () {return hashedPassword;} public void setHashedPassword (String hashedPassword) {this.hashedPassword = hashedPassword;}}
Based on the JavaBean specification, the above class has three properties: id,username and hashedPassword. These properties correspond to the column names in the select statement.
Xml mapping
Type aliases are your good helper. With them, you don't have to enter the fully qualified name of the class. For example:
Select id, username, hashedPassword from some_table where id = # {id}
In these cases, MyBatis automatically creates a ResultMap behind the scenes and maps the column to the attribute of JavaBean based on the property name.
If the column name and attribute name do not exactly match, you can use aliases (a basic SQL feature) for the column in the SELECT statement to match the tag. For example:
Select user_id as "id", user_name as "userName", hashed_password as "hashedPassword" from some_table where id = # {id}
In this way, the attribute of the entity class and the column name of the database table can be solved.
Think about it: wouldn't it be troublesome to write if we have a lot of queries and aliases them? is there any other solution?
2. ResultMap result type
The resultMap tag can establish a correspondence between the column names of the query and the attribute names of the entity class. Implement encapsulation from slave. You can simply specify the reference to the resultMap attribute in the select tag. At the same time, resultMap can implement pojo that maps query results to complex types, such as including pojo and list in the query result mapping object to achieve "pair" query and "pair-to-multi" query.
Type attribute: specifies the fully qualified class name of the entity class
Id= "userResultMap": given a unique identity, it refers to the query select tag.
Label: in the specified primary key field
Label: in specified key field
Column attribute: dependent on the specified database column name
Property attribute: attribute name of the specified entity class
Just use the resultMap attribute in the statement that references it (notice that we removed the resultType attribute). For example:
Select user_id, user_name, hashed_password from some_table where id = # {id}
Using an external resultMap is another way to resolve the mismatch between the attributes of the entity class and the column names of the database table.
At this point, I believe you have a deeper understanding of "how to solve the problem of inconsistency between mybatis mapping and actual types". 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.
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.