【07】SpringBoot3 MybatisPlus 删除(Mapper)

SpringBoot3 MybatisPlus 删除

前言

本篇中使用到的项目工程是在《SpringBoot3 MybatisPlus 加入日志功能》基础上,持续功能开发。

删除 API

delete

官网 API:

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

Demo 示例:

java 复制代码
    @Test
    public void testDelete() {

        // 待删除的数据
        ClientEntity clientEntity = new ClientEntity();
        clientEntity.setId(301L);
        clientEntity.setClientId(301L);
        clientEntity.setRealName("根据ID更新-名称");
        // 用QueryWrapper包装实体,生成删除条件
        // 底层会拼接:WHERE id = 301 AND client_id = 301
        QueryWrapper<ClientEntity> queryWrapper = new QueryWrapper<>(clientEntity);
        clientMapper.delete(queryWrapper);

        // 验证,从数据库是否可以查到数据.
        ClientEntity clientEntity1 = clientMapper.selectById(301L);
        System.out.println("clientEntity1 = " + clientEntity1);
    }

输出日志:

deleteById

官网 API:

java 复制代码
    /**
     * 根据 ID 删除
     *
     * @param useFill 是否填充
     * @param obj      主键ID或实体
     * @since 3.5.7
     */
    default int deleteById(Object obj, boolean useFill) {
        Class<?> entityClass = ReflectionKit.getSuperClassGenericType(getClass(), BaseMapper.class, 0);
        Assert.notNull(entityClass, "entityClass must not be null");
        if (!entityClass.isAssignableFrom(obj.getClass()) && useFill) {
            TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
            Assert.notNull(tableInfo, "Can not get TableInfo for entity " + entityClass);
            String keyProperty = tableInfo.getKeyProperty();
            Assert.notEmpty(keyProperty, "The current table has no primary key.");
            if (tableInfo.isWithLogicDelete() && tableInfo.isWithUpdateFill()) {
                T instance = tableInfo.newInstance();
                tableInfo.setPropertyValue(instance, keyProperty, OgnlOps.convertValue(obj, tableInfo.getKeyType()));
                return this.deleteById(instance);
            }
        }
        MapperProxyMetadata mapperProxyMetadata = MybatisUtils.getMapperProxy(this);
        SqlSession sqlSession = mapperProxyMetadata.getSqlSession();
        return sqlSession.delete(mapperProxyMetadata.getMapperInterface().getName() + Constants.DOT + SqlMethod.DELETE_BY_ID.getMethod(), obj);
    }

Demo 示例:

java 复制代码
    @Test
    public void testDeleteById() {
        int result = clientMapper.deleteById(302L);
        System.out.println("result = " + result);
    }

输出日志:

java 复制代码
2026-01-29T19:35:52.668+08:00  INFO 9652 --- [springboot3-mybatisplus] [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
JDBC Connection [HikariProxyConnection@1014250816 wrapping com.mysql.cj.jdbc.ConnectionImpl@28b5d5dc] will not be managed by Spring
==>  Preparing: DELETE FROM client WHERE id=?
==> Parameters: 302(Long)
<==    Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6d3ad37a]
result = 1

deleteById 实体类

官网 API:

java 复制代码
    /**
     * 根据实体(ID)删除
     *
     * @param entity 实体对象
     * @since 3.4.4
     */
    int deleteById(T entity);

Demo 示例:

java 复制代码
    @Test
    public void testDeleteById_02() {
        ClientEntity clientEntity = new ClientEntity();
        clientEntity.setId(302L);
        clientEntity.setClientId(302L);
        int result = clientMapper.deleteById(clientEntity);
        System.out.println("result = " + result);
    }

输出日志:

java 复制代码
2026-01-29T19:39:05.546+08:00  INFO 9512 --- [springboot3-mybatisplus] [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
JDBC Connection [HikariProxyConnection@499950630 wrapping com.mysql.cj.jdbc.ConnectionImpl@3fe98084] will not be managed by Spring
==>  Preparing: DELETE FROM client WHERE id=?
==> Parameters: 302(Long)
<==    Updates: 0
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@308a9264]
result = 0
2026-01-29T19:39:05.592+08:00  INFO 9512 --- [springboot3-mybatisplus] [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...

deleteByIds 批量

官网API

java 复制代码
    /**
     * 删除(根据ID或实体 批量删除)
     *
     * @param idList 主键ID列表或实体列表(不能为 null 以及 empty)
     * @since 3.5.7
     */
    default int deleteByIds(@Param(Constants.COLL) Collection<?> idList) {
        return deleteByIds(idList, true);
    }

Demo 示例:

java 复制代码
    @Test
    public void testDeleteByIds() {
        List<Long> ids = Arrays.asList(1L, 2L, 3L);
        int result = clientMapper.deleteBatchIds(ids);
        System.out.println("result = " + result);
    }

输出日志:

java 复制代码
2026-01-29T19:43:30.082+08:00  INFO 15452 --- [springboot3-mybatisplus] [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
JDBC Connection [HikariProxyConnection@1683834425 wrapping com.mysql.cj.jdbc.ConnectionImpl@152891f8] will not be managed by Spring
==>  Preparing: DELETE FROM client WHERE id IN ( ? , ? , ? )
==> Parameters: 1(Long), 2(Long), 3(Long)
<==    Updates: 3
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@16e07bae]
result = 3

deleteByMap

官网 API:

java 复制代码
    /**
     * 根据 columnMap 条件,删除记录
     *
     * @param columnMap 表字段 map 对象
     */
    default int deleteByMap(Map<String, Object> columnMap) {
        return this.delete(Wrappers.<T>query().allEq(columnMap));
    }

Demo 示例:

java 复制代码
2026-01-29T19:47:05.846+08:00  INFO 4684 --- [springboot3-mybatisplus] [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
JDBC Connection [HikariProxyConnection@492135521 wrapping com.mysql.cj.jdbc.ConnectionImpl@46d148bd] will not be managed by Spring
==>  Preparing: SELECT id,client_id,user_no,user_password,nick_name,real_name,created_time,upated_time FROM client WHERE id=?
==> Parameters: 50(Integer)
<==    Columns: id, client_id, user_no, user_password, nick_name, real_name, created_time, upated_time
<==        Row: 50, 50, MDDWAW2AuQ, GXzMEWqXxFIxNsfIOkyEoEdhtJhuPcGn, 太阳花,我要渲染你的微笑, 乐之, 2026-01-26 07:22:25, 2026-01-26 07:22:25
<==      Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3c9f4376]
删除之前 ===>>> clientEntity = ClientEntity(id=50, clientId=50, userNo=MDDWAW2AuQ, userPassword=GXzMEWqXxFIxNsfIOkyEoEdhtJhuPcGn, nickName=太阳花,我要渲染你的微笑, realName=乐之, createdTime=Mon Jan 26 07:22:25 CST 2026, upatedTime=Mon Jan 26 07:22:25 CST 2026)
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@11309dd4] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@2070199213 wrapping com.mysql.cj.jdbc.ConnectionImpl@46d148bd] will not be managed by Spring
==>  Preparing: DELETE FROM client WHERE (id = ?)
==> Parameters: 50(String)
<==    Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@11309dd4]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2e0163cb] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@1797821749 wrapping com.mysql.cj.jdbc.ConnectionImpl@46d148bd] will not be managed by Spring
==>  Preparing: SELECT id,client_id,user_no,user_password,nick_name,real_name,created_time,upated_time FROM client WHERE id=?
==> Parameters: 50(Integer)
<==      Total: 0
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2e0163cb]
删除之后 ===>>> clientEntity = null
2026-01-29T19:47:05.979+08:00  INFO 4684 --- [springboot3-mybatisplus] [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...

若有转载,请标明出处:https://blog.csdn.net/CharlesYuangc/article/details/157515563

相关推荐
码云数智-园园44 分钟前
微服务架构下的分布式事务:在一致性与可用性之间寻找平衡
开发语言
C++ 老炮儿的技术栈1 小时前
volatile使用场景
linux·服务器·c语言·开发语言·c++
hz_zhangrl1 小时前
CCF-GESP 等级考试 2026年3月认证C++一级真题解析
开发语言·c++·gesp·gesp2026年3月·gespc++一级
大阿明1 小时前
Spring Boot(快速上手)
java·spring boot·后端
Liu628881 小时前
C++中的工厂模式高级应用
开发语言·c++·算法
哆啦A梦15881 小时前
Springboot整合MyBatis实现数据库操作
数据库·spring boot·mybatis
bearpping1 小时前
Java进阶,时间与日期,包装类,正则表达式
java
IT猿手1 小时前
基于控制障碍函数的多无人机编队动态避障控制方法研究,MATLAB代码
开发语言·matlab·无人机·openclaw·多无人机动态避障路径规划·无人机编队
邵奈一2 小时前
清明纪念·时光信笺——项目运行指南
java·实战·项目
AI科技星2 小时前
全尺度角速度统一:基于 v ≡ c 的纯推导与验证
c语言·开发语言·人工智能·opencv·算法·机器学习·数据挖掘