MyBatis-Flex BaseMapper的接口基本用法小结

2025-02-14 18:50

本文主要是介绍MyBatis-Flex BaseMapper的接口基本用法小结,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

《MyBatis-FlexBaseMapper的接口基本用法小结》本文主要介绍了MyBatis-FlexBaseMapper的接口基本用法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具...

MyBatis-Flex简单介绍

MyBatis-Flex 是一个优雅的 MyBatis 增强框架,它非常轻量、同时拥有极高的性能与灵活性。我们可以轻松的使用 Mybaits-Flex 链接任何数据库,其内置的 QueryWrapper^亮点 帮助我们极大的减少了 SQL 编写的工作的同时,减少出错的可能性。

总而言之,MyBatis-Flex 能够极大地提高我们的开发效率和开发体验,让我们有更多的时间专注于自己的事情。

特性

1、轻量

除了 MyBatis,没有任何第三方依赖轻依赖、没有任何拦截器,其原理是通过 SqlProvider 的方式实现的轻实现。同时,在执行的过程中,没有任何的 Sql 解析(Parse)轻运行。 这带来了几个好处:1、极高的性能;2、极易对代码进行跟踪和调试; 3、更高的把控性。

2、灵活

支持 Entity 的增删改查、以及分页查询的同时,MyBatis-Flex 提供了 Db + Row^灵活 工具,可以无需实体类对数据库进行增删改查以及分页查询。 与此同时,MyBatis-Flex 内置的 QueryWrapper^灵活 可以轻易的帮助我们实现 多表查询链接查询子查询 等等常见的 SQL 场景。

3、强大

支持任意关系型数据库,还可以通过方言持续扩展,同时支持 多(复合)主键逻辑删除乐观锁配置数据脱敏数据审计、 数据填充 等等功能。

基础方法

INSERT

BaseMapper 的接口提供了 insert 和 insertBatch 方法,用于新增数据。

  • insert(entity):插入实体类数据,不忽略 null 值。
  • insertSelective(entity):插入实体类数据,但是忽略 null 的数据,只对有值的内容进行插入。这样的好处是数据库已经配置了一些默认值,这些默认值才会生效。
  • insert(entity, ignoreNulls):插入实体类数据。
  • insertWithPk(entity):插入带有主键的实体类,不忽略 null 值。
  • insertSelectiveWithPk(entity):插入带有主键的实体类,忽略 null 值。
  • insertWithPk(entity, ignoreNulls):带有主键的插入,此时实体类不会经过主键生成器生成主键。
  • insertBatch(entities):批量插入实体类数据,只会根据第一条数据来构建插入的字段内容。
  • insertBatch(entities, size):批量插入实体类数据,按 size 切分。
  • insertOrUpdate(entity):插入或者更新,若主键有值,则更新,若没有主键值,则插入,插入或者更新都不会忽略 null 值。
  • insertOrUpdateSelective(entity):插入或者更新,若主键有值,则更新,若没有主键值,则插入,插入或者更新都会忽略 null 值。
  • insertOrUpdate(entity, ignoreNulls):插入或者更新,若主键有值,则更新,若没有主键值,则插入。

① insert

    /**
     * insert(entity):插入实体类数据,不忽略 null 值。
     * insert(entity, ignoreNulls):插入实体类数据。
     */
    @Test
    public void testInsert() {
        /**
         * 默认不忽略null值
         * INSERT INTO `tb_account`(`user_name`, `age`, `birthday`) VALUES (?, ?, ?)
         */
        int row = accountMapper.insert(new Account().setUserName(null).setBirthday(new Date()).setAge(25));
        Assertions.assertEquals(row, 1);

        /**
         * ignoreNulls true:忽略null , false:不忽略null
         * INSERT INTO `tb_account`(`user_name`) VALUES (?)
         */
        int row2 = accountMapper.insert(new Account().setUserName("ly3").setBirthday(null).setAge(null), true);
        Assertions.assertEquals(row2, 1);
    }

② insertSelective 

    /**
     * insertSelective(entity):插入实体类数据,但是忽略 null 的数据,只对有值的内容进行插入。这样的好处是数据库已经配置了一些默认值,这些默认值才会生效。
     */
    @Test
    public void testInsertSelective() {
        /**
         * INSERT INTO `tb_account`(`user_name`) VALUES (?)
         */
        int row = accountMapper.insertSelective(new Account().setUserName("陈远航").setAge(null));
        Assertions.assertEquals(row, 1);
    }

③ insertWithPk 

    /**
     * insertWithPk(entity):插入带有主键的实体类,不忽略 null 值。
     */
    @Test
    public void testInsertWithPk() {
        /**
         * INSERT INTO `tb_account`(`id`, `user_name`, `age`, `birthday`) VALUES (?, ?, ?, ?)
         */
        int row = accountMapper.insertWithPk(new Account().setUserName("廖楷瑞").setId(5L).setBirthday(null));
        Assertions.assertEquals(row, 1);
    }

④ insertBatch 

    /**
     * insertBatch(entities):批量插入实体类数据,只会根据第一条数据来构建插入的字段内容。
     * insertBatch(entities, size):批量插入实体类数据,按 size 切分。
     */
    @Test
    public void testInsertBatch() {
        List<Account> accounts = new ArrayList<>(10);
        for (int i = 0; i < 10; i++) {
            Account account = new Account().setUserName("ly" + i).setBirthday(new Date()).setAge(20 + i);
            accounts.add(account);
        }
        /**
         * 批量插入,可以指定每次插入的数据大小size
         * size=2 : INSERT INTO `tb_account`(`user_name`, `age`, `birthday`) VALUES (?, ?, ?), (?, ?, ?)
         * size=3 : INSERT INTO `tb_account`(`user_name`, `age`, `birthday`) VALUES (?, ?, ?), (?, ?, ?), (?, ?, ?)
         * ......
         */
        int rows = accountMapper.insertBatch(accounts, 2);
        System.out.println("rows = " + rows);
    }

⑤ insertOrUpdate 

    /**
     * insertOrUpdate(entity):插入或者更新,若主键有值,则更新,若没有主键值,则插入,插入或者更新都不会忽略 null 值。
     * insertOrUpdateSelective(entity):插入或者更新,若主键有值,则更新,若没有主键值,则插入,插入或者更新都会忽略 null 值。
     * insertOrUpdate(entity, ignoreNulls):插入或者更新,若主键有值,则更新,若没有主键值,则插入。
     */
    @Test
    public void testInsertOrUpdate() {
        Account account = new Account().setUserName("ly-update2").setId(3L).setBirthday(new Date()).setAge(21);
        /**
         * UPDATE `tb_account` SET `user_name` = ? , `age` = ? , `birthday` = ? WHERE `id` = ?
         */
        int row = accountMapper.insertOrUpdate(account);
        Assertions.assertEquals(row, 0, 1);

        account = new Account().setUserName("ly-update2").setBirthday(null).setAge(21);
        /**
         * INSERT INTO `tb_account`(`user_name`, `age`) VALUES (?, ?)
         */
        int row2 = accountMapper.insertOrUpdateSelective(account);
        Assertions.assertEquals(row2, 0, 1);
    }

DELETE

BaseMapper 的接口提供了 deleteById、deleteBatchByIds、deleteByMap、deleteByQuery 方法,用于删除数据;

  • deleteById(id):根据主键删除数据。如果是多个主键的情况下,需要传入数组,例如:new Integer[]{100,101}。
  • deleteBatchByIds(ids):根据多个主键批量删除数据。
  • deleteBatchByIds(ids, size):根据多个主键批量删除数据。
  • deleteByMap(whereConditions):根据 Map 构建的条件来删除数据。
  • deleteByCondition(whereConditions):根据查询条件来删除数据。
  • deleteByQuery(queryWrapper):根据查询条件来删除数据。

① deleteById

    /**
     * deleteById(id):根据主键删除数据。
     * ??? 如果是多个主键的情况下,需要传入数组,例如:new Integer[]{100,101}。 ??? 怀疑态度
     */
    @Test
    public void testDeleteById() {
        /**
         * DELETE FROM `tb_account` WHERE `id` = ?
         */
        int row = accountMapper.deleteById(9L);
        Assertions.assertTrue(row > 0);
    }

② deleteBatchByIds  

    /**js
     * deleteBatchByIds(ids):根据多个主键批量删除数据。
     * deleteBatchByIds(ids, size):根据多个主键批量删除数据。
     */
    @Test
    public void testDeleteBatchByIds() {
        List<Integer> ids = List.of(5, 6, 7, 8);
        /**
         * DELETE FROM `tb_account` WHERE `id` = ? OR `id` = ? OR `id` = ? OR `id` = ?
         */
        int rows = accountMapper.deleteBatchByIds(ids);
        Assertions.assertTrue(rows > 0);

        /**
         * 分批删除
         * size=2 : DELETE FROM `tb_account` WHERE `id` = ? OR `id` = ?
         */
        int rows2 = accountMapper.deleteBatchByIds(ids, 2);
        Assertions.assertTrue(rows2 > 0);
    }

③ deleteByMap 

    /**
     * deleteByMap(whereConditions):根据 Map 构建的条件来删除数据。
     */
    @Test
    public void testDeleteBhttp://www.chinasem.cnyMap() {
        /**
         * DELETE FROM `tb_account` WHERE `tb_account`.`age` = ?
         */
        int rows = accountMapper.deleteByMap(Map.of("age", 25));
        Assertions.assertTrue(rows > 0);
    }

④ deleteByCondition  

    /**
     * deleteByCondition(whereConditions):根据查询条件来删除数据。
     */
    @Test
    public void testDeleteByCondition() {
        QueryCondition condition = QueryCondition.createEmpty().and("age = 27");
        /**
         * DELETE FROM `tb_account` WHERE age = 27
         */
        int rows = accountMapper.deleteByCondition(condition);
        Assertions.assertTrue(rows > 0);

        /**
         * DELETE FROM `tb_account` WHERE `id` > ?
         */
        int rows2 = accountMapper.deleteByCondition(ACCOUNT.ID.gt(35));
        Assertions.assertTrue(rows2 > 0);
    }

⑤ deleteByQuery

    /**
     * deleteByQuery(queryWrapper):根据查询条件来删除数据。
     */
    @Test
    public void testDeleteByQuery() {
        /**
         * DELETE FROM `tb_account` WHERE `age` >= ?
         */
        QueryWrapper wrapper = QueryWrapper.create().where(ACCOUNT.AGE.ge(32));
        int rows = accountMapper.deleteByQuery(wrapper);
        Assertions.assertTrue(rows > 0);
    }

UPDATE

BaseMapper 的接口提供了 update、updateByMap、updateByQuery 方法,用于更新数据;

  • update(entity):根据主键来更新数据,若实体类属性数据为 null,该属性不会新到数据库。
  • update(entity, ignoreNulls):根据主键来更新数据到数据库。
  • updateByMap(entity, whereConditions):根据 Map 构建的条件来更新数据。
  • updateByMap(entity, ignoreNulls, whereConditions):根据 Map 构建的条件来更新数据。
  • updateByCondition(entity, whereConditions):根据查询条件来更新数据。
  • updateByCondition(entity, ignoreNulls, whereConditions):根据查询条件来更新数据。
  • updateByQuery(entity, queryWrapper):根据查询条件来更新数据。
  • updateByQuery(entity, ignoreNulls, queryWrapper):根据查询条件来更新数据。

① update 

    @Test
    public void testUpdate() {
        Account account = new Account().setId(5L)
                .setAge(39)
                .setUserName("测试修改")
                .setBirthday(new Date(2023, Calendar.SEPTEMBER, 6, 12, 12, 12));
        /**
         * UPDATE `tb_account` SET `user_name` = ? , `age` = ? , `birthday` = ? WHERE `id` = ?
         */
        int rows = accountMapper.update(account);
        Assertions.assertTrue(rows > 0);
    }
	
    @Test
    public void testUpdateIgnoreNulls() {
        Account account = new Account().setId(6L)
                .setAge(null)
                .setUserName(null)
                .setBirthday(new Date(2023, Calendar.SEPTEMBER, 6, 12, 12, 12));
        /**
         * 不忽略null值
         * UPDATE `tb_account` SET `user_name` = ? , `age` = ? , `birthday` = ? WHERE `id` = ?
         */
        int rows = accountMapper.update(account, false);
        System.out.println("rows = " + rows);
    }

② updateByMap 

    @Test
    public void testUpdateByMap() {
        Account account = new Account()
                .setId(7L) // 不生效
                .setUserName("updateByMap")
                .setAge(24)
                .setBirthday(null);
        // 注意:这样更新实体类的id是不生效的
        Map<String, Object> whereCondition = Map.of("id", 13);

        /**
         * 忽略null
         * UPDATE `tb_account` SET `user_name` = ? , `age` = ? WHERE `id` = ?
         */
        accountMapper.updateByMap(account, whereCondition);

        /**
         * 不忽略null
         * UPDATE `tb_account` SET `user_name` = ? , `age` = ? , `birthday` = ? WHERE `id` = ?
         */
        accountMapper.updateByMap(account, false, whereCondition);
    }

③ updateByCondition 

    /**
     * updateByCondition(entity, whereConditions):根据查询条件来更新数据。
     * updateByCondition(entity, ignoreNulls, whereConditions):根据查询条件来更新数据。
     */
    @Test
    public void testUpdateByCondition() {
        Account account = new Account()
                .setId(8L)
                .setUserName("updateByCondition")
                .setBirthday(DateUtil.toDate(LocalDateTime.now()));
        QueryCondition condition = QueryCondition.create(new QueryColumn("id"),
                SqlConsts.EQUALS, 8);
        /**
         * 忽略null
         * UPDATE `tb_account` SET `user_name` = ? , `birthday` = ? WHERE `id` = ?
         */
        accountMapper.updateByCondition(account, condition);

        /**
         * 不忽略null
         * UPDATE `tb_account` SET `user_name` = ? , `age` = ? , `birthday` = ? WHChina编程ERE `id` = ?
         */
        accountMapper.updateByCondition(account, false, condition);
    }

④ updateByQuery

    /**
     * updateByQuery(entity, queryWrapper):根据查询条件来更新数据。
     * updateByQuery(entity, ignoreNulls, queryWrapper):根据查询条件来更新数据。
     */
    @Test
    public void testUpdateByQuery() {
        Account account = new Account().setUserName("updateByQuery");

        /**
         * UPDATE `tb_account` SET `user_name` = ? WHERE `id` = ?
         */
        accountMapper.updateByQuery(accou编程nt,
                QueryWrapper.create().where(ACCOUNT.ID.eq(9L)));
    }

⑤ UpdateEntity部分字段更新

    @Test
    public void testUpdateEntity1() {
        Account account = UpdateEntity.of(Account.class, 10L);
        /**
         * 官方说明:通过 UpdateEntity 创建的对象,只会更新调用了 setter 方法的字段,若不调用 setter 方法,不管这个对象里的属性的值是什么,都不会更新到数据库。
         */
        account.setAge(66)
                .setUserName("UpdateEntity")
                .setBirthday(null);
        accountMapper.update(account);
    }

    @Test
    public void testUpdateEntity2() {
        Account account = new Account()
                .setId(10L)
                .setUserName("UpdateEntity2");
        UpdateWrapper wrapper = UpdateWrapper.of(account);
        // wrapper.set(ACCOUNT.AGE, ACCOUNT.AGE.add(1));

        wrapper.setRaw("age", "age + 1");
        accountMapper.update(account);
    }

    @Test
    public void testUpdateEntity3() {
        Account account = new Account()
                .setId(10L)
                .setUserName("UpdateEntity2");
        UpdateWrapper wrapper = UpdateWrapper.of(account);

        // wrapper.set(ACCOUNT.AGE, ACCOUNT.AGE.add(1));

        wrapper.setRaw(ACCOUNT.AGE, "select * from t_account where id = 3");
        // accountMapper.updateByCondition(account, wrapper);
    }

⑥ UpdateChain

    @Test
    public void testUpdateChain() {
        /**
         * UPDATE `tb_account` SET `birthday` = ? , `age` = age + 1 WHERE `age` = ?
         */
        UpdateChain.of(Account.class)
                .set(Account::getBirthday, new Date())
                .setRaw(Account::getAge, "age + 1")
                .where(ACCOUNT.AGE.eq(11)).update();
    }

set() 和 setRaw() 的区别在 Row、UpdateWrapper、UpdateChain 中,都提供了 set() 和 setRaw() 两个方法用于设置数据。 那么,它们有什么区别呢?

  • set() 方法用于设置参数数据。
  • setRaw() 用于设置 SQL 拼接数据。

举个例子,

//更新数据
UpdateChain.of(Account.class)
	.set(Account::getAge, ACCOUNT.AGE.add(1))
	http://www.chinasem.cn.where(Account::getId).ge(100)
	.and(Account::getAge).eq(18)
	.update();

通过 UpdateChain 进行 update(),其执行的 SQL 如下:

UPDATE `tb_account` SET `age` = `age` + 1
WHERE  `id` >= 100 AND `age` = 18

到此这篇关于MyBatis-Flex BaseMapper的接口基本用法小结的文章就介绍到这了,更多相关MyBatis-Flex BaseMapper接口用法内容请搜索China编程(www.chinasem.cn)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程China编程(www.chinasem.cn)! 

这篇关于MyBatis-Flex BaseMapper的接口基本用法小结的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1153435

相关文章

kotlin中的行为组件及高级用法

《kotlin中的行为组件及高级用法》Jetpack中的四大行为组件:WorkManager、DataBinding、Coroutines和Lifecycle,分别解决了后台任务调度、数据驱动UI、异... 目录WorkManager工作原理最佳实践Data Binding工作原理进阶技巧Coroutine

如何用java对接微信小程序下单后的发货接口

《如何用java对接微信小程序下单后的发货接口》:本文主要介绍在微信小程序后台实现发货通知的步骤,包括获取Access_token、使用RestTemplate调用发货接口、处理AccessTok... 目录配置参数 调用代码获取Access_token调用发货的接口类注意点总结配置参数 首先需要获取Ac

讯飞webapi语音识别接口调用示例代码(python)

《讯飞webapi语音识别接口调用示例代码(python)》:本文主要介绍如何使用Python3调用讯飞WebAPI语音识别接口,重点解决了在处理语音识别结果时判断是否为最后一帧的问题,通过运行代... 目录前言一、环境二、引入库三、代码实例四、运行结果五、总结前言基于python3 讯飞webAPI语音

MyBatis-Plus中Service接口的lambdaUpdate用法及实例分析

《MyBatis-Plus中Service接口的lambdaUpdate用法及实例分析》本文将详细讲解MyBatis-Plus中的lambdaUpdate用法,并提供丰富的案例来帮助读者更好地理解和应... 目录深入探索MyBATis-Plus中Service接口的lambdaUpdate用法及示例案例背景

MyBatis-Plus中静态工具Db的多种用法及实例分析

《MyBatis-Plus中静态工具Db的多种用法及实例分析》本文将详细讲解MyBatis-Plus中静态工具Db的各种用法,并结合具体案例进行演示和说明,具有很好的参考价值,希望对大家有所帮助,如有... 目录MyBATis-Plus中静态工具Db的多种用法及实例案例背景使用静态工具Db进行数据库操作插入

Windows命令之tasklist命令用法详解(Windows查看进程)

《Windows命令之tasklist命令用法详解(Windows查看进程)》tasklist命令显示本地计算机或远程计算机上当前正在运行的进程列表,命令结合筛选器一起使用,可以按照我们的需求进行过滤... 目录命令帮助1、基本使用2、执行原理2.1、tasklist命令无法使用3、筛选器3.1、根据PID

Maven pom.xml文件中build,plugin标签的使用小结

《Mavenpom.xml文件中build,plugin标签的使用小结》本文主要介绍了Mavenpom.xml文件中build,plugin标签的使用小结,文中通过示例代码介绍的非常详细,对大家的学... 目录<build> 标签Plugins插件<build> 标签<build> 标签是 pom.XML

JavaScript中的Map用法完全指南

《JavaScript中的Map用法完全指南》:本文主要介绍JavaScript中Map用法的相关资料,通过实例讲解了Map的创建、常用方法和迭代方式,还探讨了Map与对象的区别,并通过一个例子展... 目录引言1. 创建 Map2. Map 和对象的对比3. Map 的常用方法3.1 set(key, v

MyBatis的配置对象Configuration作用及说明

《MyBatis的配置对象Configuration作用及说明》MyBatis的Configuration对象是MyBatis的核心配置对象,它包含了MyBatis运行时所需的几乎所有配置信息,这个对... 目录MyBATis配置对象Configuration作用Configuration 对象的主要作用C

MyBatis与其使用方法示例详解

《MyBatis与其使用方法示例详解》MyBatis是一个支持自定义SQL的持久层框架,通过XML文件实现SQL配置和数据映射,简化了JDBC代码的编写,本文给大家介绍MyBatis与其使用方法讲解,... 目录ORM缺优分析MyBATisMyBatis的工作流程MyBatis的基本使用环境准备MyBati