Mybatis Plus使用大全

一、快速开始

前言

Mybatis Plus的使用,对于我们开发效率有了质的提升,对于单表操作是真的方便,如果是多表关联查询,建议还是用传统的Mybatis在xml中写SQL会好一些。
以下是我整理出来的在service层的基本应用场景,从增、删、改、查四个方面整理出用的频率比较高的几种场景。mapper层的操作大同小异,参考官方文档https://baomidou.com/只有一些小调整。

数据处理参考表:
在这里插入图片描述

项目中导入依赖

项目pom文件中加入mybatis-plus的依赖

<!-- mybatis-plus -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.2</version>
        </dependency>

yml配置

yml中引入以下配置

mybatis-plus:
  # xml路径地址
  mapper-locations: classpath:/mapper/*.xml
  global-config:
    db-config:
      # 主键新增类型->自增
      id-type: auto
      # 逻辑删除配置
      logic-delete-value: 1
      logic-not-delete-value: 0

配置实体类、mapper层、service层

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Student {
    
    
    @TableId
    private Long studentId;
    private String studentName;
    private String studentNo;
    private String sex;
    private Integer age;
    private String grade;
}
@Mapper
public interface StudentMapper extends BaseMapper<Student> {
    
    
}
public interface StudentService extends IService<Student> {
    
    
}
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    
    
}

二、使用大全

1.查询

查询所有

	/**
	 * select * from student
	 */
	List<Student> list = studentService.list();

查询数量

	/**
	 * select count(1) from student
	 */
	int count = studentService.count();

根据主键ID查询

	/**
	 * select * from student where student_id = 5
	 */
	Student student = studentService.getById(5);

有条件查询

	/**
	 * select * from student
	 * where sex = 1
	 * and student_no like '%202200%'
	 * and (age = 8 or age = 14)
	 * order by student_id desc
	 */
	QueryWrapper<Student> qw = new QueryWrapper<>();
	qw.eq("sex",1).like("student_no","202200");
	qw.and(obj -> {
    
    
	    obj.eq("age", 8).or().eq("age", 12);
	});
	qw.orderByDesc("student_id");
	List<Student> list1 = studentService.list(qw);

有条件查询(部分字段、in)

	/**
	 * select student_id,student_name from student where age in (8, 9, 10, 12)
	 */
	List<Integer> integers = Arrays.asList(8, 9, 10, 12);
	List<Student> list2 = studentService.list(new QueryWrapper<Student>()
	        .select("student_id,student_name").in("age",integers));

查询一个(条件要限制,查出多个会报错)

	/**
	 * select * from student where student_no = '20220005'
	 */
	Student one = studentService.getOne(new QueryWrapper<Student>().eq("student_no", "20220005"));

聚合查询

扫描二维码关注公众号,回复: 17266167 查看本文章
	/**
	 * select sex,count(1) as count
	 * from student group by sex
	 */
	QueryWrapper<Student> qw1 = new QueryWrapper<Student>().select("sex,count(1) as count").groupBy("sex");
	List<Map<String, Object>> maps1 = studentService.listMaps(qw1);

聚合查询加条件

	/**
	 * select sex,count(1) as count
	 * from student
	 * where age >= 12
	 * group by sex
	 * having count(1) >= 3
	 */
	QueryWrapper<Student> qw2 = new QueryWrapper<Student>().select("sex,count(1) as count").ge("age",12)
	        .groupBy("sex").last("having count(1) >= 3");
	List<Map<String, Object>> maps2 = studentService.listMaps(qw2);

分页查询,必须配置PaginationInterceptor才生效分页效果

	/**
	 * select * from student order by age desc
	 * limit 0,2
	 */
	List<Student> records = studentService.page(new Page<>(1, 2), new QueryWrapper<Student>().orderByDesc("age")).getRecords();

分页查询的配置类不要忘哦!

@Configuration
public class MyBatisConfiguration {
    
    
    @Bean
    public PaginationInterceptor paginationInterceptor(){
    
    
        return new PaginationInterceptor();
    }
}

2.新增

普通新增

	Student student = Student.builder().studentName("sa").studentNo("121212").sex("0").age(15).grade("七").build();
	studentService.save(student);

批量新增

	Student student2 = Student.builder().studentName("dada").studentNo("645343").sex("1").age(14).grade("六").build();
	List<Student> students = Arrays.asList(student2);
	studentService.saveBatch(students);

新增或修改

	studentService.saveOrUpdate(student);

3.修改

修改一,根据对象中包含的值修改部分字段(必须要有主键ID)

	/**
	 * update student set student_name = 'saa' where student_id = 9
	 */
	Student student = Student.builder().studentId(9L).studentName("saa").build();
	studentService.updateById(student);

修改二,严格按照规格修改字段

	/**
	 * update student set student_no = '999999' where student_name = 'saa'
	 */
	UpdateWrapper<Student> uw = new UpdateWrapper<Student>()
	        .eq("student_name","saa").set("student_no","999999");
	studentService.update(uw);

4.删除

根据条件删除

	/**
	 * delete from student where student_name = 'dada'
	 */
	studentService.remove(new QueryWrapper<Student>().eq("student_name","dada"));

根据主键ID删除

	/**
	 * delete from student where student_id = 12
	 */
	studentService.removeById(12);

根据主键ID的集合批量删除

	/**
	 * delete from student where student_id in (9, 13)
	 */
	List<Integer> list = Arrays.asList(9, 13);
	studentService.removeByIds(list);

猜你喜欢

转载自blog.csdn.net/weixin_50989469/article/details/126507674