97 lines
2.9 KiB
Markdown
97 lines
2.9 KiB
Markdown
# 日志同步问题根本原因分析
|
||
|
||
## 🎯 问题根源
|
||
|
||
### 发现的关键问题
|
||
|
||
在 `XqClientLogMapper.xml` 文件中发现了**字段映射不一致**的严重问题:
|
||
|
||
#### 问题1:resultMap字段映射错误
|
||
|
||
```xml
|
||
<resultMap id="BaseResultMap" type="com.aiotagro.cattletrade.business.entity.XqClientLog">
|
||
<result column="battery" property="deviceVoltage" /> <!-- ❌ 错误:使用battery -->
|
||
<result column="temperature" property="deviceTemp" /> <!-- ❌ 错误:使用temperature -->
|
||
<result column="deviceld" property="serverDeviceId" /> <!-- ❌ 错误:使用deviceld -->
|
||
</resultMap>
|
||
```
|
||
|
||
#### 问题2:批量插入SQL使用不同的字段名
|
||
|
||
```xml
|
||
<insert id="batchInsert">
|
||
INSERT INTO xq_client_log (
|
||
device_voltage, device_temp, server_device_id, <!-- ✅ 正确:使用device_voltage, device_temp -->
|
||
...
|
||
)
|
||
</insert>
|
||
```
|
||
|
||
### 🔍 根本原因
|
||
|
||
**字段名不一致导致的问题:**
|
||
1. `resultMap` 使用的是旧的字段名(battery, temperature, deviceld)
|
||
2. 批量插入SQL使用的是新的字段名(device_voltage, device_temp, server_device_id)
|
||
3. 实体类 `XqClientLog.java` 使用的是新的字段名
|
||
|
||
这导致:
|
||
- 查询时:使用 `battery` 字段,但表中可能没有这个字段
|
||
- 插入时:使用 `device_voltage` 字段,这是正确的
|
||
- 但是数据截断错误可能是因为字段长度定义不足
|
||
|
||
## 🛠️ 解决方案
|
||
|
||
### 方案1:修复resultMap字段映射
|
||
|
||
将 `resultMap` 中的字段名修改为与实际表结构一致:
|
||
|
||
```xml
|
||
<resultMap id="BaseResultMap" type="com.aiotagro.cattletrade.business.entity.XqClientLog">
|
||
<result column="device_voltage" property="deviceVoltage" />
|
||
<result column="device_temp" property="deviceTemp" />
|
||
<result column="server_device_id" property="serverDeviceId" />
|
||
</resultMap>
|
||
```
|
||
|
||
### 方案2:修复Base_Column_List
|
||
|
||
```xml
|
||
<sql id="Base_Column_List">
|
||
id, device_id, device_voltage, device_temp, server_device_id,
|
||
latitude, longitude, walk_steps, y_walk_steps,
|
||
create_time, create_by, update_time, update_by
|
||
</sql>
|
||
```
|
||
|
||
### 方案3:修复查询SQL
|
||
|
||
```xml
|
||
<select id="xqLogList" resultMap="BaseResultMap"> <!-- 使用resultMap而不是resultType -->
|
||
SELECT
|
||
<include refid="Base_Column_List"/>
|
||
FROM
|
||
xq_client_log log
|
||
WHERE
|
||
log.device_id IN (...)
|
||
AND log.create_time BETWEEN #{createTime} AND #{checkTime}
|
||
ORDER BY log.create_time DESC
|
||
</select>
|
||
```
|
||
|
||
## 📋 实施步骤
|
||
|
||
1. **步骤1**:修复 `XqClientLogMapper.xml` 中的字段映射
|
||
2. **步骤2**:重新编译和打包应用
|
||
3. **步骤3**:重启Java后端应用(端口16200)
|
||
4. **步骤4**:清空 `xq_client_log` 表
|
||
5. **步骤5**:测试手动同步功能
|
||
6. **步骤6**:验证60分钟自动同步功能
|
||
|
||
## 🎯 预期结果
|
||
|
||
修复后应该能够:
|
||
- ✅ 成功批量插入项圈日志数据
|
||
- ✅ 正确查询项圈日志数据
|
||
- ✅ 60分钟自动同步功能正常工作
|
||
|