一, 简介
在 MybatisPlus 中, BaseMapper 中定义了一些常用的 CRUD 方法, 当我们自定义的 Mapper 接口继承 BaseMapper 后即可拥有了这些方法.
二, BaseMapper 中的 CRUD 方法
通用 CRUD 封装接口, 为 Mybatis-Plus 启动时自动解析实体表关系映射转换为 Mybatis 内部对象注入容器
泛型 T 为任意实体对象
参数 Serializable 为任意类型主键 Mybatis-Plus 不推荐使用复合主键约定每一张表都有自己的唯一 id 主键
对象 Wrapper 为 条件构造器 https://mp.baomidou.com/guide/wrapper.html
- /*
- * Copyright (c) 2011-2020, hubin ([email protected]).
- * <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 官网:
来源: http://www.bubuko.com/infodetail-3209260.html