• CRUD 接口
    • Mapper CRUD 接口
      • insert
      • deleteById
      • deleteByMap
      • delete
      • deleteBatchIds
      • updateById
      • update
      • selectById
      • selectBatchIds
      • selectByMap
      • selectOne
      • selectCount
      • selectList
      • selectMaps
      • selectObjs
      • selectPage
      • selectMapsPage
    • Service CRUD 接口
      • save
      • saveBatch
      • saveBatch
      • saveOrUpdateBatch
      • saveOrUpdateBatch
      • removeById
      • removeByMap
      • remove
      • removeByIds
      • updateById
      • update
      • updateBatchById
      • saveOrUpdate
      • getById
      • listByIds
      • listByMap
      • getOne
      • getMap
      • getObj
      • count
      • list
      • page
      • listMaps
      • listObjs
      • pageMaps
    • mapper 层 选装件
      • AlwaysUpdateSomeColumnById
      • insertBatchSomeColumn
      • deleteByIdWithFill

    CRUD 接口

    Mapper CRUD 接口

    说明:

    • 通用 CRUD 封装BaseMapperCRUD 接口 - 图1接口,为 Mybatis-Plus 启动时自动解析实体表关系映射转换为 Mybatis 内部对象注入容器
    • 泛型 T 为任意实体对象
    • 参数 Serializable 为任意类型主键 Mybatis-Plus 不推荐使用复合主键约定每一张表都有自己的唯一 id 主键
    • 对象 Wrapper 为 条件构造器

    insert

    1. /**
    2. * <p>
    3. * 插入一条记录
    4. * </p>
    5. *
    6. * @param entity 实体对象
    7. * @return 插入成功记录数
    8. */
    9. int insert(T entity);

    deleteById

    1. /**
    2. * <p>
    3. * 根据 ID 删除
    4. * </p>
    5. *
    6. * @param id 主键ID
    7. * @return 删除成功记录数
    8. */
    9. int deleteById(Serializable id);

    deleteByMap

    1. /**
    2. * <p>
    3. * 根据 columnMap 条件,删除记录
    4. * </p>
    5. *
    6. * @param columnMap 表字段 map 对象
    7. * @return 删除成功记录数
    8. */
    9. int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);

    delete

    1. /**
    2. * <p>
    3. * 根据 entity 条件,删除记录
    4. * </p>
    5. *
    6. * @param wrapper 实体对象封装操作类(可以为 null)
    7. * @return 删除成功记录数
    8. */
    9. int delete(@Param(Constants.WRAPPER) Wrapper<T> wrapper);

    deleteBatchIds

    1. /**
    2. * <p>
    3. * 删除(根据ID 批量删除)
    4. * </p>
    5. *
    6. * @param idList 主键ID列表(不能为 null 以及 empty)
    7. * @return 删除成功记录数
    8. */
    9. int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);

    updateById

    1. /**
    2. * <p>
    3. * 根据 ID 修改
    4. * </p>
    5. *
    6. * @param entity 实体对象
    7. * @return 修改成功记录数
    8. */
    9. int updateById(@Param(Constants.ENTITY) T entity);

    update

    1. /**
    2. * <p>
    3. * 根据 whereEntity 条件,更新记录
    4. * </p>
    5. *
    6. * @param entity 实体对象 (set 条件值,可为 null)
    7. * @param updateWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
    8. * @return 修改成功记录数
    9. */
    10. int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> updateWrapper);

    selectById

    1. /**
    2. * <p>
    3. * 根据 ID 查询
    4. * </p>
    5. *
    6. * @param id 主键ID
    7. * @return 实体
    8. */
    9. T selectById(Serializable id);

    selectBatchIds

    1. /**
    2. * <p>
    3. * 查询(根据ID 批量查询)
    4. * </p>
    5. *
    6. * @param idList 主键ID列表(不能为 null 以及 empty)
    7. * @return 实体集合
    8. */
    9. List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);

    selectByMap

    1. /**
    2. * <p>
    3. * 查询(根据 columnMap 条件)
    4. * </p>
    5. *
    6. * @param columnMap 表字段 map 对象
    7. * @return 实体集合
    8. */
    9. List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);

    selectOne

    • 如果逻辑非唯一该方法不会自动替您 limit 1 你需要 wrapper.last("limit 1") 设置唯一性。
    1. /**
    2. * <p>
    3. * 根据 entity 条件,查询一条记录
    4. * </p>
    5. *
    6. * @param queryWrapper 实体对象
    7. * @return 实体
    8. */
    9. T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    selectCount

    1. /**
    2. * <p>
    3. * 根据 Wrapper 条件,查询总记录数
    4. * </p>
    5. *
    6. * @param queryWrapper 实体对象
    7. * @return 满足条件记录数
    8. */
    9. Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    selectList

    1. /**
    2. * <p>
    3. * 根据 entity 条件,查询全部记录
    4. * </p>
    5. *
    6. * @param queryWrapper 实体对象封装操作类(可以为 null)
    7. * @return 实体集合
    8. */
    9. List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    selectMaps

    1. /**
    2. * <p>
    3. * 根据 Wrapper 条件,查询全部记录
    4. * </p>
    5. *
    6. * @param queryWrapper 实体对象封装操作类(可以为 null)
    7. * @return 字段映射对象 Map 集合
    8. */
    9. List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    selectObjs

    1. /**
    2. * <p>
    3. * 根据 Wrapper 条件,查询全部记录
    4. * 注意: 只返回第一个字段的值
    5. * </p>
    6. *
    7. * @param queryWrapper 实体对象封装操作类(可以为 null)
    8. * @return 字段映射对象集合
    9. */
    10. List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    selectPage

    1. /**
    2. * <p>
    3. * 根据 entity 条件,查询全部记录(并翻页)
    4. * </p>
    5. *
    6. * @param page 分页查询条件(可以为 RowBounds.DEFAULT)
    7. * @param queryWrapper 实体对象封装操作类(可以为 null)
    8. * @return 实体分页对象
    9. */
    10. IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    selectMapsPage

    1. /**
    2. * <p>
    3. * 根据 Wrapper 条件,查询全部记录(并翻页)
    4. * </p>
    5. *
    6. * @param page 分页查询条件
    7. * @param queryWrapper 实体对象封装操作类
    8. * @return 字段映射对象 Map 分页对象
    9. */
    10. IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

    Service CRUD 接口

    说明:

    • 通用 Service CRUD 封装IServiceCRUD 接口 - 图2接口,进一步封装 CRUD 采用 get 查询单行 remove 删除 list 查询集合 page 分页 前缀命名方式区分 Mapper 层避免混淆,
    • 泛型 T 为任意实体对象
    • 建议如果存在自定义通用 Service 方法的可能,请创建自己的 IBaseService 继承 Mybatis-Plus 提供的基类
    • 对象 Wrapper 为 条件构造器

    save

    1. /**
    2. * <p>
    3. * 插入一条记录(选择字段,策略插入)
    4. * </p>
    5. *
    6. * @param entity 实体对象
    7. */
    8. boolean save(T entity);

    saveBatch

    1. /**
    2. * 插入(批量)
    3. *
    4. * @param entityList 实体对象集合
    5. * @param batchSize 插入批次数量
    6. */
    7. boolean saveBatch(Collection<T> entityList);

    saveBatch

    1. /**
    2. * 插入(批量)
    3. *
    4. * @param entityList 实体对象集合
    5. * @param batchSize 插入批次数量
    6. */
    7. boolean saveBatch(Collection<T> entityList, int batchSize);

    saveOrUpdateBatch

    1. /**
    2. * <p>
    3. * 批量修改插入
    4. * </p>
    5. *
    6. * @param entityList 实体对象集合
    7. */
    8. boolean saveOrUpdateBatch(Collection<T> entityList);

    saveOrUpdateBatch

    1. /**
    2. * <p>
    3. * 批量修改插入
    4. * </p>
    5. *
    6. * @param entityList 实体对象集合
    7. * @param batchSize 每次的数量
    8. */
    9. boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);

    removeById

    1. /**
    2. * <p>
    3. * 根据 ID 删除
    4. * </p>
    5. *
    6. * @param id 主键ID
    7. */
    8. boolean removeById(Serializable id);

    removeByMap

    1. /**
    2. * <p>
    3. * 根据 columnMap 条件,删除记录
    4. * </p>
    5. *
    6. * @param columnMap 表字段 map 对象
    7. */
    8. boolean removeByMap(Map<String, Object> columnMap);

    remove

    1. /**
    2. * <p>
    3. * 根据 entity 条件,删除记录
    4. * </p>
    5. *
    6. * @param queryWrapper 实体包装类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
    7. */
    8. boolean remove(Wrapper<T> queryWrapper);

    removeByIds

    1. /**
    2. * <p>
    3. * 删除(根据ID 批量删除)
    4. * </p>
    5. *
    6. * @param idList 主键ID列表
    7. */
    8. boolean removeByIds(Collection<? extends Serializable> idList);

    updateById

    1. /**
    2. * <p>
    3. * 根据 ID 选择修改
    4. * </p>
    5. *
    6. * @param entity 实体对象
    7. */
    8. boolean updateById(T entity);

    update

    1. /**
    2. * <p>
    3. * 根据 whereEntity 条件,更新记录
    4. * </p>
    5. *
    6. * @param entity 实体对象
    7. * @param updateWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper}
    8. */
    9. boolean update(T entity, Wrapper<T> updateWrapper);

    updateBatchById

    1. /**
    2. * <p>
    3. * 根据ID 批量更新
    4. * </p>
    5. *
    6. * @param entityList 实体对象集合
    7. * @param batchSize 更新批次数量
    8. */
    9. boolean updateBatchById(Collection<T> entityList, int batchSize);

    saveOrUpdate

    1. /**
    2. * <p>
    3. * TableId 注解存在更新记录,否插入一条记录
    4. * </p>
    5. *
    6. * @param entity 实体对象
    7. */
    8. boolean saveOrUpdate(T entity);

    getById

    1. /**
    2. * <p>
    3. * 根据 ID 查询
    4. * </p>
    5. *
    6. * @param id 主键ID
    7. */
    8. T getById(Serializable id);

    listByIds

    1. /**
    2. * <p>
    3. * 查询(根据ID 批量查询)
    4. * </p>
    5. *
    6. * @param idList 主键ID列表
    7. */
    8. Collection<T> listByIds(Collection<? extends Serializable> idList);

    listByMap

    1. /**
    2. * <p>
    3. * 查询(根据 columnMap 条件)
    4. * </p>
    5. *
    6. * @param columnMap 表字段 map 对象
    7. */
    8. Collection<T> listByMap(Map<String, Object> columnMap);

    getOne

    1. /**
    2. * <p>
    3. * 根据 Wrapper,查询一条记录
    4. * </p>
    5. *
    6. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
    7. * @param throwEx 有多个 result 是否抛出异常
    8. */
    9. T getOne(Wrapper<T> queryWrapper, boolean throwEx);

    getMap

    1. /**
    2. * <p>
    3. * 根据 Wrapper,查询一条记录
    4. * </p>
    5. *
    6. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
    7. */
    8. Map<String, Object> getMap(Wrapper<T> queryWrapper);

    getObj

    1. /**
    2. * <p>
    3. * 根据 Wrapper,查询一条记录
    4. * </p>
    5. *
    6. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
    7. */
    8. Object getObj(Wrapper<T> queryWrapper);

    count

    1. /**
    2. * <p>
    3. * 根据 Wrapper 条件,查询总记录数
    4. * </p>
    5. *
    6. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
    7. */
    8. int count(Wrapper<T> queryWrapper);

    list

    1. /**
    2. * <p>
    3. * 查询列表
    4. * </p>
    5. *
    6. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
    7. */
    8. List<T> list(Wrapper<T> queryWrapper);

    page

    1. /**
    2. * <p>
    3. * 翻页查询
    4. * </p>
    5. *
    6. * @param page 翻页对象
    7. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
    8. */
    9. IPage<T> page(IPage<T> page, Wrapper<T> queryWrapper);

    listMaps

    1. /**
    2. * <p>
    3. * 查询列表
    4. * </p>
    5. *
    6. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
    7. */
    8. List<Map<String, Object>> listMaps(Wrapper<T> queryWrapper);

    listObjs

    1. /**
    2. * <p>
    3. * 根据 Wrapper 条件,查询全部记录
    4. * </p>
    5. *
    6. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
    7. */
    8. List<Object> listObjs(Wrapper<T> queryWrapper);

    pageMaps

    1. /**
    2. * <p>
    3. * 翻页查询
    4. * </p>
    5. *
    6. * @param page 翻页对象
    7. * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
    8. */
    9. IPage<Map<String, Object>> pageMaps(IPage<T> page, Wrapper<T> queryWrapper);

    mapper 层 选装件

    说明:

    选装件位于 com.baomidou.mybatisplus.extension.injector.methods.additional 包下需要配合Sql 注入器使用,案例CRUD 接口 - 图3使用详细见源码注释CRUD 接口 - 图4

    AlwaysUpdateSomeColumnById

    1. int alwaysUpdateSomeColumnById(T entity);

    insertBatchSomeColumn

    1. int insertBatchSomeColumn(List<T> entityList);

    deleteByIdWithFill

    1. int deleteByIdWithFill(T entity);