日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

MybatisPlus的BaseMapper和Wrapper使用

作者:浪子塵晨 更新時(shí)間: 2023-12-25 編程語(yǔ)言

一、簡(jiǎn)介

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

二、BaseMapper中的CRUD方法

  • 通用 CRUD 封裝BaseMapper接口,為 Mybatis-Plus 啟動(dòng)時(shí)自動(dòng)解析實(shí)體表關(guān)系映射轉(zhuǎn)換為 Mybatis 內(nèi)部對(duì)象注入容器

  • 泛型 T 為任意實(shí)體對(duì)象

  • 參數(shù) Serializable 為任意類型主鍵 Mybatis-Plus 不推薦使用復(fù)合主鍵約定每一張表都有自己的唯一 id 主鍵

  • 對(duì)象 Wrapper 為 條件構(gòu)造器

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

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

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

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

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

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

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

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

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

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

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

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

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

    /**
     * <p>
     * 根據(jù) entity 條件,查詢?nèi)坑涗?     * </p>
     *
     * @param queryWrapper 實(shí)體對(duì)象封裝操作類(可以為 null)
     */
    List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /**
     * <p>
     * 根據(jù) Wrapper 條件,查詢?nèi)坑涗?     * </p>
     *
     * @param queryWrapper 實(shí)體對(duì)象封裝操作類(可以為 null)
     */
    List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /**
     * <p>
     * 根據(jù) Wrapper 條件,查詢?nèi)坑涗?     * 注意: 只返回第一個(gè)字段的值
     * </p>
     *
     * @param queryWrapper 實(shí)體對(duì)象封裝操作類(可以為 null)
     */
    List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /**
     * <p>
     * 根據(jù) entity 條件,查詢?nèi)坑涗洠ú⒎?yè))
     * </p>
     *
     * @param page         分頁(yè)查詢條件(可以為 RowBounds.DEFAULT)
     * @param queryWrapper 實(shí)體對(duì)象封裝操作類(可以為 null)
     */
    IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    /**
     * <p>
     * 根據(jù) Wrapper 條件,查詢?nèi)坑涗洠ú⒎?yè))
     * </p>
     *
     * @param page         分頁(yè)查詢條件
     * @param queryWrapper 實(shí)體對(duì)象封裝操作類
     */
    IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
}

三、MybatisPlus的QueryWrapper和UpdateWrapper應(yīng)用

  Mybatis-plus的wrapper構(gòu)造條件使用如下圖所示:

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

原文鏈接:https://blog.csdn.net/fygkchina/article/details/128624602

  • 上一篇:沒(méi)有了
  • 下一篇:沒(méi)有了
欄目分類
最近更新