<insert id="insertListSelective"> INSERT INTO art_customer_leads_pool <trim prefix="(" suffix=")" suffixOverrides=","> <if test="item.id != null">id,</if> <if test="item.activityId != null">activity_id,</if> <if test="item.activityName != null">activity_name,</if> <if test="item.comCode != null">com_code,</if> <if test="item.comName != null">com_name,</if> <if test="item.customerName != null">customer_name,</if> <if test="item.licenseNo != null">license_no,</if> <if test="item.expiryMonth != null">expiry_month,</if> <if test="item.mobile != null">mobile,</if> <if test="item.referrerStaffNo != null">referrer_staff_no,</if> <if test="item.referrerType != null">referrer_type,</if> <if test="item.status != null">status,</if> <if test="item.source != null">source,</if> <if test="item.leadInboundTime != null">lead_inbound_time,</if> <if test="item.createTime != null">create_time,</if> <if test="item.updateTime != null">update_time,</if> </trim> VALUES <foreach collection="records" item="item" separator=","> ( <trim prefix="(" suffix=")" suffixOverrides=","> <if test="item.id != null">#{item.id,jdbcType=BIGINT},</if> <if test="item.activityId != null">#{item.activityId,jdbcType=VARCHAR},</if> <if test="item.activityName != null">#{item.activityName,jdbcType=VARCHAR},</if> <if test="item.comCode != null">#{item.comCode,jdbcType=VARCHAR},</if> <if test="item.comName != null">#{item.comName,jdbcType=VARCHAR},</if> <if test="item.customerName != null">#{item.customerName,jdbcType=VARCHAR},</if> <if test="item.licenseNo != null">#{item.licenseNo,jdbcType=VARCHAR},</if> <if test="item.expiryMonth != null">#{item.expiryMonth,jdbcType=DATE},</if> <if test="records.mobile != null">#{item.mobile,jdbcType=VARCHAR},</if> <if test="item.referrerStaffNo != null">#{item.referrerStaffNo,jdbcType=VARCHAR},</if> <if test="item.referrerType != null">#{item.referrerType,jdbcType=VARCHAR},</if> <if test="item.status != null">#{item.status,jdbcType=INTEGER},</if> <!-- 假设是整数 --> <if test="item.source != null">#{item.source,jdbcType=VARCHAR},</if> <if test="item.leadInboundTime != null">#{item.leadInboundTime,jdbcType=TIMESTAMP},</if> <if test="item.createTime != null">#{item.createTime,jdbcType=TIMESTAMP},</if> <if test="item.updateTime != null">#{item.updateTime,jdbcType=TIMESTAMP}</if> </trim> ) </foreach> </insert>,帮我修改一下这个方法让他可以正常运行 int insertListSelective(@Param("item") List<ArtCustomerLeadsPool> record);
时间: 2025-05-11 12:54:03 浏览: 26
当然可以帮你修改这段代码,使其能够正常工作。首先需要明确的是,`@Param("item")` 参数名称应该与你在映射文件中使用的 `collection="records"` 相匹配,并且你应该传递一个列表给它。此外,在动态字段插入的情况下,最好将所有列名及其对应的值放在两个独立的部分处理。
以下是改进后的Mapper.xml 和 Mapper.java 文件内容:
### Mapper.xml
```xml
<insert id="insertListSelective">
INSERT INTO art_customer_leads_pool
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="record[0].id != null">id,</if>
<if test="record[0].activityId != null">activity_id,</if>
<if test="record[0].activityName != null">activity_name,</if>
<if test="record[0].comCode != null">com_code,</if>
<if test="record[0].comName != null">com_name,</if>
<if test="record[0].customerName != null">customer_name,</if>
<if test="record[0].licenseNo != null">license_no,</if>
<if test="record[0].expiryMonth != null">expiry_month,</if>
<if test="record[0].mobile != null">mobile,</if>
<if test="record[0].referrerStaffNo != null">referrer_staff_no,</if>
<if test="record[0].referrerType != null">referrer_type,</if>
<if test="record[0].status != null">status,</if>
<if test="record[0].source != null">source,</if>
<if test="record[0].leadInboundTime != null">lead_inbound_time,</if>
<if test="record[0].createTime != null">create_time,</if>
<if test="record[0].updateTime != null">update_time,</if>
</trim>
VALUES
<foreach collection="record" item="item" separator=",">
(
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="item.id != null">#{item.id,jdbcType=BIGINT},</if>
<if test="item.activityId != null">#{item.activityId,jdbcType=VARCHAR},</if>
<if test="item.activityName != null">#{item.activityName,jdbcType=VARCHAR},</if>
<if test="item.comCode != null">#{item.comCode,jdbcType=VARCHAR},</if>
<if test="item.comName != null">#{item.comName,jdbcType=VARCHAR},</if>
<if test="item.customerName != null">#{item.customerName,jdbcType=VARCHAR},</if>
<if test="item.licenseNo != null">#{item.licenseNo,jdbcType=VARCHAR},</if>
<if test="item.expiryMonth != null">#{item.expiryMonth,jdbcType=DATE},</if>
<if test="item.mobile != null">#{item.mobile,jdbcType=VARCHAR},</if>
<if test="item.referrerStaffNo != null">#{item.referrerStaffNo,jdbcType=VARCHAR},</if>
<if test="item.referrerType != null">#{item.referrerType,jdbcType=VARCHAR},</if>
<if test="item.status != null">#{item.status,jdbcType=INTEGER},</if>
<if test="item.source != null">#{item.source,jdbcType=VARCHAR},</if>
<if test="item.leadInboundTime != null">#{item.leadInboundTime,jdbcType=TIMESTAMP},</if>
<if test="item.createTime != null">#{item.createTime,jdbcType=TIMESTAMP},</if>
<if test="item.updateTime != null">#{item.updateTime,jdbcType=TIMESTAMP}</if>
</trim>
)
</foreach>
</insert>
```
### Mapper.java (接口)
```java
public interface ArtCustomerLeadsPoolMapper {
int insertListSelective(@Param("record") List<ArtCustomerLeadsPool> records);
}
```
#### 修改点说明:
1. **参数一致**:确保了Java接口方法接收的参数名为 `record`, 与 XML 中的 `<foreach collection="record"...>` 匹配。
2. **检查首个元素**: 在定义表结构(即列名部分),通过测试第一个记录是否存在某个属性来确定是否包含该字段。
3. **优化JDBC类型设置**:对于字符串类型的字段指定了适当的 JDBC 类型如 VARCHAR;日期时间戳相关的则指定 DATE 或 TIMESTAMP 等。
4. **移除冗余条件**:修复了一个错误——原本对 `records.mobile` 的检测应当是指向当前迭代项 `item`.
请注意,以上提供的解决方案基于标准做法进行了简化和规范化调整。实际应用时还应考虑数据库性能、事务管理等更多细节问题。
阅读全文
相关推荐





