拼接可用于in查询字符串

组件功能:

将一个可遍历的对象中的数据拼接为可用于sql中进行in查询的字符串

注:

当前方法还无法进行数组遍历

/**
 * 组件:拼接参数
 * 拼接好的样式为:(1,2,3....)
 * @param t 包含参数的列表
 * @param <T>实现Iterable的接口的类型
 * @return 拼接好的字符串
 */
protected  <T extends Iterable> String joinParameter(T t){
    //参数判空
    if(t == null){
        return null;
    }else {

        //参数字符串暂存区
        StringBuffer tempBuffer = new StringBuffer('(');

        //使用迭代器遍历列表
        Iterator iterator = t.iterator();
        //用于定位的索引
        int count = 0;
        while (iterator.hasNext()) {

            Object obj = iterator.next();

            //判断是否需要增加‘,’
            if (count > 0) {
                tempBuffer.append(',');
            }

            //根据不同的类型进行拼接字符串
            if (obj instanceof String) {
                try {
                    tempBuffer.append('\'').append(URLEncoder.encode(obj.toString(), "UTF-8")).append('\'');
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            } else if (obj instanceof Number) {
                tempBuffer.append(obj);
            }

            //索引+1
            count++;

        }
        //拼接结束符
        tempBuffer.append(')');

        //返回字符串
        return tempBuffer.toString();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值