mybatis映射map集合

简单的map映射就不介绍了,这里只介绍复杂的map映射,比如我的数据类型是这样的
在这里插入图片描述
前端需要映射成一个map里面6个键值对
比如键是pm2_5 值是96.52727020240256

数据结构
mybatis映射map集合_第1张图片
mybatis映射map集合_第2张图片
要把这些数据映射到pollutantList里面

这里说明一下,在mybatis里面不能完全处理,需要借助java
mybatis代码

	<resultMap id="AirDayReportMap" type="com.swcote.sensor.dto.DistrictDayAqiReport">
        <result property="districtCode" column="districtCode"/>
        <result property="aqi" column="aqi"/>
        <result property="aqiGrade" column="aqiGrade"/>
        <result property="primaryPollutant" column="primaryPollutant"/>
        <result property="reportTime" column="reportTime"/>
        <collection property="pollutantList" ofType="HashMap" javaType="ArrayList">
            <result property="pm2_5" column="pm2_5"/>
            <result property="o3" column="o3"/>
            <result property="no2" column="no2"/>
            <result property="so2" column="so2"/>
            <result property="pm10" column="pm10"/>
            <result property="co" column="co"/>
        collection>
    resultMap>
    
    <select id="getAllDistrictDayAqis" resultMap="AirDayReportMap">
        SELECT
            district_code as "districtCode",
            aqi,
            aqi_grade as "aqiGrade",
            primary_pollutant as "primaryPollutant",
            report_time as "reportTime",
            pm2_5 as "pm2_5",
            o3 as "o3",
            no2 as "no2",
            so2 as "so2",
            pm10 as "pm10",
            co as "co",
            time
        FROM aqi_sensor_day
        WHERE time = #{day}
    select>

这里映射过来是list>集合
加上@JsonIgnore保证前端不用获取PollutantList,当然获取了也没关系

 @JsonIgnore
 public List<Map<String, Number>> getPollutantList() {
      return pollutantList;
  }

因为前端请求的字段是pollutants
会调用getPollutants方法

public Map<String, Number> getPollutants() {
        Map<String, Number> resultMap = new HashMap<>();
        for (Map<String, Number> pollutant : pollutantList) {
            resultMap.putAll(pollutant);
        }
        return resultMap;
    }

在这个get方法里面转换我们从数据库获取的字段就可以了就可以了

你可能感兴趣的:(mybatis)