MyBatis handler Issue in my Spring Boot Example for Map Object

I have a problem to implement custom handler for Map through MyBatis in my Spring Boot example.

Here is my request object shown below

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class QueryRequest implements Serializable {

    private List<String> years;
    private List<String> months;
    private List<String> region;
    private List<String> office;

}

Here is the response object shown below

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class TaskCountResponse implements Serializable {

    private String region;
    private String office;
    private Map<String, Integer> monthlyCounts;

}

Here is the dao shown below

@Repository
public interface QueryTaskDao {

    List<TaskCountResponse> getTaskStatusCounts(QueryRequest queryRequest);
}

Here is the custom handler for Map<String,Integer> shown below

@Slf4j
public class MapTypeHandler extends BaseTypeHandler<Map<String, Integer>> {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Map<String, Integer> parameter, JdbcType jdbcType) throws SQLException {
        try {
            String json = objectMapper.writeValueAsString(parameter);
            log.info("MapTypeHandler | setNonNullParameter | json : " + json);
            ps.setString(i, json);
        } catch (IOException e) {
            throw new SQLException("Error converting Map to JSON", e);
        }
    }

    @Override
    public Map<String, Integer> getNullableResult(ResultSet rs, String columnName) throws SQLException {
        try {
            String json = rs.getString(columnName);
            log.info("MapTypeHandler | getNullableResult(ResultSet rs, String columnName) | json : " + json);
            if (json != null) {
                return objectMapper.readValue(json, new TypeReference<Map<String, Integer>>() {});
            }
            return null;
        } catch (IOException e) {
            throw new SQLException("Error converting JSON to Map", e);
        }
    }

    @Override
    public Map<String, Integer> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        try {
            String json = rs.getString(columnIndex);
            log.info("MapTypeHandler | getNullableResult(ResultSet rs, int columnIndex) | json : " + json);
            if (json != null) {
                return objectMapper.readValue(json, new TypeReference<Map<String, Integer>>() {});
            }
            return null;
        } catch (IOException e) {
            throw new SQLException("Error converting JSON to Map", e);
        }
    }

    @Override
    public Map<String, Integer> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        try {
            String json = cs.getString(columnIndex);
            log.info("MapTypeHandler | getNullableResult(CallableStatement cs, int columnIndex) | json : " + json);
            if (json != null) {
                return objectMapper.readValue(json, new TypeReference<Map<String, Integer>>() {});
            }
            return null;
        } catch (IOException e) {
            throw new SQLException("Error converting JSON to Map", e);
        }
    }
}

Here is the XML part of mybatis

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.dao.QueryTaskDao">
    <cache/>

    <!-- Result Map to map query results to TaskStatusCountResponse -->
    <resultMap id="taskStatusCountResultMap" type="com.example.model.TaskStatusCountResponse">
        <result property="region" column="region_name"/>
        <result property="office" column="office_name"/>
        <!-- Handling dynamic columns for monthlyCounts -->
        <result property="monthlyCounts" column="monthlyCounts" javaType="java.util.Map" typeHandler="com.example.utils.MapTypeHandler"/>
    </resultMap>

    <!-- SQL fragment for dynamic filtering -->
    <sql id="queryTaskStatusCondition">
        <where>
            <!-- Filter by years -->
            <if test="years != null and !years.isEmpty()">
                AND SUBSTRING(tt.task_finish_time, 1, 4) IN
                <foreach collection="years" item="year" open="(" close=")" separator=",">
                    #{year}
                </foreach>
            </if>

            <!-- Filter by months -->
            <if test="months != null and !months.isEmpty()">
                AND SUBSTRING(tt.task_finish_time, 6, 2) IN
                <foreach collection="months" item="month" open="(" close=")" separator=",">
                    #{month}
                </foreach>
            </if>

            <!-- Filter by region -->
            <if test="region != null and !region.isEmpty()">
                AND tt.region_name IN
                <foreach collection="region" item="region" open="(" close=")" separator=",">
                    #{region}
                </foreach>
            </if>

            <!-- Filter by office -->
            <if test="office != null and !office.isEmpty()">
                AND tt.office_name IN
                <foreach collection="office" item="office" open="(" close=")" separator=",">
                    #{office}
                </foreach>
            </if>
        </where>
    </sql>

    <!-- Main SQL query to get Task Status Counts -->
    <select id="getTaskStatusCounts" resultMap="taskStatusCountResultMap" parameterType="com.example.model.QueryRequest">
        SELECT
        tt.region_name,
        tt.office_name,

        <!-- Dynamically create counts for each year-month combination -->
        <trim prefix="" suffix="" suffixOverrides=",">
            <foreach collection="years" item="year" separator=",">
                <foreach collection="months" item="month" separator=",">
                    COUNT(CASE WHEN SUBSTRING(tt.task_finish_time, 1, 7) = CONCAT(#{year}, '-', #{month}) THEN 1 END) AS month_${year}_${month}
                </foreach>
            </foreach>
        </trim>

        FROM
        task_list tt
        <include refid="queryTaskStatusCondition"/>
        GROUP BY
        tt.region_name,
        tt.office_name
        ORDER BY
        tt.region_name_en ASC;
    </select>
</mapper>

When I send a request to http://localhost:8048/statistic/getTaskCountsbyTime

{
    "years": ["2022", "2023"],
    "months": ["01", "02", "03"],
    "region": ["A Region", "B Region"],
    "office": ["A Region Office", "B Region Office"]
}

I get this response

[
    {
        "region": "A Region",
        "office": "A Region Office",
        "monthlyCounts": null
    },
    {
        "region": "B Region",
        "office": "B Region Office",
        "monthlyCounts": null
    }
]

The response should look something like this (with actual counts in place of null):

[
    {
        "region": "A Region",
        "office": "A Region Office",
        "monthlyCounts": {
            "month_2022_01": 10,
            "month_2022_02": 15,
            "month_2022_03": 12,
            "month_2023_01": 5,
            "month_2023_02": 7,
            "month_2023_03": 6
        }
    },
    {
        "region": "B Region",
        "office": "B Region Office",
        "monthlyCounts": {
            "month_2022_01": 8,
            "month_2022_02": 14,
            "month_2022_03": 13,
            "month_2023_01": 4,
            "month_2023_02": 6,
            "month_2023_03": 9
        }
    }
]

I get null of monthlyCounts .Where is the problem in xml mapper or mapper type handler?
Can you revise it to fix ?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật