MyBatis queries MySQL to return List<Map> or Map type data operations

  MyBatis queries the MySQL database, and the returned result can be a specific class, Map, List<Map>, etc. The advantage of returning the query result to the Map type is that there is no need to create an additional class for this query. If you only query some specific columns and don't want to create additional classes, you can return the results to Map or List<Map>.
  If it is clear that there is only one record in the query result, return Map; if there may be multiple records in the query result, return List<Map>.
  The implementation method is relatively simple, as long as the resultType is set instead of the resultMap in the DAO layer XML file. See examples below.

1. The query result returns Map

  If it is clear that the query result has at most one record (such as searching based on the primary key), the result can be returned to Map. The key of the Map result is equal to the column name of the queried database. The number of keys in the Map is equal to the number of columns queried. The specific Mapper.xml, Mapper.java and test results are shown below.

1.1 Mapper.xml
  <select id="selectOneByPK" resultType="java.util.HashMap">
    select mchnt_id, mchnt_nm, brand_id, brand_nm
    from tbl_test_hotel
    where mchnt_id = #{mchntId,jdbcType=VARCHAR}
  </select>
1.2 Mapper.java
  Map<String, Object> selectOneByPK(String mchntId);
1.3 Test results

  Search based on the primary key and find one result. Because 4 column data of mchnt_id, mchnt_nm, brand_id, brand_nm are queried , the Map result contains 4 keys. For the convenience of viewing the results, the test results are formatted as follows.

{
	"mchnt_nm": "85度C东方路店",
	"mchnt_id": "22223333",
	"brand_nm": "85度C",
	"brand_id": "00001"
}
2. The query result returns List<Map>

  If it is foreseeable that there may be multiple records in the query result, return the result to List<Map>. The key of each Map is equal to the column name of the database to be queried, and the number of keys of the Map is equal to the number of columns to be queried. The specific Mapper.xml, Mapper.java and test results are shown below.

2.1 Mapper.xml
  <select id="selectHotels" resultType="java.util.HashMap">
    select mchnt_nm, brand_id, brand_nm
    from tbl_test_hotel
    where brand_nm = #{brandNm,jdbcType=VARCHAR} limit 3
  </select>
2.2 Mapper.java
  List<Map<String, Object>> selectHotels(String brandNm);
2.3 Test results

Because the 3 columns of mchnt_nm, brand_id, and brand_nm   are queried , the Map result contains 3 keys. Search based on non-primary attributes, and return a List collection when multiple records are found. For the convenience of viewing the results, the test results are formatted as follows.

[{
	"mchnt_nm": "85度C东方路店",
	"brand_nm": "85度C",
	"brand_id": "00001"
}, {
	"mchnt_nm": "85度C西方路店",
	"brand_nm": "85度C",
	"brand_id": "00001"
}, {
	"mchnt_nm": "85度C南方路店",
	"brand_nm": "85度C",
	"brand_id": "00001"
}]
Article reference:

Mybatis query implementation returns List类型数据操作

Guess you like

Origin blog.csdn.net/piaoranyuji/article/details/126777699