Mybatis search returns the data method of Map, List collection type


Mybatis search returns data of Map, List collection type

1. Find the List collection of returned Bean objects

Basically, it is no different from returning a Bean object. The resultType is still the full class name of the Bean object, but the method type in the interface needs to be modified.

public List<Employee> getEmpListByEmail(String email);

Second, the query returns the Map collection of Bean objects

Similarly, resultType is still the full class name of the Bean object, but the method type in the interface needs to be modified and annotations added.

@MapKey(“Bean object property name”): Specify which property of the Bean object the key of the Map is, generally set as the primary key, because the Map cannot store duplicate keys.

public Map<Integer,Employee> getEmpMapByEmail(String email);

3. The query returns the Map collection of a single record

That is, the current record uses the key as the column name, and the value is stored in the map as the column attribute (there must be only one record queried, otherwise an error will be reported)

Note that the resultType needs to be set to map, the method type in the interface needs to be modified, and the key of the map defaults to the column name.

public Map<String, Object> getEmpByEmail(String email);

Mybatis query returns List collection, map collection, List<Map> collection

return map type

1. in xml

<select  resultType="java.util.HashMap">  

2.Dao interface

Map<String,Object> selectUser(); 

In this way, SQL can only return one row of records or none. If multiple rows of records are returned, the program will report an error.

return List<String> type

3. in xml

<select  resultType="java.lang.String">  

2.Dao interface

List<String> selectUser(); 

This method can return multiple rows of records, but each row of records only has a specified column of data.

return List<Map> type


<select  resultType="java.util.HashMap">  

2.Dao interface

List<Map<String,Object>> selectUser ();

This method can return the specified multi-row and multi-column records.

Returns the type of List<specified object>


< resultMap type = "com. XXX. BscntrUnitInfoResult (object)" >
  <result column="unit_id" property="unitId" jdbcType="INTEGER" (field mapping)/>
  <result column="unit_name" property="unitName"
   jdbcType="VARCHAR" />
  <result column="unit_type" property="unitType"
   jdbcType="INTEGER" />
  <result column="super_unit_id" property="superUnitId"
   jdbcType="INTEGER" />
  <result column="gis_start_x" property="gisStartX"
   jdbcType="FLOAT" />
  <result column="ext_top" property="extTop" jdbcType="DOUBLE" />
<select resultMap="baseResult">

In the Dao interface:

public List<BscntrUnitInfoResult> getBscntrUnitInfoListByName();

The above is a personal experience, I hope it can give you a reference, and I hope you can support developpaer a lot.