更新操作方法实战
@Test
public void updateById(){
User user = new User();
user.setId(2L);
user.setAge(21);
user.setEmail("abc@qq.com");
int rows = userMapper.updateById(user);
System.out.println("影响记录数:"+rows);
}
@Test
public void updateByWrapper(){
//此条件出现在where后
UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("name","Jom").eq("age",28);
//此条件出现在set后
User user = new User();
user.setAge(29);
user.setEmail("abc@qq.com");
int rows = userMapper.update(user,updateWrapper);
System.out.println("影响记录数:"+rows);
}
// UPDATE user SET age=?, email=? WHERE age=? AND email=? AND (name = ? AND age = ?)
@Test
public void updateByWrapper2(){
//此条件设置后放到updateWrapper构造函数后,会出现在where后面
User whereUser = new User();
whereUser.setEmail("abc2@qq.com");
//此条件出现在where后,会和上面的条件重复出现
UpdateWrapper<User> updateWrapper = new UpdateWrapper<>(whereUser);
updateWrapper.eq("name","Jom").eq("age",28);
//此条件出现在set后
User user = new User();
user.setAge(29);
user.setEmail("abc@qq.com");
int rows = userMapper.update(user,updateWrapper);
System.out.println("影响记录数:"+rows);
}
// UPDATE user SET age=? WHERE (name = ? AND age = ?)
@Test
public void updateByWrapper3(){
//此条件出现在where后,set字段少时可以直接加到后面
UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("name","Jom").eq("age",29).set("age",30);
int rows = userMapper.update(null,updateWrapper);
System.out.println("影响记录数:"+rows);
}
// UPDATE user SET age=? WHERE (name = ? AND age = ?)
@Test
public void updateByWrapperLambda(){
//此条件出现在where后,set字段少时可以直接加到后面
// UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
// updateWrapper.eq("name","Jom").eq("age",29).set("age",30);
//lambda写法
LambdaUpdateWrapper<User> lambdaUpdateWrapper = Wrappers.<User>lambdaUpdate();
lambdaUpdateWrapper.eq(User::getName,"Jom").eq(User::getAge,30).set(User::getAge,31);
int rows = userMapper.update(null,lambdaUpdateWrapper);
System.out.println("影响记录数:"+rows);
}
// true
@Test
public void updateByWrapperLambdaChain(){
//lambda的链式写法
boolean update = new LambdaUpdateChainWrapper<User>(userMapper)
.eq(User::getName,"Jom").eq(User::getAge,31).set(User::getAge,32).update( );
System.out.println("更新是否成功:"+update);
}
删除操作方法实战
// DELETE FROM user WHERE id=?
@Test
public void deleteById(){
int rows = userMapper.deleteById(5L);
System.out.println("影响记录数:"+rows);
}
@Test
public void deleteByMap(){
Map<String,Object> map = new HashMap<>();
map.put("name","Sandy");
map.put("age",21);
int rows = userMapper.deleteByMap(map);
System.out.println("删除记录数为:"+rows);
}
// DELETE FROM user WHERE id IN ( ? , ? , ? )
@Test
public void deleteBatchIds(){
int rows = userMapper.deleteBatchIds(Arrays.asList(1320737652386902017L,1318216907102400513L,1318215819104182273L));
System.out.println("删除记录数为:"+rows);
}
// DELETE FROM user WHERE (id = ? OR age > ?)
@Test
public void deleteByWrapper(){
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(User::getId,3L).or().gt(User::getAge,40);
int rows = userMapper.delete(lambdaQueryWrapper);
System.out.println("删除条数为:"+rows);
}