去掉参数中第一个“,”

记录一下,前端传参中,传给我参数是"categoryIds: ,1731557494586241026,1731569816263311362,1731569855534579713,1731858335179223042,1731858366821052418"

但是后端,因为我的mybati是in查询,所以因为第一个是","。所以会导致系统报错。

下面提供三个解决方法:
方法一:

找前端,让前端改!!

方法二(修改mybatis):
XML 复制代码
<if test="pd.categoryIds != null and pd.categoryIds.trim() != ''">
    <choose>
        <when test="pd.categoryIds.startsWith(',')">
            <bind name="categoryIds" value="pd.categoryIds.substring(1)" />
        </when>
        <otherwise>
            <bind name="categoryIds" value="pd.categoryIds" />
        </otherwise>
    </choose>
    AND a.category_id IN (${categoryIds})
</if>

在这个改进后的条件语句中,我们首先使用 trim() 方法去除 categoryIds 字符串两端的空格。然后使用 startsWith(',') 方法判断是否以逗号开头,如果是则使用 substring(1) 方法去除开头的逗号。最后,我们将处理后的 categoryIds 值赋给一个新的变量 categoryIds,并在条件语句中使用它来构建查询条件。

方法三:
java 复制代码
    if (categoryIds.startsWith(",")) {
            categoryIds = categoryIds.substring(1);
        }

        String[] categoryIdArr = categoryIds.split(",");
        List<Long> categoryIdList = new ArrayList<>();

        for (String categoryId : categoryIdArr) {
            categoryIdList.add(Long.parseLong(categoryId.trim()));
        }
  1. 如果 categoryIds 字符串以逗号开头,那么使用 substring(1) 方法去掉开头的逗号。这是因为在查询条件中,逗号前面不应该有任何字符。
  2. 使用 split(",") 方法将 categoryIds 字符串按逗号分隔成多个字符串,然后赋值给一个名为 categoryIdArr 的字符串数组。这个数组包含了多个类别 ID 字符串。
  3. 创建一个名为 categoryIdList 的空列表,用于存储将要查询的类别 ID。
  4. 遍历 categoryIdArr 数组中的每个字符串,在遍历过程中,使用 Long.parseLong() 方法将字符串解析成 Long 类型,并且使用 trim() 方法去除字符串两端的空格。然后,将解析后的 Long 类型值添加到 categoryIdList 列表中。
  5. 最后,当循环遍历完成后,categoryIdList 列表中包含了多个类别 ID 的 Long 类型值,这个列表就可以用于查询数据了。
相关推荐
蔡蓝2 小时前
设计模式-状态模式
ui·设计模式·状态模式
lyh13448 小时前
在macOS上运行Linux容器的方法
数据结构·状态模式
人生如梦-3 天前
抽奖系统核心——抽奖管理
状态模式
lyh13446 天前
【Fiddler工具判断前后端Bug】
状态模式
季鸢6 天前
Java设计模式之状态模式详解
java·设计模式·状态模式
萌新小码农‍7 天前
Spring框架学习day7--SpringWeb学习(概念与搭建配置)
学习·spring·状态模式
EndingCoder8 天前
React从基础入门到高级实战:React 高级主题 - React 微前端实践:构建可扩展的大型应用
前端·javascript·react.js·前端框架·状态模式
何双新11 天前
第14讲、Odoo 18 实现一个Markdown Widget模块
python·状态模式
zwjapple12 天前
react-native的token认证流程
react native·状态模式·token
何中应12 天前
【设计模式-4.6】行为型——状态模式
java·设计模式·状态模式