Вот мой объект запроса, показанный ниже
Код: Выделить всё
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class QueryRequest implements Serializable {
private List years;
private List months;
private List region;
private List office;
}
Код: Выделить всё
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class TaskCountResponse implements Serializable {
private String region;
private String office;
private Map monthlyCounts;
}
Код: Выделить всё
@Repository
public interface QueryTaskDao {
List getTaskStatusCounts(QueryRequest queryRequest);
}
Код: Выделить всё
@Slf4j
public class MapTypeHandler extends BaseTypeHandler {
private static final ObjectMapper objectMapper = new ObjectMapper();
@Override
public void setNonNullParameter(PreparedStatement ps, int i, Map 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 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() {});
}
return null;
} catch (IOException e) {
throw new SQLException("Error converting JSON to Map", e);
}
}
@Override
public Map 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() {});
}
return null;
} catch (IOException e) {
throw new SQLException("Error converting JSON to Map", e);
}
}
@Override
public Map 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() {});
}
return null;
} catch (IOException e) {
throw new SQLException("Error converting JSON to Map", e);
}
}
}
Код: Выделить всё
AND SUBSTRING(tt.task_finish_time, 1, 4) IN
#{year}
AND SUBSTRING(tt.task_finish_time, 6, 2) IN
#{month}
AND tt.region_name IN
#{region}
AND tt.office_name IN
#{office}
SELECT
tt.region_name,
tt.office_name,
COUNT(CASE WHEN SUBSTRING(tt.task_finish_time, 1, 7) = CONCAT(#{year}, '-', #{month}) THEN 1 END) AS month_${year}_${month}
FROM
task_list tt
GROUP BY
tt.region_name,
tt.office_name
ORDER BY
tt.region_name_en ASC;
Код: Выделить всё
{
"years": ["2022", "2023"],
"months": ["01", "02", "03"],
"region": ["A Region", "B Region"],
"office": ["A Region Office", "B Region Office"]
}
Код: Выделить всё
[
{
"region": "A Region",
"office": "A Region Office",
"monthlyCounts": null
},
{
"region": "B Region",
"office": "B Region Office",
"monthlyCounts": 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
}
}
]
Можете ли вы пересмотреть его, чтобы исправить?
Подробнее здесь: https://stackoverflow.com/questions/792 ... map-object
Мобильная версия