5.1 KiB
5.1 KiB
招生迎新数据库字段与实体类映射问题修复
问题描述
在招生统计模块开发过程中,发现后端Mapper.xml中使用的SQL查询字段与实体类映射存在不一致的情况,导致API无法正确返回数据。
具体问题:
- Mapper.xml中使用了
del_flag字段,而实体类中使用的是deleted字段 - 数据库表中的逻辑删除字段可能是
del_flag,但实体类属性是deleted - 前端调用API返回404错误,无法获取统计数据
问题解决方案
1. 修改后端SQL查询
需要更新以下Mapper.xml文件中的SQL语句:
文件1: ZsjyEnrollmentInfoMapper.xml
<!-- 统计总报名人数 -->
<select id="countTotal" resultType="java.lang.Long">
select count(1) from zsjy_enrollment_info
where deleted = '0'
</select>
<!-- 统计今日报名人数 -->
<select id="countTodayEnrollments" resultType="java.lang.Long">
select count(1) from zsjy_enrollment_info
where deleted = '0'
and date_format(application_time, '%Y-%m-%d') = date_format(now(), '%Y-%m-%d')
</select>
<!-- 按专业统计报名人数 -->
<select id="groupByMajor" resultType="java.util.Map">
select applied_major as name, count(1) as value
from zsjy_enrollment_info
where deleted = '0'
group by applied_major
order by value desc
</select>
<!-- 按地区统计报名人数 -->
<select id="groupByRegion" resultType="java.util.Map">
select exam_region as name, count(1) as value
from zsjy_enrollment_info
where deleted = '0'
group by exam_region
order by value desc
</select>
<!-- 按性别统计 -->
<select id="countByGender" resultType="java.util.Map">
select gender, count(1) as count
from zsjy_enrollment_info
where deleted = '0'
group by gender
</select>
<!-- 按日期范围统计报名人数 -->
<select id="countByDateRange" resultType="java.util.Map" parameterType="java.lang.Integer">
SELECT DATE_FORMAT(application_time, '%Y-%m-%d') as date, COUNT(1) as count
FROM zsjy_enrollment_info
WHERE deleted = '0'
AND application_time >= DATE_SUB(CURDATE(), INTERVAL #{days} DAY)
GROUP BY DATE_FORMAT(application_time, '%Y-%m-%d')
ORDER BY date
</select>
文件2: ZsjyEnrollmentProcessMapper.xml
<!-- 根据报名信息ID查询流程状态 -->
<select id="selectByEnrollmentId" resultType="org.dromara.zsjy.domain.vo.ZsjyEnrollmentProcessVo">
<include refid="selectZsjyEnrollmentProcessVo"/>
where enrollment_id = #{enrollmentId}
and deleted = '0'
limit 1
</select>
<!-- 根据缴费状态统计人数 -->
<select id="countByPaymentStatus" resultType="java.lang.Long">
select count(1) from zsjy_enrollment_process
where payment_status = #{status}
and deleted = '0'
</select>
<!-- 根据录取状态统计人数 -->
<select id="countByAdmissionStatus" resultType="java.lang.Long">
select count(1) from zsjy_enrollment_process
where admission_status = #{status}
and deleted = '0'
</select>
<!-- 按流程状态统计人数 -->
<select id="countByStatus" resultType="java.util.Map">
select
sum(case when current_status = '0' then 1 else 0 end) as pending,
sum(case when current_status = '1' then 1 else 0 end) as infoAuditing,
sum(case when current_status = '2' then 1 else 0 end) as processing,
sum(case when current_status = '3' then 1 else 0 end) as finalAuditing,
sum(case when current_status = '4' then 1 else 0 end) as completed,
sum(case when current_status = '9' then 1 else 0 end) as rejected
from zsjy_enrollment_process
where deleted = '0'
</select>
<!-- 查询当前用户待审核的流程 -->
<select id="selectMyAuditList" resultType="org.dromara.zsjy.domain.vo.ZsjyEnrollmentProcessVo">
select p.*
from zsjy_enrollment_process p
where p.current_handler = #{userId}
and p.current_status in ('1', '2', '3')
and p.deleted = '0'
order by p.create_time desc
</select>
2. 后端部署问题解决
如果数据库字段确实是del_flag而不是deleted,则需要修改实体类映射,使其与数据库字段名一致:
第一种方法:修改实体类的字段映射注解
@TableLogic
@TableField(value = "del_flag")
private String deleted;
第二种方法:统一修改数据库表结构,将del_flag修改为deleted
-- 修改zsjy_enrollment_info表
ALTER TABLE zsjy_enrollment_info CHANGE COLUMN del_flag deleted char(1) DEFAULT '0';
-- 修改zsjy_enrollment_process表
ALTER TABLE zsjy_enrollment_process CHANGE COLUMN del_flag deleted char(1) DEFAULT '0';
3. 前端临时解决方案
在后端API修复前,前端采用模拟数据方式确保页面正常显示,通过以下方法实现:
- 在页面加载时立即使用模拟数据初始化统计卡片和图表
- 同时尝试从后端获取真实数据,如果成功则更新显示
- 确保后端API路径正确,需要包含
/dev-api前缀
后续工作
- 与后端开发人员确认数据库字段命名规则
- 统一实体类和数据库表字段的命名
- 重新部署后端服务,验证API功能
- 更新API文档,确保准确性