MybatisPlus的BaseMapper和Wrapper使用


一、簡介
  在MybatisPlus中,BaseMapper中定義了一些常用的CRUD方法,當我們自定義的Mapper接口繼承BaseMapper后即可擁有了這些方法。

二、BaseMapper中的CRUD方法
  • 通用 CRUD 封裝BaseMapper接口,為 Mybatis-Plus 啟動時自動解析實體表關系映射轉換為 Mybatis 內部對象注入容器
  • 泛型 T 為任意實體對象
  • 參數 Serializable 為任意類型主鍵 Mybatis-Plus 不推薦使用復合主鍵約定每一張表都有自己的唯一 id 主鍵
  • 對象 Wrapper 為 條件構造器
/*
 * Copyright (c) 2011-2020, hubin (jobob@qq.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.baomidou.mybatisplus.core.mapper;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;

/**
 * <p>
 * Mapper 繼承該接口后,無需編寫 mapper.xml 文件,即可獲得CRUD功能
 * </p>
 * <p>
 * 這個 Mapper 支持 id 泛型
 * </p>
 *
 * @author hubin
 * @since 2016-01-23
 */
public interface BaseMapper<T> {

    /**
     * <p>
     * 插入一條記錄
     * </p>
     *
     * @param entity 實體對象
     */
    int insert(T entity);

    /**
     * <p>
     * 根據 ID 刪除
     * </p>
     *
     * @param id 主鍵ID
     */
    int deleteById(Serializable id);

    /**
     * <p>
     * 根據 columnMap 條件,刪除記錄
     * </p>
     *
     * @param columnMap 表字段 map 對象
     */
    int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);

    /**
     * <p>
     * 根據 entity 條件,刪除記錄
     * </p>
     *
     * @param queryWrapper 實體對象封裝操作類(可以為 null)
     */
    int delete(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /**
     * <p>
     * 刪除(根據ID 批量刪除)
     * </p>
     *
     * @param idList 主鍵ID列表(不能為 null 以及 empty)
     */
    int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);

    /**
     * <p>
     * 根據 ID 修改
     * </p>
     *
     * @param entity 實體對象
     */
    int updateById(@Param(Constants.ENTITY) T entity);

    /**
     * <p>
     * 根據 whereEntity 條件,更新記錄
     * </p>
     *
     * @param entity        實體對象 (set 條件值,不能為 null)
     * @param updateWrapper 實體對象封裝操作類(可以為 null,里面的 entity 用於生成 where 語句)
     */
    int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> updateWrapper);

    /**
     * <p>
     * 根據 ID 查詢
     * </p>
     *
     * @param id 主鍵ID
     */
    T selectById(Serializable id);

    /**
     * <p>
     * 查詢(根據ID 批量查詢)
     * </p>
     *
     * @param idList 主鍵ID列表(不能為 null 以及 empty)
     */
    List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);

    /**
     * <p>
     * 查詢(根據 columnMap 條件)
     * </p>
     *
     * @param columnMap 表字段 map 對象
     */
    List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);

    /**
     * <p>
     * 根據 entity 條件,查詢一條記錄
     * </p>
     *
     * @param queryWrapper 實體對象
     */
    T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /**
     * <p>
     * 根據 Wrapper 條件,查詢總記錄數
     * </p>
     *
     * @param queryWrapper 實體對象
     */
    Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /**
     * <p>
     * 根據 entity 條件,查詢全部記錄
     * </p>
     *
     * @param queryWrapper 實體對象封裝操作類(可以為 null)
     */
    List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /**
     * <p>
     * 根據 Wrapper 條件,查詢全部記錄
     * </p>
     *
     * @param queryWrapper 實體對象封裝操作類(可以為 null)
     */
    List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /**
     * <p>
     * 根據 Wrapper 條件,查詢全部記錄
     * 注意: 只返回第一個字段的值
     * </p>
     *
     * @param queryWrapper 實體對象封裝操作類(可以為 null)
     */
    List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /**
     * <p>
     * 根據 entity 條件,查詢全部記錄(並翻頁)
     * </p>
     *
     * @param page         分頁查詢條件(可以為 RowBounds.DEFAULT)
     * @param queryWrapper 實體對象封裝操作類(可以為 null)
     */
    IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /**
     * <p>
     * 根據 Wrapper 條件,查詢全部記錄(並翻頁)
     * </p>
     *
     * @param page         分頁查詢條件
     * @param queryWrapper 實體對象封裝操作類
     */
    IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
}

三、MybatisPlus的QueryWrapper和UpdateWrapper應用

  Mybatis-plus的wrapper構造條件使用如下圖所示:

  附上MybatisPlus官網: https://mp.baomidou.com/guide/crud-interface.html#mapper-crud-%E6%8E%A5%E5%8F%A3


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM