百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 编程网 > 正文

Mybatis-Plus 详解(二) mybatis plus 2

yuyutoo 2024-10-12 00:03 11 浏览 0 评论

通过前?的学习,我们了解到通过继承 BaseMapper 就可以获取到各种各样的单表操作,接下来我们将详细讲解这些操作。

通?CRUD

1 插?操作

?法定义

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

测试?例

@Test
    public void testInsert(){
        User user = new User();
        user.setAge(18);
        user.setEmail("test@123.cn");
        user.setName("小明");
        //返回的result是受影响的?数,并不是?增后的id
        int result = userMapper.insert(user);
        System.out.println(result);
        System.out.println(user.getId());
    }

MP ?持的 id 策略:

package com.baomidou.mybatisplus.annotation;
import lombok.Getter;
/**
* ?成ID类型枚举类
*
* @author hubin
* @since 2015-11-10
*/
@Getter
public enum IdType {
   /**
   * 数据库ID?增
   */
   AUTO(0),
   /**
   * 该类型为未设置主键类型
   */
   NONE(1),
   /**
   * ?户输?ID
   * <p>该类型可以通过??注册?动填充插件进?填充</p>
   */
   INPUT(2),
   /* 以下3种类型、只有当插?对象ID 为空,才?动填充。 */
   /**
   * 全局唯?ID (idWorker)
   */
   ID_WORKER(3),
   /**
   * 全局唯?ID (UUID)
   */
   UUID(4),
   /**
   * 字符串全局唯?ID (idWorker 的字符串表示)
   */
   ID_WORKER_STR(5);
   private final int key;
   IdType(int key) {
   		this.key = key;
   }
}

修改 User 对象:


@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("tb_user")
public class User {
     @TableId(type = IdType.AUTO) //指定id类型为?增?
     private Long id;
     private String userName;
     private String password;
     private String name;
     private Integer age;
     private String email;
}

@TableField

在 MP 中通过 @TableField 注解可以指定字段的?些属性,常常解决的问题有 2 个:

  • 1 、对象中的属性名和字段名不?致的问题(?驼峰)
  • 2 、对象中的属性字段在表中不存在的问题

其他?法,如?字段不加?查询字段:

效果:

User(id=1, name=null, age=18, email=test1@baomidou.com, address=null)
User(id=2, name=null, age=20, email=test2@baomidou.com, address=null)
User(id=3, name=null, age=28, email=test3@baomidou.com, address=null)
User(id=4, name=null, age=21, email=test4@baomidou.com, address=null)
User(id=5, name=null, age=24, email=test5@baomidou.com, address=null)

2 更新操作

MP 中,更新操作有 2 种,?种是根据 id 更新,另?种是根据条件更新。

根据 id 更新

?法定义:

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

测试:

@Test
    public void testUpdateById() {
        User user = new User();
        user.setId(6L); //主键
        user.setAge(21); //更新的字段
        //根据id更新,更新不为null的字段
        this.userMapper.updateById(user);
    }

根据条件更新

?法定义:

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

测试?例:

@Test
    public void testUpdate() {
        User user = new User();
        user.setAge(22); //更新的字段
        //更新的条件
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("id", 6);
        //执?更新操作
        int result = this.userMapper.update(user, wrapper);
        System.out.println("result = " + result);
    }

3 删除操作

deleteById:

?法定义:

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

测试?例:

@Test
    public void testDeleteById() {
        //执?删除操作
        int result = this.userMapper.deleteById(6L);
        System.out.println("result = " + result);
    }

deleteByMap:

?法定义:

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

测试?例:

@Test
 public void testDeleteByMap() {
   Map<String, Object> columnMap = new HashMap<>();
   columnMap.put("age",21);
   columnMap.put("name","小明");
   //将columnMap中的元素设置为删除的条件,多个之间为and关系
   int result = this.userMapper.deleteByMap(columnMap);
   System.out.println("result = " + result);
 }

delete

?法定义:

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

测试?例:

@Test
    public void testDelete() {
        User user = new User();
        user.setAge(20);
        user.setName("小明");
        //将实体对象进?包装,包装为操作条件
        QueryWrapper<User> wrapper = new QueryWrapper<>(user);
        int result = this.userMapper.delete(wrapper);
        System.out.println("result = " + result);
    }

deleteBatchIds

?法定义:

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

测试?例:

@Test
    public void deleteBatchIds() {
        //根据id集合批量删除
        int result = this.userMapper.deleteBatchIds(Arrays.asList(1L,10L,20L));
        System.out.println("result = " + result);
    }

4 查询操作

MP 提供了多种查询操作,包括根据 id 查询、批量查询、查询单条数据、查询列表、分?查询等操作。

selectById

?法定义:

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

测试?例:

@Test
    public void testSelectById() {
        //根据id查询数据
        User user = this.userMapper.selectById(2L);
        System.out.println("result = " + user);
    }

selectBatchIds

?法定义:

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

测试?例:

@Test
    public void testSelectBatchIds() {
        //根据id集合批量查询
        List<User> users = this.userMapper.selectBatchIds(Arrays.asList(2L, 3L, 10L));
        for (User user : users) {
            System.out.println(user);
        }
    }

selectOne

?法定义:

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

测试?例:

@Test
 public void testSelectOne() {
 QueryWrapper<User> wrapper = new QueryWrapper<User>();
 wrapper.eq("name", "jack");
 //根据条件查询?条数据,如果结果超过?条会报错
 User user = this.userMapper.selectOne(wrapper);
 System.out.println(user);
 }

selectCount

?法定义:

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

测试?例:

@Test
    public void testSelectCount() {
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        wrapper.gt("age", 23); //年龄?于23岁
        //根据条件查询数据条数
        Integer count = this.userMapper.selectCount(wrapper);
        System.out.println("count = " + count);
    }

selectList

?法定义:

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

测试?例:

@Test
    public void testSelectList() {
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        wrapper.gt("age", 23); //年龄?于23岁
        //根据条件查询数据
        List<User> users = this.userMapper.selectList(wrapper);
        for (User user : users) {
            System.out.println("user = " + user);
        }
    }

selectPage

?法定义:

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

配置分?插件:

@Configuration
@MapperScan("com.mp.sb.mapper") //设置mapper接?的扫描包
public class MybatisPlusConfig {
    /**
     * 分?插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

测试?例:

@Test
    public void testSelectPage() {
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        wrapper.gt("age", 20); //年龄?于20岁
        Page<User> page = new Page<>(1,1);
        //根据条件查询数据
        IPage<User> iPage = this.userMapper.selectPage(page, wrapper);
        System.out.println("数据总条数:" + iPage.getTotal());
        System.out.println("总?数:" + iPage.getPages());
        List<User> users = iPage.getRecords();
        for (User user : users) {
            System.out.println("user = " + user);
        }
    }

结果:

数据总条数:3
总?数:3
user = User(id=3, name=null, age=28, email=test3@baomidou.com, address=null)

相关推荐

Python操作Word文档神器:python-docx库从入门到精通

Python操作Word文档神器:python-docx库从入门到精通动动小手,点击关注...

Python 函数调用从入门到精通:超详细定义解析与实战指南 附案例

一、函数基础:定义与调用的核心逻辑定义:函数是将重复或相关的代码块封装成可复用的单元,通过函数名和参数实现特定功能。它是Python模块化编程的基础,能提高代码复用性和可读性。定义语法:...

等这么长时间Python背记手册终于来了,入门到精通(视频400集)

本文毫无套路!真诚分享!前言:无论是学习任何一门语言,基础知识一定要扎实,基础功非常的重要,找一个有丰富编程经验的老师或者师兄带着你会少走很多弯路,你的进步速度也会快很多,无论我们学习的目的是什么,...

图解Python编程:从入门到精通系列教程(附全套速查表)

引言本系列教程展开讲解Python编程语言,Python是一门开源免费、通用型的脚本编程语言,它上手简单,功能强大,它也是互联网最热门的编程语言之一。Python生态丰富,库(模块)极其丰富,这使...

Python入门教程(非常详细)从零基础入门到精通,看完这一篇就够

本书是Python经典实例解析,采用基于实例的方法编写,每个实例都会解决具体的问题和难题。主要内容有:数字、字符串和元组,语句与语法,函数定义,列表、集、字典,用户输入和输出等内置数据结构,类和对象,...

Python函数全解析:从入门到精通,一文搞定!

1.为什么要用函数?函数的作用:封装代码,提高复用性,减少重复,提高可读性。...

Python中的单例模式:从入门到精通

Python中的单例模式:从入门到精通引言单例模式是一种常用的软件设计模式,它保证了一个类只有一个实例,并提供一个全局访问点。这种模式通常用于那些需要频繁创建和销毁的对象,比如日志对象、线程池、缓存等...

【Python王者归来】手把手教你,Python从入门到精通!

用800个程序实例、5万行代码手把手教你,Python从入门到精通!...

Python从零基础入门到精通:一个月就够了

如果想从零基础到入门,能够全职学习(自学),那么一个月足够了。...

Python 从入门到精通:一个月就够了

要知道,一个月是一段很长的时间。如果每天坚持用6-7小时来做一件事,你会有意想不到的收获。作为初学者,第一个月的月目标应该是这样的:熟悉基本概念(变量,条件,列表,循环,函数)练习超过30个编...

Python零基础到精通,这8个入门技巧让你少走弯路,7天速通编程!

Python学习就像玩积木,从最基础的块开始,一步步搭建出复杂的作品。我记得刚开始学Python时也是一头雾水,走了不少弯路。现在回头看,其实掌握几个核心概念,就能快速入门这门编程语言。来聊聊怎么用最...

神仙级python入门教程(非常详细),从0到精通,从看这篇开始!

python入门虽然简单,很多新手依然卡在基础安装阶段,大部分教程对一些基础内容都是一带而过,好多新手朋友,对一些基础知识常常一知半解,需要在网上查询很久。...

Python类从入门到精通,一篇就够!

一、Python类是什么?大家在生活中应该都见过汽车吧,每一辆真实存在、能在路上跑的汽车,都可以看作是一个“对象”。那这些汽车是怎么生产出来的呢?其实,在生产之前,汽车公司都会先设计一个详细的蓝图...

学习Python从入门到精通:30天足够了,这才是python基础的天花板

当年2w买的全套python教程用不着了,现在送给有缘人,不要钱,一个月教你从入门到精通1、本套视频共487集,本套视频共分4季...

30天Python 入门到精通(3天学会python)

以下是一个为期30天的Python入门到精通学习课程,专为零基础新手设计。课程从基础语法开始,逐步深入到面向对象编程、数据处理,最后实现运行简单的大语言模型(如基于HuggingFace...

取消回复欢迎 发表评论: