Basic usage of MyBatis - CRUD methods provided in BaseMapper

The MyBatis Plus framework provides the BaseMapper interface, which encapsulates common CRUD (addition, deletion, modification, query) operation methods. The following are some methods provided by BaseMapper and their function descriptions:

  1. insert(T entity): Insert a piece of data

    T entity = new T(); // 创建实体对象
    baseMapper.insert(entity); // 插入数据
    
  2. insertBatch(List entityList): Insert data in batches

    List<T> entityList = new ArrayList<>();
    // 添加多条实体对象到entityList中
    baseMapper.insertBatch(entityList); // 批量插入数据
    
  3. insertOrUpdate(T entity): insert or update data

    T entity = new T(); // 创建实体对象
    baseMapper.insertOrUpdate(entity); // 插入或更新数据
    
  4. deleteById(Serializable id): Delete data according to the primary key ID

    Serializable id = 1L; // 要删除的实体的主键ID
    baseMapper.deleteById(id); // 删除数据
    
  5. deleteByMap(Map<String, Object> columnMap): Delete data according to conditions

    Map<String, Object> columnMap = new HashMap<>();
    columnMap.put("name", "Tom"); // 设置删除条件,这里以name字段等于"Tom"为例
    baseMapper.deleteByMap(columnMap); // 删除满足条件的数据
    
  6. delete(Wrapper wrapper): Delete data according to conditions

    // 创建Wrapper对象,设置删除条件
    LambdaQueryWrapper<T> wrapper = new LambdaQueryWrapper<>();
    wrapper.eq(T::getName, "Tom"); // 设置删除条件,这里以name字段等于"Tom"为例
    baseMapper.delete(wrapper); // 删除满足条件的数据
    
  7. deleteBatchIds(Collection<? extends Serializable> idList): Delete data in batches according to the primary key ID

    Collection<? extends Serializable> idList = Arrays.asList(1L, 2L, 3L); // 要删除的实体的主键ID列表
    baseMapper.deleteBatchIds(idList); // 批量删除数据
    
  8. updateById(T entity): Update data according to the primary key ID

    T entity = new T(); // 创建实体对象
    entity.setId(1L); // 设置要更新的实体的主键ID
    baseMapper.updateById(entity); // 更新数据
    
  9. update(T entity, Wrapper updateWrapper): update data according to conditions

    T entity = new T(); // 创建实体对象
    // 创建Wrapper对象,设置更新条件
    LambdaUpdateWrapper<T> updateWrapper = new LambdaUpdateWrapper<>();
    updateWrapper.eq(T::getName, "Tom"); // 设置更新条件,这里以name字段等于"Tom"为例
    baseMapper.update(entity, updateWrapper); // 更新满足条件的数据
    
  10. selectById(Serializable id): Query data according to the primary key ID

    Serializable id = 1L; // 要查询的实体的主键ID
    T entity = baseMapper.selectById(id); // 查询数据
    
  11. selectBatchIds(Collection<? extends Serializable> idList): Query data in batches according to the primary key ID

    Collection<? extends Serializable> idList = Arrays.asList(1L, 2L, 3L); // 要查询的实体的主键ID列表
    List<T> entityList = baseMapper.selectBatchIds(idList); // 批量查询数据
    
  12. selectByMap(Map<String, Object> columnMap): query data according to conditions

    Map<String, Object> columnMap = new HashMap<>();
    columnMap.put("name", "Tom"); // 设置查询条件,这里以name字段等于"Tom"为例
    List<T> entityList = baseMapper.selectByMap(columnMap); // 查询满足条件的数据
    
  13. selectOne(Wrapper queryWrapper): Query a single piece of data according to conditions

    // 创建Wrapper对象,设置查询条件
    LambdaQueryWrapper<T> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(T::getName, "Tom"); // 设置查询条件,这里以name字段等于"Tom"为例
    T entity = baseMapper.selectOne(queryWrapper); // 查询满足条件的数据
    
  14. selectCount(Wrapper queryWrapper): Query the total number of data according to the condition

    // 创建Wrapper对象,设置查询条件
    LambdaQueryWrapper<T> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(T::getName, "Tom"); // 设置查询条件,这里以name字段等于"Tom"为例
    int count = baseMapper.selectCount(queryWrapper); // 查询满足条件的数据总数
    
  15. selectList(Wrapper queryWrapper): query data list according to conditions

    // 创建Wrapper对象,设置查询条件
    LambdaQueryWrapper<T> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(T::getAge, 18); // 设置查询条件,这里以age字段等于18为例
    List<T> entityList = baseMapper.selectList(queryWrapper); // 查询满足条件的数据列表
    
  16. selectMaps(Wrapper queryWrapper): Query the data list according to the conditions and return the Map collection

    // 创建Wrapper对象,设置查询条件
    LambdaQueryWrapper<T> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(T::getAge, 18); // 设置查询条件,这里以age字段等于18为例
    List<Map<String, Object>> mapList = baseMapper.selectMaps(queryWrapper); // 查询满足条件的数据列表,并返回Map集合
    
  17. selectPage(Page page, Wrapper queryWrapper): Query data in pages according to conditions

    // 创建Wrapper对象,设置查询条件
    LambdaQueryWrapper<T> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(T::getAge, 18); // 设置查询条件,这里以age字段等于18为例
    Page<T> page = new Page<>(1, 10); // 创建分页对象,设置当前页和每页显示数量
    IPage<T> pageResult = baseMapper.selectPage(page, queryWrapper); // 分页查询满足条件的数据
    List<T> entityList = pageResult.getRecords(); // 当前页的数据列表
    long total = pageResult.getTotal(); // 总记录数
    

The above are some of the CRUD methods and their function descriptions provided by BaseMapper of the MyBatis Plus framework. These methods can facilitate database operations, and are simple and flexible to use.

Guess you like

Origin blog.csdn.net/qq_41177135/article/details/131745178