update 更新
boolean update(Wrapper<T> updateWrapper);
boolean update(T entity, Wrapper<T> updateWrapper);
boolean updateById(T entity);
boolean updateBatchById(Collection<T> entityList);
boolean updateBatchById(Collection<T> entityList, int batchSize);
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("wtf2@baomidou.com");
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("lyw2019@baomidou.com");
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("lyw2019@baomidou.com");
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);
}
}
删除 delete
int delete(@Param(Constants.WRAPPER) Wrapper<T> wrapper);
int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
int deleteById(Serializable id);
int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
package com.mp;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest
public class DeleteTest {
@Autowired
private UserMapper userMapper;
@Test
public void deleteById() {
int rows = userMapper.deleteById(1088248166370832385L);
System.out.println("删除记录条数:" + rows);
}
@Test
public void deleteByMap() {
Map<String, Object> columnMap = new HashMap<>();
columnMap.put("name", "刘明强5");
columnMap.put("age", 31);
int rows = userMapper.deleteByMap(columnMap);
System.out.println("删除记录条数:" + rows);
}
@Test
public void deleteBatchIds() {
int rows = userMapper.deleteBatchIds(Arrays.asList(1209754139000852481L, 1209838254358319105L, 1209838329864204289L));
System.out.println("删除记录条数:" + rows);
}
@Test
public void updateByWrapper4() {
LambdaQueryWrapper<User> lambdaQuery = Wrappers.lambdaQuery();
lambdaQuery.gt(User::getAge, 35).lt(User::getAge, 40);
int rows = userMapper.delete(lambdaQuery);
System.out.println("影响记录条数:" + rows);
}
}