Skip to content

MybatisPlus在BaseMapper中提供了很多的方法,足以支撑对数据库单表的基本增删改查,我们来看下面几个:

  • int insert(T entity) :向数据表插入一条记录
  • T selectById(Serializable id):根据主键查询一条记录,并封装到一个指定的实体类对象中
  • List<T> selectByIds(Collection idList):根据id集合查询多条记录,并封装到一个指定的实体类对象集合中
  • int updateById(T entity):根据id更新一条记录
  • int deleteById(Serializable id):根据id删除一条记录
  • int deleteByIds(Collection idList):根据id集合删除记录

具体的演示示例代码如下:

创建实体类 ​

java
package com.itheima.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("user")
public class User {
    @TableId(type = IdType.AUTO) // 设置属性为ID自动增长
    private Integer id;
    private String username;
    private String password;
    private String name;
    private Integer age;
}

创建持久层接口 ​

java
package com.itheima.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itheima.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface UserMapper extends BaseMapper<User> {}

基本的增删改查操作 ​

insert ​

int insert(T entity) :向数据表插入一条记录,从Mybatisplus-v3.5.7开始。

特别说明

除了第一个例子的代码有完整的测试类,剩下的演示代码为了避免冗余,只写测试方法!!!

java
package com.itheima.mapper;

import com.itheima.entity.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class UserMapperTest {
    @Autowired
    private UserMapper userMapper;

    @Test
    public void insertDemo() {
        User user = new User(null, "zhangsan", "123456", "张三", 18);
        int insertRow = userMapper.insert(user);
        System.out.println("当前操作影响的行数是:" + insertRow);
    }
}

selectById ​

T selectById(Serializable id):根据主键查询一条记录,并封装到一个指定的实体类对象中

java
@Test
public void selectByIdDemo() {
    User user = userMapper.selectById(1);
    System.out.println(user);
}

selectByIds ​

List<T> selectByIds(Collection idList):根据id集合查询多条记录,并封装到一个指定的实体类对象集合中

Java
@Test
public void selectByIdsDemo() {
    List<User> users = userMapper.selectByIds(List.of(28, 29, 30));
    System.out.println(users);
}

updateById ​

int updateById(T entity):根据id更新一条记录

java
@Test
public void updateByIdDemo() {
    // 先查找到一条数据
    User user = userMapper.selectById(28);
    // 判断查找到的数据是否为null
    if (user != null) {
        // 更新数据
        user.setAge(80);
        user.setName("我是修改之后的数据");
        int rowLength = userMapper.updateById(user);
        System.out.println("修改受到影响的行数是:" + rowLength);
    }
}

deleteById ​

int deleteById(Serializable id):根据id删除一条记录

Java
@Test
public void deleteByIdDemo() {
    int deleteRowLength = userMapper.deleteById(28);
    System.out.println("单个删除操作影响的代码行数:" + deleteRowLength);

}

deleteByIds ​

int deleteByIds(Collection idList):根据id集合删除记录

java
@Test
public void deleteByIdsDemo() {
    int batchDeleteRows = userMapper.deleteByIds(List.of(29, 30, 31));
    System.out.println("批量删除操作影响的行数:" + batchDeleteRows);
}