MyBatisPlus(二)提供的CRUD功能

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: 继承了BaseMapper<T>接口后,便可实现表的基础CRUD功能了,下面我们来看看具体内容。

继承了BaseMapper<T>接口后,便可实现表的基础CRUD功能了,下面我们来看看具体内容。

BaseMapper<T>接口源码:

public interface BaseMapper<T> extends Mapper<T> {
   

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

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

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

    /**
     * 根据 entity 条件,删除记录
     *
     * @param wrapper 实体对象封装操作类(可以为 null)
     */
    int delete(@Param(Constants.WRAPPER) Wrapper<T> wrapper);

    /**
     * 删除(根据ID 批量删除)
     *
     * @param idList 主键ID列表(不能为 null 以及 empty)
     */
    int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);

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

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

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

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

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

    /**
     * 根据 entity 条件,查询一条记录
     *
     * @param queryWrapper 实体对象封装操作类(可以为 null)
     */
    T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

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

    /**
     * 根据 entity 条件,查询全部记录
     *
     * @param queryWrapper 实体对象封装操作类(可以为 null)
     */
    List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

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

    /**
     * 根据 Wrapper 条件,查询全部记录
     * <p>注意: 只返回第一个字段的值</p>
     *
     * @param queryWrapper 实体对象封装操作类(可以为 null)
     */
    List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

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

    /**
     * 根据 Wrapper 条件,查询全部记录(并翻页)
     *
     * @param page         分页查询条件
     * @param queryWrapper 实体对象封装操作类
     */
    <E extends IPage<Map<String, Object>>> E selectMapsPage(E page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
}

基础CRUD

插入一条记录

@Test
public void testInsert() {
   
    User user = new User();
    user.setName("Lili");
    user.setAge(0);
    user.setEmail("lili@163.com");
    userMapper.insert(user);
}

执行日志:

JDBC Connection [HikariProxyConnection@648176342 wrapping com.mysql.cj.jdbc.ConnectionImpl@11bd803] will not be managed by Spring
==>  Preparing: INSERT INTO user ( id, name, age, email ) VALUES ( ?, ?, ?, ? )
==> Parameters: 1307918819829981186(Long), Lili(String), 0(Integer), lili@163.com(String)
<==    Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@30506c0d]

根据 ID 删除

@Test
public void testDeleteById() {
   
    int result = userMapper.deleteById(1307918819829981186L);
    System.out.println(result);
}

执行日志:

JDBC Connection [HikariProxyConnection@693275170 wrapping com.mysql.cj.jdbc.ConnectionImpl@7ea08277] will not be managed by Spring
==>  Preparing: DELETE FROM user WHERE id=?
==> Parameters: 1307918819829981186(Long)
<==    Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@62e6a3ec]
1

根据 ID 批量删除

@Test
public void testDeleteBatchIds() {
   
    int result = userMapper.deleteBatchIds(Arrays.asList(1307916018324221953L, 1307916038335238146L));
    System.out.println(result);
}

执行日志:

JDBC Connection [HikariProxyConnection@1299885218 wrapping com.mysql.cj.jdbc.ConnectionImpl@604b1e1d] will not be managed by Spring
==>  Preparing: DELETE FROM user WHERE id IN ( ? , ? )
==> Parameters: 1307917913344421889(Long), 1307918050477113346(Long)
<==    Updates: 2
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5754de72]
2

根据 ID 修改

@Test
public void testUpdateById() {
   
    User user = new User();
    user.setId(5L);
    user.setAge(10);
    int result = userMapper.updateById(user);
    System.out.println(result);
}

执行日志:

JDBC Connection [HikariProxyConnection@719146276 wrapping com.mysql.cj.jdbc.ConnectionImpl@3f93e4a8] will not be managed by Spring
==>  Preparing: UPDATE user SET age=? WHERE id=?
==> Parameters: 10(Integer), 5(Long)
<==    Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5754de72]
1

根据 ID 查询

@Test
public void testSelectById() {
   
    User user = userMapper.selectById(5L);
    System.out.println(user);
}

执行日志:

JDBC Connection [HikariProxyConnection@1286643712 wrapping com.mysql.cj.jdbc.ConnectionImpl@5ff2e84b] will not be managed by Spring
==>  Preparing: SELECT id,name,age,email FROM user WHERE id=?
==> Parameters: 5(Long)
<==    Columns: id, name, age, email
<==        Row: 5, Billie, 10, test5@baomidou.com
<==      Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@62e6a3ec]
User{id=5, name='Billie', age=10, email='test5@baomidou.com'}

根据 ID 批量查询

@Test
public void testSelectBatchIds() {
   
    List<User> users = userMapper.selectBatchIds(Arrays.asList(4L, 5L));
    System.out.println(users);
}

执行日志:

JDBC Connection [HikariProxyConnection@1970900227 wrapping com.mysql.cj.jdbc.ConnectionImpl@40f8f5a8] will not be managed by Spring
==>  Preparing: SELECT id,name,age,email FROM user WHERE id IN ( ? , ? )
==> Parameters: 4(Long), 5(Long)
<==    Columns: id, name, age, email
<==        Row: 4, Sandy, 21, test4@baomidou.com
<==        Row: 5, Billie, 10, test5@baomidou.com
<==      Total: 2
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@c81fd12]
[User{id=4, name='Sandy', age=21, email='test4@baomidou.com'}, User{id=5, name='Billie', age=10, email='test5@baomidou.com'}]

columnMap参数

根据 columnMap 条件删除记录

删除年龄为18岁的用户

@Test
public void testDeleteByMap() {
   
    Map<String, Object> map = new HashMap<>();
    map.put("age", 18);
    int result = userMapper.deleteByMap(map);
    System.out.println(result);
}

执行日志:

JDBC Connection [HikariProxyConnection@874981105 wrapping com.mysql.cj.jdbc.ConnectionImpl@c808207] will not be managed by Spring
==>  Preparing: DELETE FROM user WHERE age = ?
==> Parameters: 18(Integer)
<==    Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@31ee96f4]
1

根据 columnMap 条件查询

查看年龄为28岁的用户

@Test
public void testSelectByMap() {
   
    Map<String, Object> map = new HashMap<>();
    map.put("age", 28);
    List<User> users = userMapper.selectByMap(map);
    System.out.println(users);
}

执行日志:

JDBC Connection [HikariProxyConnection@1183572822 wrapping com.mysql.cj.jdbc.ConnectionImpl@5c9ac4cc] will not be managed by Spring
==>  Preparing: SELECT id,name,age,email FROM user WHERE age = ?
==> Parameters: 28(Integer)
<==    Columns: id, name, age, email
<==        Row: 3, Tom, 28, test3@baomidou.com
<==      Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@652ab8d9]
[User{id=3, name='Tom', age=28, email='test3@baomidou.com'}]

上一篇:SpringBoot 全家桶 | MyBatisPlus(一)快速开始
下一篇:SpringBoot 全家桶 | MyBatisPlus(三)条件构造器

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
17小时前
|
SQL 前端开发 Java
通过使用Mybatis插件来实现数据的分页功能
通过使用Mybatis插件来实现数据的分页功能
|
17小时前
|
SQL Java 数据库连接
Mybatis技术专题(3)MybatisPlus自带强大功能之多租户插件实现原理和实战分析
Mybatis技术专题(3)MybatisPlus自带强大功能之多租户插件实现原理和实战分析
86 1
|
17小时前
|
SQL Java 数据库连接
Mybatis是如何实现分页功能的
Mybatis是如何实现分页功能的
12 0
|
17小时前
|
移动开发 Java 测试技术
Spring MVC+Spring+Mybatis实现支付宝支付功能(附完整代码)
Spring MVC+Spring+Mybatis实现支付宝支付功能(附完整代码)
28 1
|
17小时前
|
SQL Java 关系型数据库
【MyBatis-Plus】快速精通Mybatis-plus框架—核心功能
【MyBatis-Plus】快速精通Mybatis-plus框架—核心功能
54 0
【MyBatis-Plus】快速精通Mybatis-plus框架—核心功能
|
17小时前
如何使用MybatisPlus的代码生成器功能?
如何使用MybatisPlus的代码生成器功能?
|
17小时前
|
算法 Java 数据库连接
实现 MyBatis-Plus 中的配置加密功能(使用 AES 算法)
实现 MyBatis-Plus 中的配置加密功能(使用 AES 算法)
198 0
|
17小时前
|
测试技术 数据库
深入解析MyBatis-Plus中的逻辑删除功能及实例
深入解析MyBatis-Plus中的逻辑删除功能及实例
261 0
|
17小时前
|
SQL NoSQL Java
MyBatis-Plus主要提供了哪些功能?
MyBatis-Plus主要提供了哪些功能?
25 0
|
17小时前
|
API 数据库
springboot+mybatis-plus演示做题功能后端实现
springboot+mybatis-plus演示做题功能后端实现
25 0