gpt-4.1-mini-2025-04-14· OpenAI
WildChat-4.8M (AI2)
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD…
- Usercode
- Assistant
<resultMap id="PillExpiryWarningResult" type="com.ruoyi.pill.domain.PillExpiryWarning">
<result property="warningId" column="warning_id" />
<result property="drugId" column="drug_id" />
<result property="factoryId" column="factory_id" />
<result property="productionDate" column="production_date" />
<result property="validityPeriod" column="validity_period" />
<result property="purchaseId" column="purchase_id" />
<result property="stock" column="stock" />
<result property="createTime" column="create_time" />
<result property="drugName" column="drug_name"/>
<result property="factoryName" column="factory_name"/>
<result property="purchaseTime" column="purchase_time"/>
<result property="daysToExpire" column="days_to_expire"/>
</resultMap>
<sql id="BaseSelectSql">
select
pe.warning_id,
pe.drug_id,
pe.factory_id,
pe.production_date,
pe.validity_period,
pe.purchase_id,
pd.num as stock,
pe.create_time,
pd.drug_name,
pf.factory_name,
pp.purchase_time,
datediff(date_add(pe.production_date, interval pe.validity_period day), curdate()) as days_to_expire
from pill_expiry_warning pe
LEFT JOIN pill_drug pd ON pe.drug_id = pd.drug_id
LEFT JOIN pill_factory pf ON pe.factory_id = pf.factory_id
LEFT JOIN pill_purchase pp ON pe.purchase_id = pp.purchase_id
</sql>
<select id="selectPillExpiryWarningList" parameterType="com.ruoyi.pill.domain.PillExpiryWarning" resultMap="PillExpiryWarningResult">
<include refid="BaseSelectSql"/>
<where>
<if test="drugId != null "> and pe.drug_id = #{drugId}</if>
<if test="factoryId != null "> and pe.factory_id = #{factoryId}</if>
<if test="productionDate != null "> and pe.production_date = #{productionDate}</if>
<if test="validityPeriod != null "> and pe.validity_period = #{validityPeriod}</if>
<if test="purchaseId != null "> and pe.purchase_id = #{purchaseId}</if>
</where>
</select>
<select id="selectPillExpiryWarningByWarningId" parameterType="Long" resultMap="PillExpiryWarningResult">
<include refid="BaseSelectSql"/>
where pe.warning_id = #{warningId}
</select>
<insert id="insertPillExpiryWarning" parameterType="com.ruoyi.pill.domain.PillExpiryWarning" useGeneratedKeys="true" keyProperty="warningId">
insert into pill_expiry_warning
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="drugId != null">drug_id,</if>
<if test="factoryId != null">factory_id,</if>
<if test="productionDate != null">production_date,</if>
<if test="validityPeriod != null">validity_period,</if>
<if test="purchaseId != null">purchase_id,</if>
<if test="stock != null">stock,</if>
<if test="createTime != null">create_time,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="drugId != null">#{drugId},</if>
<if test="factoryId != null">#{factoryId},</if>
<if test="productionDate != null">#{productionDate},</if>
<if test="validityPeriod != null">#{validityPeriod},</if>
<if test="purchaseId != null">#{purchaseId},</if>
<if test="stock != null">#{stock},</if>
<if test="createTime != null">#{createTime},</if>
</trim>
</insert>
<update id="updatePillExpiryWarning" parameterType="com.ruoyi.pill.domain.PillExpiryWarning">
update pill_expiry_warning
<set>
<if test="drugId != null">drug_id = #{drugId},</if>
<if test="factoryId != null">factory_id = #{factoryId},</if>
<if test="productionDate != null">production_date = #{productionDate},</if>
<if test="validityPeriod != null">validity_period = #{validityPeriod},</if>
<if test="purchaseId != null">purchase_id = #{purchaseId},</if>
<if test="stock != null">stock = #{stock},</if>
<if test="createTime != null">create_time = #{createTime},</if>
</set>
where warning_id = #{warningId}
</update>
<delete id="deletePillExpiryWarningByWarningId" parameterType="Long">
delete from pill_expiry_warning where warning_id = #{warningId}
</delete>
<delete id="deletePillExpiryWarningByWarningIds" parameterType="Long[]">
delete from pill_expiry_warning where warning_id in
<foreach item="warningId" collection="array" open="(" separator="," close=")">
#{warningId}
</foreach>
</delete>
你的 MyBatis Mapper 看起来整体结构是正确的,但你提到“前端下拉框显示无数据”,这往往可能是以下几个方面的问题,不局限于 Mapper xml:
建议先确认数据库中是否有满足条件的数据:
BaseSelectSql 的 SQL,查看返回数据是否有值。例如:
select
pe.warning_id,
pe.drug_id,
pe.factory_id,
pe.production_date,
pe.validity_period,
pe.purchase_id,
pd.num as stock,
pe.create_time,
pd.drug_name,
pf.factory_name,
pp.purchase_time,
datediff(date_add(pe.production_date, interval pe.validity_period day), curdate()) as days_to_expire
from pill_expiry_warning pe
LEFT JOIN pill_drug pd ON pe.drug_id = pd.drug_id
LEFT JOIN pill_factory pf ON pe.factory_id = pf.factory_id
LEFT JOIN pill_purchase pp ON pe.purchase_id = pp.purchase_id
确认数据是否真的存在。
selectPillExpiryWarningList 有一些 <if test="xxx != null"> 条件,如果你前端传入了过滤条件且这些条件导致没有数据,结果也为空。<where> 条件,直接执行完整查询,看是否有数据返回,以此确定是否是参数过滤导致无数据。resultMap定义了多个映射字段,对应表中JOIN查询的列。stock 对应 pd.num,确认表和字段名是否正确,字段类型是否匹配。<where> 语句你写的 <where> 里条件都是:
<where>
<if test="drugId != null "> and pe.drug_id = #{drugId}</if>
<if test="factoryId != null "> and pe.factory_id = #{factoryId}</if>
<if test="productionDate != null "> and pe.production_date = #{productionDate}</if>
<if test="validityPeriod != null "> and pe.validity_period = #{validityPeriod}</if>
<if test="purchaseId != null "> and pe.purchase_id = #{purchaseId}</if>
</where>
<where> 会自动处理首个and,但是你这里and写在每个if元素内,有时会产生SQL语法问题,建议改为:
<where>
<if test="drugId != null">pe.drug_id = #{drugId}</if>
<if test="factoryId != null">and pe.factory_id = #{factoryId}</if>
<if test="productionDate != null">and pe.production_date = #{productionDate}</if>
<if test="validityPeriod != null">and pe.validity_period = #{validityPeriod}</if>
<if test="purchaseId != null">and pe.purchase_id = #{purchaseId}</if>
</where>
或者完全去掉and,依赖<where>自动添加:
<where>
<if test="drugId != null">pe.drug_id = #{drugId}</if>
<if test="factoryId != null">pe.factory_id = #{factoryId}</if>
<if test="productionDate != null">pe.production_date = #{productionDate}</if>
<if test="validityPeriod != null">pe.validity_period = #{validityPeriod}</if>
<if test="purchaseId != null">pe.purchase_id = #{purchaseId}</if>
</where>
这也可能避免你SQL语法错误。
parameterType是否指定正确,参数接受是否正常。<where>条件全表查询,看是否有数据。如果你需要,我可以帮你写一个无过滤条件的通用查询 SQL 或帮你写一个简单的 REST 接口示例用于测试查询。你可以再反馈你前端用到的调用代码,我帮你排查。
Not yet included in a published release.