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