在原来使用MyBatis的时候,每次与查询数据都需要在对应的xml中编写sql语句。

在xml中编写sql语句,比较繁琐复杂,而MyBatis-Plus可以直接使用java代码的形式编写sql语句。

MyBatis-Plus是在MyBatis的基础上,做了更多的功能和增强,不影响MyBatis原有的功能。

1、准备工作

1.1 java实体类映射

  1. 实体类命名要与表名一致,实体类的字段名和表的字段名一致,并采用驼峰对应。
  2. 实体类的所有字段都必须要有getter和setter方法,这是MyBatis-Plus的必须要求。
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.IdType;
import java.time.LocalDate;
import com.baomidou.mybatisplus.annotation.TableId;
import java.time.LocalDateTime;
import java.io.Serializable;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

@Data  //lombok,提供getter、setter方法
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("students")
public class Students implements Serializable {//学生信息表

    private static final long serialVersionUID = 1L;

    //学生ID
    @TableId(value = "student_id", type = IdType.AUTO)
    private Integer studentId;

    //学生姓名
    private String studentName;

    //性别
    private String gender;

    //出生日期
    private LocalDate birthday;

    //所属班级ID
    private Integer classId;

    //联系电话
    private String phone;

    //创建时间
    private LocalDateTime createdAt;

}

1.2 mapper文件

mapper是一个接口,要继承BaseMapper类。

继承BaseMapper类的泛型为该mapper文件对应的java实体类。

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

//学生信息表 Mapper 接口
@Mapper
public interface StudentsMapper extends BaseMapper<Students> {

}

1.3 maven配置

        <dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
			<version>3.5.15</version>
		</dependency>

2、mapper的使用

mapper用于执行所有的查询与修改操作。

但只用mapper,就只能进行简单的查询与修改。

下方示例:

@SpringBootTest
class Day260420Test {

    @Autowired
    public StudentsMapper studentsMapper;

    @Test
    public void test01(){

        studentsMapper.selectById(1);
        Students students = new Students();
        students.setStudentId(1);
        students.setStudentName("啊Q");
        studentsMapper.updateById( students);//只会修改有设置过的字段,如studentName
        studentsMapper.deleteById(2);

    }


}

3、条件构造器

在编写sql语句的条件时,Wrapper类允许我们以链式调用的方式构造查询条件。

借助Wrapper,我们可以构建各种条件。

Wrapper可以分为两类,查询和修改。

查询有QueryWrapper和LambdaQueryWrapper。

修改有UpdateWrapper和LambdaUpdateWrapper。

修改相比查询多了set方法,用于修改数据。

QueryWrapper和UpdateWrapper设置条件时,表示比较的字段是MySQL表的字段名。

而LambdaQueryWrapper和LambdaUpdateWrapper设置条件时,表示比较的字段是传入其字段的lamba表达式。

比较方法意思
allEq传入一个map,在map中设置等于条件
eq等于
ne不等于
gt大于
ge大于等于
lt小于
le小于等于
between在两数之间
notBetween不在两树之间
like模糊匹配

Wrapper只用于生成sql的条件,实际的查询和修改操作由mapper执行。

下方示例:

@SpringBootTest
class Day260420Test {

    @Autowired
    public StudentsMapper studentsMapper;

    @Test
    public void test02(){

        System.out.println("-----------------QueryWrapper-------------");
        QueryWrapper<Students> studentsQueryWrapper = new QueryWrapper<>();
        studentsQueryWrapper.ge("student_id",3);
        List<Students> students01 = studentsMapper.selectList(studentsQueryWrapper);
        for (Object o :students01) {
            System.out.println(o);
        }

        System.out.println("-----------------LambdaQueryWrapper-------------");
        LambdaQueryWrapper<Students> studentsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        studentsLambdaQueryWrapper.ge(Students::getStudentId,4);
        List<Students> students02 = studentsMapper.selectList(studentsLambdaQueryWrapper);
        for (Object o :students02) {
            System.out.println(o);
        }

        System.out.println("-----------------UpdateWrapper-------------");
        UpdateWrapper<Students> studentsUpdateWrapper = new UpdateWrapper<>();
        studentsUpdateWrapper.set("student_name","啊Q");
        studentsUpdateWrapper.eq("student_id",3);
        System.out.println(studentsMapper.selectById(3));
        studentsMapper.update(studentsUpdateWrapper);
        System.out.println("修改后");
        System.out.println(studentsMapper.selectById(3));

        System.out.println("-----------------LambdaUpdateWrapper-------------");
        LambdaUpdateWrapper<Students> studentsLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        studentsLambdaUpdateWrapper.set(Students::getStudentName,"啊Q");
        studentsLambdaUpdateWrapper.eq(Students::getStudentId,4);
        System.out.println(studentsMapper.selectById(4));
        studentsMapper.update(studentsLambdaUpdateWrapper);
        System.out.println("修改后");
        System.out.println(studentsMapper.selectById(4));
        
    }

}

4、分页查询

maven配置:

        <dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-jsqlparser</artifactId>
			<version>3.5.15</version>
		</dependency>

配置类:

@Configuration
public class MybatisPlusConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

}

代码示例:

@SpringBootTest
class Day260420Test {

    @Autowired
    public StudentsMapper studentsMapper;

    @Test
    public void test03(){

        IPage<Students> page = new Page<>(1, 2);//new Page<>(第几页, 每页多少个)
        studentsMapper.selectPage(page, new LambdaQueryWrapper<Students>());

        System.out.println("当前页:"+page.getCurrent());
        System.out.println("每页显示的条数:"+page.getSize());
        System.out.println("总记录数:"+page.getTotal());
        System.out.println("总页数:"+page.getPages());

        List<Students> records = page.getRecords();
        for (Object o :records) {
            System.out.println(o);
        }

    }

}

5、一键配置java实体类和mapper文件

安装图片中所示的MyBatis-Plus插件

然后在Tools工具栏下,点击Config Database,配置好数据库

最后在Tools工具栏下,点击Code Generator

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐