MybatisPlus的BaseMapper和Wrapper使用

1、简介
  在MybatisPlus中,BaseMapper中定义了一些经常使用的CRUD方法,当咱们自定义的Mapper接口继承BaseMapper后便可拥有了这些方法。

2、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); }

3、MybatisPlus的QueryWrapper和UpdateWrapper应用html

  Mybatis-plus的wrapper构造条件使用以下图所示:java

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

相关文章
相关标签/搜索