Mybatis-plus练习增加、删除、修改代码
package com.yase;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.mysql.cj.util.StringUtils;
import com.yase.entity.Teacher;
import com.yase.mapper.TeacherMapper;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SpringBootTest
class SpringbootMybatisplusApplicationTests {
@Resource
private TeacherMapper mapper;
//查询所有数据
@Test
void selectList() {
List<Teacher> list = mapper.selectList(null);
list.forEach(System.out::println);
}
//添加数据
@Test
void addMember(){
Teacher teacher = new Teacher();
teacher.setTeacherName("李四").setAge(20).setSex("男").setAddr("江南好").setDeleted(0).setId(200);
mapper.insert(teacher);
}
//通过id删除数据
@Test
void deleteById(){
//返回删除的数据条数
int i = mapper.deleteById(202);
System.out.println("影响数据"+i+"条");
Teacher teacher = new Teacher();
teacher.setId(2);
mapper.deleteById(teacher);
}
//通过字段:值,and 字段:值,删除数据
@Test
void deleteByMap(){
Map<String,Object> map = new HashMap<>();
map.put("teacher_name","教师李四");
map.put("gender","男");
int i = mapper.deleteByMap(map);
System.out.println("影响数据"+i+"条");
}
//通过集合批量删除数据
@Test
void deleteBatchIds(){
//通过Arrays.asList一次性增加多条数据,不存在则不处理
List<Integer> list = Arrays.asList(12,24);
int i = mapper.deleteBatchIds(list);
System.out.println("影响数据"+i+"条");
}
@Test
void delete(){
QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
wrapper.eq("gender","男");
mapper.delete(wrapper);
}
//删除性别为男的姓名包含张的数据
@Test
void delecte2(){
QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
wrapper.eq("gender","男")
.like("teacher_name","张");
mapper.delete(wrapper);
}
//删除李四女的这条数据
@Test
void delete3(){
Teacher teacher = new Teacher();
teacher.setTeacherName("李四").setSex("女");
QueryWrapper<Teacher> wrapper = new QueryWrapper<>(teacher);
mapper.delete(wrapper);
}
//使用lambda方式,获取指定的字段名,防止写错
@Test
void delete4(){
LambdaQueryWrapper<Teacher> wrapper = new LambdaQueryWrapper<>();
wrapper.like(Teacher::getTeacherName,"张")
.eq(Teacher::getSex,"男");
mapper.delete(wrapper);
}
//
@Test
void delete05(){
Teacher teacher = new Teacher();
teacher.setTeacherName("张");
QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
wrapper.eq(!StringUtils.isNullOrEmpty(teacher.getSex()),"sex",teacher.getSex())
.like(!StringUtils.isNullOrEmpty(teacher.getTeacherName()),"teacher_name",teacher.getTeacherName());
LambdaQueryWrapper<Teacher> wrapper1 = new LambdaQueryWrapper<>();
wrapper1.like(!StringUtils.isNullOrEmpty(teacher.getSex()),Teacher::getSex,teacher.getSex())
.like(!StringUtils.isNullOrEmpty(teacher.getTeacherName()),Teacher::getTeacherName,teacher.getTeacherName());
mapper.delete(wrapper);
}
@Test
void updateById(){
Teacher teacher = new Teacher();
teacher.setSex("女").setAge(30).setId(1);
mapper.updateById(teacher);
}
@Test
void update(){
Teacher teacher = new Teacher();
teacher.setSex("男").setAge(25);
UpdateWrapper wrapper = new UpdateWrapper();
wrapper.likeRight("teacher_name","张");
mapper.update(teacher,wrapper);
UpdateWrapper<Teacher> wrapper1 = new UpdateWrapper<>();
wrapper1.set("gender","女").set("age",30)
.likeRight("teacher_name","张");
mapper.update(null,wrapper1);
}
@Test
void update2(){
LambdaUpdateWrapper<Teacher> wrapper = new LambdaUpdateWrapper<>();
wrapper.set(Teacher::getSex,"女").set(Teacher::getAge,30)
.likeRight(Teacher::getTeacherName,"张");
mapper.update(null,wrapper);
}
}