首页 > 其他分享 >Mybatis-Plus 之BaseMapper 方法详解

Mybatis-Plus 之BaseMapper 方法详解

时间:2023-02-08 21:35:44浏览次数:40  
标签:queryWrapper Wrapper return BaseMapper List param Plus Param Mybatis

package com.itheima.dao;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.exceptions.TooManyResultsException;
import org.apache.ibatis.session.RowBounds;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

public class baseMapper1 {
    /**
     * Mapper 继承该接口后,无需编写 mapper.xml 文件,即可获得CRUD功能
     * 这个 Mapper 支持 id 泛型
     */
    public interface BaseMapper<T> {

        /**
         * 插入一条记录
         *
         * @param entity 实体对象: T
         * @return int
         */
        Integer insert(T entity);

        /**
         * 根据 ID 删除
         *
         * @param id 主键ID
         * @return int
         */
        Integer deleteById(Serializable id);

        /**
         * 根据 columnMap 条件,删除记录
         *
         * @param columnMap 表字段 map 对象
         * @return int
         */
        Integer deleteByMap(@Param("cm") Map<String, Object> columnMap);

        /**
         * 根据 entity 条件,删除记录
         *
         * @param querywrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
         * @return int
         */
//        int delete(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
        Integer delete(@Param("ew") Wrapper<T> querywrapper);

        /**
         * 删除(根据ID或实体 批量删除)
         *
         * @param idList 主键ID列表
         *                  * @param idList 主键ID列表或者实体类列表(不能为 null 以及 empty)
         * @return int
         */
//         int deleteBatchIds(@Param(constants.COLLECTION) Collection<?> idList);
//         Integer deleteBatchIds(List<? extends Serializable> idList);
        int deleteBatchIds(@Param("coll") Collection<?> idList);

        /**
         * 根据 ID 修改
         *
         * @param entity 实体对象
         * @return int
         */
        //      int updataById(@Param(Constants.ENTITY) T entity);
        //     Integer updateById(T entity);
        int updateById(@Param("et") T entity);

        /**
         * 根据 whereEntity 条件,更新记录
         *
         * @param entity  实体对象(set 条件值,可以为null)
         *                实体对象
         * @param updateWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
         *                实体对象封装操作类(可以为 null)
         * @return
         */
        // Integer update(@Param("et") T entity, @Param("ew") Wrapper<T> wrapper);
        int update(@Param("et") T entity, @Param("ew") Wrapper<T> updateWrapper);

        /**
         * 根据 ID 查询
         *
         * @param id 主键ID
         * @return T
         */
        T selectById(Serializable id);

        /**
         * 查询(根据ID 批量查询)
         *
         * @param idList 主键ID列表(不能为 null 以及 empty)
         *               主键ID列表
         * @return List<T>
         */
        // List<T> selectBatchIds(List<? extends Serializable> idList);
        List<T> selectBatchIds(@Param("coll") Collection<? extends Serializable> idList);

        /**
         * 查询(根据 columnMap 条件)
         *
         * @param columnMap 表字段 map 对象
         * @return List<T>
         */
        List<T> selectByMap(@Param("cm") Map<String, Object> columnMap);

        /**
         * 根据 entity 条件,查询一条记录,查询一条记录,例如 qw.last(“limit 1”)限制取一条记录,注意:多条数据会报异常。
         *
         * @param entity 实体对象
         * @param queryWrapper 实体对象封装操作类(可以为null)
         * @return T
         */
        // T selectOne(@Param("ew") T entity); //完整如下
        default T selectOne(@Param("ew") Wrapper<T> queryWrapper) {
            List<T> list = this.selectList(queryWrapper);
            if (list.size() == 1) {
                return list.get(0);
            } else if (list.size() > 1) {
                throw new TooManyResultsException("Expected one result (or null) to be returned by selectOne(), but found: " + list.size());
            } else {
                return null;
            }
        }

        /**
         * exists 存在(参数构造器)
         * 根据 Wrapper 条件,判断是否存在记录
         *
         * @param queryWrapper 实体对象封装操作类
         * @return {@link boolean}
         **/
        default boolean exists(Wrapper<T> queryWrapper) {
            Long count = this.selectCount(queryWrapper);
            return null != count && count > 0L;
        }

        /**
         * 根据 Wrapper 条件构造器,查询总记录数
         *
         * @param queryWrapper 实体对象封装操作类(可以为 null)
         *                     实体对象
         * @return int
         */
        // Long selectCount(@Param("Constants.WRAPPER") Wrapper<T> wrapper);
        Long selectCount(@Param("ew") Wrapper<T> queryWrapper);

        /**
         * 根据 Wrapper<T> queryWrapper 条件构造器,查询全部记录
         *
         * @param queryWrapper 实体对象封装操作类(可以为 null)
         * @return List<T>
         */
        List<T> selectList(@Param("ew") Wrapper<T> queryWrapper);

        /**
         * 根据 Wrapper 条件,查询全部记录
         *
         * @param queryWrapper 实体对象封装操作类(可以为 null)
         * @return List<T>
         */
        List<Map<String, Object>> selectMaps(@Param("ew") Wrapper<T> queryWrapper);

        /**
         * 根据 Wrapper<T> queryWrapper 条件构造器,查询全部记录
         *
         * @param queryWrapper 实体对象封装操作类(可以为 null)
         * @return List<Object>
         */
        List<Object> selectObjs(@Param("ew") Wrapper<T> queryWrapper);

        /**
         * 根据 entity 条件,查询全部记录(并翻页)
         *
         * @param page 分页查询条件(可以为 RowBounds.DEFAULT)
         * @param queryWrapper  实体对象封装操作类(可以为 null)
         * @return List<T>
         */
        <P extends IPage<T>> P selectPage(P page, @Param("ew") Wrapper<T> queryWrapper);


        /**
         * 根据 Wrapper 条件,查询全部记录(并翻页)
         *
         * @param page 分页查询条件(可以为 RowBounds.DEFAULT)
         * @param queryWrapper   实体对象封装操作类
         * @return List<Map < String, Object>>
         */
        // List<Map<String, Object>> selectMapsPage(RowBounds rowBounds, @Param("ew") Wrapper<T> wrapper); }
        <P extends IPage<Map<String, Object>>> P selectMapsPage(P page, @Param("ew") Wrapper<T> queryWrapper);
    }
}

 

标签:queryWrapper,Wrapper,return,BaseMapper,List,param,Plus,Param,Mybatis
From: https://www.cnblogs.com/yayuya/p/17103194.html

相关文章

  • mybatis使用resultMap获取不到值的解决方案
    目录mybatisresultMap获取不到值问题描述原因及解决方法Mybatis从数据库中获取值为nullResultMap要解决的问题:属性名和字段名不一致解决方法 mybatisre......
  • element plus + vue3表单第一次数据未清空的bug问题解决
    使用框架:elementPlus+vue3场景描述:场景一:表单的添加和修改功能,公用同一个弹框,点击修改后,点击添加表单显示的是上次修改的数据。场景二:点击修改,数据回显到表单,然后......
  • RPM 安装好19c后SQLPLUS登录报错ORA-12547
    问题描述:RPM安装好19c后SQLPLUS登录报错ORA-12547,如下所示:1、异常重现2、问题排查--查?/bin/oracle权限,确认无问题.--查/home/oracle/.bash_profile文件,发现ORACLE_HOME目......
  • spring的@Param注解和mybatis中的@Param注解的区别
    1、spring中@Param(importorg.springframework.data.repository.query.Param;)intselectRoleCount(@Param("businessId")IntegerbusinessId,@Param("memberId")Lo......
  • Element Plus
    ElementPlus是基于vue3的UI框架官网https://element-plus.gitee.io/zh-CN/安装cnpminstallelement-plus--save查看package.json导入在main.js中加入import......
  • MyBatis中的#和$有什么区别
    什么是MyBatisMyBatis是一款优秀的持久层框架,特别是在国内(国外据说还是Hibernate的天下)非常的流行,我们常说的SSM组合中的M指的就是#mybatis#。MyBatis支持定制化SQL......
  • 解决mybatis resultMap根据type找不到对应的包问题
    目录mybatisresultMap根据type找不到对应的包mybatisresultMap根据type找不到对应的包这里需要配置typeAliasesPackage自动配置别名typeAliasesPackage定义多个时......
  • 关于mybatis resulttype 返回值异常的问题
    目录mybatisresulttype返回值异常例如:resulttype="student"但是当中有些字段为空例如:数据库字段为:s_name实体类字段为namemybatisresultType="map"的常见问题一、......
  • MyBatis-Plus——saveOrUpdate方法如何确定主键
    saveOrUpdate方法:先更新,更新失败返回0;发起查找,查找失败返回0,最后进行插入操作有三种执行情况1.插入的数据不带id插入成功。同时MyBatis-Plus会自动生成一个19位的id,默认主......
  • 整合MyBatis
      创建实现类的目的,sqlsession私有sqlsession通过在配置文件中的构造方法注入到实现类的私有属性中,test方法只做一件事,就是通过getbean生成实现类对象,然后调用方法输......