foreword
When using Mybatis to query, the queried data may have one piece of data or multiple pieces of data. We have to specify the return type according to the number of returned data. If there are multiple records, the return value type cannot be an entity. kind.
There are multiple records queried
If there are multiple records queried, but the return type of our method is entity class, the following error will be reported
org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne()
because, when the return value type of our method is an entity class, the bottom layer will call the selectOne method, and the return data of this method can only be there is one
Summary: There is one record queried, and the return value type can be either an entity class or a collection. If there are multiple records, the return value type can be set to a List collection or a Map collection, but it must not be set to an entity class. Type, otherwise an error TooManyResultsException
Query the second SQL statement using the aggregate function
Sometimes we want to be able to query how many records there are, then the SQL statement uses the grouping function, then the result of the query should be a single row and a single column
/**
* 查询用户信息的总记录数
* @param
* @return
*/
Integer getCount();
Do you feel weird, why is it like this? The reason is because mybatis sets up type aliases
MyBatis has already taken good aliases for Java types (be careful to repeat when aliasing yourself)
MyBatis has built-in corresponding type aliases for many common Java types. They are all case-insensitive, and we must not occupy existing aliases when creating aliases.
Query 3 returns a value of type map
When the returned data is one piece
, if the result of our query does not have an entity class corresponding to it, we can set the return value type to the Map collection
. When the above query record is one, we can use the map to query the record. Let's see if there are multiple records, can we query them with the method just now?
Map<String,Object> getUserMap();
Solution: put multiple maps in the list collection
List<Map<String, Object>> getUserMapToList();
Does it mean that the map collection must be placed in the list collection, so that the multiple records queried can be output? The answer is no, we actually have a way, just through annotation
@MapKey("id")
Map<String, Object> getUserMap();
this is the official explanation .
Here we recommend a more detailed introduction, you can Check it out:
Mybatis Source Code Analysis: The Use of @MapkeySummary: If there are multiple records queried, we can have the following solutions:
① Receive through the List collection of the entity class type
② Receive through the List collection of the map type
③ Add the @MapKey annotation to the method of the mapper interface, this time You can use the map collection of each data conversion as a value, and use the value of a certain field as a key, and put it in the same map collection