更新
package top.mgy.mybatistest.com.mp;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import top.mgy.mybatistest.dao.UserMapper;
import top.mgy.mybatistest.entity.User;
@SpringBootTest
public class UpdateTest {
@Autowired
private UserMapper userMapper;
@Test
public void updateById(){
//根据id更新数据
User user = new User();
user.setId(1088248166370832385L);
user.setAge(26);
user.setEmail("123@qq.com");
int row = userMapper.updateById(user);
System.out.println("影响的行数:"+row);
}
@Test
public void updateByWrapper(){
//根据自定义字段更新数据
UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
//Wrapper中设置的条件 将出现在 where 后面
userUpdateWrapper.eq("name","李艺伟").eq("age",28);
// user 中设置的值将出现在 set中
User user = new User();
user.setEmail("lyq@qq.com");
user.setAge(29);
int row = userMapper.update(user, userUpdateWrapper);
System.out.println("受影响的行数:"+row);
}
@Test
public void updateByWrapper2(){
/**
* 简便方式 ,适合于修改字段较少的时候
*/
UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
// 直接通过 set 修改
userUpdateWrapper.eq("name","李艺伟").eq("age",29).set("age",30);
int row = userMapper.update(null, userUpdateWrapper);
System.out.println("受影响的行数:"+row);
}
@Test
public void updateByWrapper3(){
/**
* Lambda 方式
*/
LambdaUpdateWrapper<User> userLambdaUpdateWrapper = Wrappers.<User>lambdaUpdate();
userLambdaUpdateWrapper.eq(User::getName,"李艺伟").eq(User::getAge,30).set(User::getAge,31);
int row = userMapper.update(null, userLambdaUpdateWrapper);
System.out.println("受影响的行数:"+row);
}
@Test
public void updateByWrapper4(){
/**
* 链式调用
*/
boolean row = new LambdaUpdateChainWrapper<User>(userMapper)
.eq(User::getName, "李艺伟").eq(User::getAge, 31).set(User::getAge, 32).update();
System.out.println("是否更新成功:"+row);
}
}
删除
package top.mgy.mybatistest.com.mp;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import top.mgy.mybatistest.dao.UserMapper;
import java.util.HashMap;
import java.util.Map;
@SpringBootTest
public class DeleteTest {
@Autowired
private UserMapper userMapper;
@Test
public void deleteByUId(){
int row = userMapper.deleteById(1243763737180590081L);
System.out.println("受影响的行数:"+row);
}
@Test
public void deleteByMap(){
Map<String,Object> map = new HashMap<>();
map.put("name","向北");
map.put("age",31);
// 删除 name=向北 and age=31的记录
int row = userMapper.deleteByMap(map);
System.out.println("受影响的行数:"+row);
}
@Test
public void deleteBatchIds(){
//批量删除
int row = userMapper.deleteBatchIds(Arrays.asList(1243752778902867969L, 1243755707546284033L));
System.out.println("受影响的行数:"+row);
}
@Test
public void deleteWrapper(){
//查询构造器删除
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(User::getAge,31);
int row = userMapper.delete(lambdaQueryWrapper);
System.out.println("受影响的行数:"+row);
}
}