记一次sql错误(自动更新时间冲突)

在项目收尾阶段,作者在测试逻辑删除操作时发现表中的时间字段未自动更新,即使操作成功。通过检查数据结构、同事的代码(包括UserServiceImpl、UserMapper和UserMapper.xml)以及测试类,发现问题出在update_time字段已有值导致更新失效。解决方案是修改service代码或mapper.xml,确保在更新时正确处理update_time字段。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近项目到收尾,由于没有测试人员,只能自己去测试;在测试同事的代码时,发现无论怎么更新(逻辑删除操作),表种的时间不会自动更新;
最开始以为自己憋出来的数据有问题,但是每次结果显示都是success,然后去数据库直接操作,时间也是会更新的;
以下使用模拟数据和表;
一、数据结构如下
user表
表结构

里面有两条数据
user数据
同事代码:
1、UserServiceImpl

package com.liuzm.service.impl;

import com.liuzm.bean.User;
import com.liuzm.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl {

    @Autowired
    UserMapper userMapper;

    public void DeleteUserById(String id){
        User user = userMapper.selectById(id);
        if(user != null ){
            user.setIsDelete("0");
            int i = userMapper.updateUser(user);
            if(i == 1){
                System.out.println("删除成功");
            }
        }
    }
}

2.UserMapper

package com.liuzm.mapper;

import com.liuzm.bean.User;
import java.util.List;

public interface UserMapper {
    int insert(User record);

    List<User> selectAll();

    User selectById(String id);

    int updateUser(User user);
}

UserMapper.xml

  <update id="updateUser" parameterType="com.liuzm.bean.User">
    update user_table set
    <if test="name != null">name = #{name}, </if>
    <if test="email != null">email = #{email}, </if>
    <if test="gender != null">gender = #{gender}, </if>
    <if test="isDelete != null">is_delete = #{isDelete}, </if>
    <if test="createTime != null">create_time = #{createTime}, </if>
    <if test="updateTime != null"> update_time = #{updateTime} </if>
    where id = #{id}
  </update>

    <select id="selectById" resultType="com.liuzm.bean.User">
    select id, name, email, gender, is_delete as isDelete, create_time as createTime, update_time as updateTime
    from user_table
    where id = #{id} limit 1;
    </select>

4、test类

package com.liuzm;

import com.liuzm.service.impl.UserServiceImpl;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;


@RunWith(SpringRunner.class)
@SpringBootTest(classes=MybatisApplication.class)
public class SqlTest {

    @Autowired
    UserServiceImpl userService;

    @Test
    public void test(){
        userService.DeleteUserById("1");
    }
}

5、运行结果
i.控制台打印

==>  Preparing: update user_table set name = ?, email = ?, gender = ?, is_delete = ?, create_time = ?, update_time = ? where id = ? 
==> Parameters: root(String), root@163.com(String), 1(String), 0(String), 2020-09-01 13:44:39.0(Timestamp), 2020-09-07 17:12:09.0(Timestamp), 1(Integer)
<==    Updates: 1

2数据库数据
在这里插入图片描述
可以看出第一条数据的更新时间并没有变化;我认为是在更新时update_time有值的话会优于脚本执行;

修改一下service代码或者mapper.xml文件就可以解决:
service代码
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值