1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| package com.mp;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.service.additional.update.impl.LambdaUpdateChainWrapper; import com.mp.dao.UserMapper; import com.mp.entity.User; 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;
import java.time.LocalDateTime;
@RunWith(SpringRunner.class) @SpringBootTest public class UpdateTest {
@Autowired private UserMapper userMapper;
@Test public void updateById() { User user = new User(); user.setId(1088248166370832385L); user.setAge(26); user.setEmail("[email protected]"); int rows = userMapper.updateById(user); System.out.println("影响记录条数:" + rows); }
@Test public void updateByWrapper() { UpdateWrapper<User> updateWrapper = new UpdateWrapper<>(); updateWrapper.eq("name", "李艺伟").eq("age", 28); User user = new User(); user.setEmail("[email protected]"); user.setAge(29); int rows = userMapper.update(user, updateWrapper); System.out.println("影响记录条数:" + rows); }
@Test public void updateByWrapper3() { UpdateWrapper<User> updateWrapper = new UpdateWrapper<>(); updateWrapper.eq("name", "李艺伟").eq("age", 29).set("age", 30); User user = new User(); user.setEmail("[email protected]"); user.setAge(29); int rows = userMapper.update(user, updateWrapper); System.out.println("影响记录条数:" + rows); }
@Test public void updateByWrapper4() { LambdaUpdateWrapper<User> lambdaUpdateWrapper = Wrappers.lambdaUpdate(); lambdaUpdateWrapper.eq(User::getName, "李艺伟").eq(User::getAge, 30).set(User::getAge, 31);
int rows = userMapper.update(null, lambdaUpdateWrapper); System.out.println("影响记录条数:" + rows); }
@Test public void updateByWrapper5() { boolean update = new LambdaUpdateChainWrapper<>(userMapper) .eq(User::getName, "李艺伟").eq(User::getAge, 31).set(User::getAge, 32).update(); System.out.println(update); } }
|