MybatisPlus的BaseMapper和Wrapper使用

Stella981
• 阅读 1060
一、简介  在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).
    • 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
    • http://www.apache.org/licenses/LICENSE-2.0
    • 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;

    /**

    • Mapper 继承该接口后,无需编写 mapper.xml 文件,即可获得CRUD功能
    • 这个 Mapper 支持 id 泛型
    • @author hubin
    • @since 2016-01-23

    */ public interface BaseMapper {

    /**
     * <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的BaseMapper和Wrapper使用

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

点赞
收藏
评论区
推荐文章
Wesley13 Wesley13
2年前
java通过反射拿到mybatis中的sql语句并操作
privatestaticfinalintMaxBatchLength100;publicvoidupdateBatch(List<Tlist,BaseMapper<Tmapper){if(!Proxy.isProxyClass(mapper.get
Wesley13 Wesley13
2年前
java判断通常的逻辑
packagecom.stylefeng.guns.core.common.constant.factory;importcom.baomidou.mybatisplus.mapper.EntityWrapper;importcom.baomidou.mybatisplus.mapper.Wrapper;importcom.style
Stella981 Stella981
2年前
MyBatis 面试题(附答案解析)
目录MyBatis的实现逻辑MyBatis的缓存实现逻辑{}和${}的区别是什么?MyBatis中自定义标签的执行原理简述Mapper接口的工作原理在Spring中Mapper接口是如何被注入的?在Mapper接口中是否可以有重载方法?当实体类中的属性名和表中的字
Stella981 Stella981
2年前
Field baseMapper in com.baomidou.mybatisplus.extension.service.impl.ServiceImpl required a single...
在学习使用mybatisplus时,遇到一个奇怪的异常如代码一:代码一:ErrorstartingApplicationContext.Todisplaytheconditionsreportrerunyourapplicationwith'debug'enabled.2019071709
Stella981 Stella981
2年前
MybatisPlus知识详解以及用十数个例子完成MybatisPlus的入门到进阶
1\.MybatisPlus1.1业务需求Mybatis缺点:1.Mybatis操作数据库的过程中,需要编辑大量的sql语句.无论该sql是否复杂或者简单.2.Mybatis操作时在xml配置文件中需要额外记忆单独的标签.需求:能否实现单表操作的CRUD的全自动的实现.能否实现
Stella981 Stella981
2年前
Spring Boot + Mybatis
前言:MybatisPlus(简称MP)是一个Mybatis的增强工具,在Mybatis的基础上只做增强不做改变,为简化开发、提高效率而生。简单来说,MybatisPlus是Mybatis的增强工具包,其简化了CRUD操作,提供了代码生成器,强大的条件构造器。同时内置了多个实用插件:标配的分页插件、性能
Stella981 Stella981
2年前
Beetlsql扩展之自定义Mapper
BeetlSql有一个BaseMapper,提供了很多内置的Dao操作,如增删改查等10几个方法,用户只需要些一个类继承此接口便能很快的完成一个Dao,比如publicinterfaceUserDaoextendsBaseMapper<User{}UserDao没有包含任何方法,但集成了BaseMapper,因
Wesley13 Wesley13
2年前
@TableLogic表逻辑处理注解(逻辑删除)
在字段上加上这个注解再执行BaseMapper的删除方法时,删除方法会变成修改例:实体类:@TableLogicprivateIntegerdel;service层:调用BaseMapper的deleteById(id);执行是效果:加@TableLogic的情况下走Update
Stella981 Stella981
2年前
Mybatis Plus各种查询方法
packagecom.xiao.permission_system;importcom.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;importcom.baomidou.mybatisplus.core.conditions.query
京东云开发者 京东云开发者
5个月前
手把手教你如何扩展(破解)mybatisplus的sql生成 | 京东云技术团队
mybatisplus的常用CRUD方法众所周知,mybatisplus提供了强大的代码生成能力,他默认生成的常用的CRUD方法(例如插入、更新、删除、查询等)的定义,能够帮助我们节省很多体力劳动。他的BaseMapper中定义了这些常用的CRUD方法,我