MyBatis-Plus LambdaQueryChainWrapper 使用 find_in_set 函数查询
LambdaQueryChainWrapper 是 MyBatis-Plus 提供的一种查询构造器,可以用于链式查询数据库。find_in_set 是 MySQL 中的一个函数,用于在一个字符串列表中查找指定值的位置。在 LambdaQueryChainWrapper 中使用 find_in_set 函数可以通过如下方式实现:\n\njava\nimport com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;\nimport com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;\n\npublic class Test {\n public static void main(String[] args) {\n LambdaQueryChainWrapper<User> queryWrapper = new LambdaQueryChainWrapper<>(userMapper)\n .select(User::getId, User::getName)\n .eq(User::getGender, "male") // 添加其他查询条件\n .apply("find_in_set('admin', roles)"); // 使用 find_in_set 函数\n\n List<User> userList = queryWrapper.list();\n // 处理查询结果\n }\n}\n\n\n在上述代码中,我们使用 LambdaQueryChainWrapper 进行链式查询,并通过 eq 方法添加了一个等于条件 User::getGender。然后使用 apply 方法传入 "find_in_set('admin', roles)" 字符串作为查询条件,其中 'admin' 是要查找的值,roles 是字符串列表字段的名称。\n\n注意,apply 方法可以接收任意的 SQL 语句作为查询条件,所以我们可以在其中使用 MySQL 中的任意函数进行查询。
原文地址: https://www.cveoy.top/t/topic/qnR6 著作权归作者所有。请勿转载和采集!