在患者提醒数据管理功能中,API响应中的Boolean字段(如notificationEnabled、bloodPressureEnabled等)始终返回null,尽管数据库中存储的是正确的TINYINT(1)值(1表示true,0表示false)。
id: 1991527305027313666
patient_user_id: 1988147181088956418
is_notification_enabled: 1
is_subscription_available: 1
is_blood_pressure_enabled: 0
is_blood_sugar_enabled: 0
is_heart_rate_enabled: 0
is_medication_enabled: 0
{
"code": 200,
"message": "ok",
"data": {
"reminder": {
"id": "1991527305027313666",
"patientUserId": "1988147181088956418",
"notificationEnabled": null,
"subscriptionAvailable": null,
"bloodPressureEnabled": null,
"bloodSugarEnabled": null,
"heartRateEnabled": null,
"medicationEnabled": null,
"bloodPressureTimes": [],
"bloodSugarTimes": [],
"heartRateTimes": [],
"medicationEnabled": null,
"createTime": "2025-11-20T23:21:25"
}
}
}
CAST(field AS SIGNED)或field + 0强制转换为数字SELECT *,说明可能有缓存或配置问题通过调试日志发现,MyBatis查询确实返回了正确的数据:
==> Preparing: SELECT * FROM t_patient_reminder WHERE patient_user_id = ?
==> Parameters: 1988147181088956418(Long)
<== Columns: ..., 1, 1, 0, <<BLOB>>, 0, <<BLOB>>, 0, <<BLOB>>, 0, ...
但PO对象中的Boolean字段仍然为null。这说明MyBatis无法将TINYINT(1)正确映射到Java的Boolean类型。
MySQL Connector/J 5.1.49版本对TINYINT(1)的处理方式与预期不符。在某些情况下,TINYINT(1)被映射为其他类型而不是Boolean。
// 原来
private Boolean notificationEnabled;
// 修改后
private Byte notificationEnabled;
// 查询时转换
reminderResponse.setNotificationEnabled(
patientReminder.getNotificationEnabled() != null &&
patientReminder.getNotificationEnabled() == 1
);
// 保存时转换
patientReminder.setNotificationEnabled(
request.getNotificationEnabled() ? (byte)1 : (byte)0
);
// 原来
@Select("SELECT ... FROM t_patient_reminder WHERE patient_user_id = #{patientUserId}")
PatientReminder selectByPatientUserId(@Param("patientUserId") Long patientUserId);
// 修改后
PatientReminder selectByPatientUserId(@Param("patientUserId") Long patientUserId);
// 在Service中使用QueryWrapper查询
修复后API响应正确:
{
"notificationEnabled": true,
"subscriptionAvailable": true,
"bloodPressureEnabled": false,
"bloodSugarEnabled": false,
"heartRateEnabled": false,
"medicationEnabled": false
}