1.根据id删除
package com.mp.delete;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.List;
import java.util.Map;
/**
* @Description 测试,删除方法
* @Author LL
* @Date 2020-03-24 22:53
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class DeleteTest {
@Autowired
private UserMapper userMapper;
/**
* 根据id删除
*/
@Test
public void deleteById(){
int rows = userMapper.deleteById(1241614720021913633L);
System.out.println("删除条数:"+rows);
}
/**
* 根据多个id删除
*/
@Test
public void deleteBatchIds(){
List<Long> ids = Arrays.asList(1241614720021913633L,1241614720021913667L);
int rows = userMapper.deleteBatchIds(ids);
System.out.println("删除条数:"+rows);
}
}
2.其它删除方法
package com.mp.delete;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.List;
import java.util.Map;
/**
* @Description 测试,删除方法
* @Author LL
* @Date 2020-03-24 22:53
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class DeleteTest {
@Autowired
private UserMapper userMapper;
/**
* 根据map中的参数作为条件删除
*/
@Test
public void deleteByMap(){
Map<String, Object> deleteMap = new HashMap<>();
deleteMap.put("name", "张1");
deleteMap.put("age", 33);
int rows = userMapper.deleteByMap(deleteMap);
System.out.println("删除条数:"+rows);
}
}
3.以条件构造器为参数的删除方法
package com.mp.delete;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.List;
import java.util.Map;
/**
* @Description 测试,删除方法
* @Author LL
* @Date 2020-03-24 22:53
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class DeleteTest {
@Autowired
private UserMapper userMapper;
/**
* 条件构造器为参数的删除方法
*/
@Test
public void deleteWrapper(){
LambdaQueryWrapper<User> lambdaQuery = Wrappers.lambdaQuery();
lambdaQuery.eq(User::getAge, 38).or().gt(User::getAge, 45);
int rows = userMapper.delete(lambdaQuery);
System.out.println("影响行数:"+rows);
}
}