zoukankan      html  css  js  c++  java
  • 使用JdbcTemplate过程中使用到多个参数和like模糊

    项目中经常会用到模糊查询,最近使用JdbcTemplate过程中就遇到了。

    一开始尝试了拼接的方式去

    String sql = "select count(1) from web_users where status = ? and " +
                    "createtime >= ? and createtime <= ? and deleted = 0 and " +
                    "address like '%"+"?"+"%'";
            return getJdbcTemplate().queryForObject(sql, new Object[]{status,
                    new Timestamp(begintime), new Timestamp(endtime), address}, Integer.class);

    String sql = "select * from web_users where status = ? and " +
                    "createtime >= ? and createtime <= ? and deleted = 0 and " +
                    "address like ? limit ? offset ?";
            return getJdbcTemplate().query(sql, new Object[]{status, new Timestamp(begintime),
                    new Timestamp(endtime), "'%"+address+"'%", count, start}, new DaoRowMapper<>(User.class));

    以上两种方式都会报同样的错误。参数不正确。

    既然这两种方式通不行,尝试以下方式:

    List<Object> queryList = new ArrayList<>();
            if (!address.equals("")) {
                sql += " and address like ? ";
                queryList.add("%" + address + "%");
            }
            return getJdbcTemplate().query(sql, queryList.toArray(), new DaoRowMapper<>(User.class));

    哈哈哈,搞定!

  • 相关阅读:
    matplotlib种类
    matplotlib安装
    Python input保证输入为int类型
    centos7 安装 smplayer
    matplotlib与numpy
    Windows系统pip安装whl包
    python3打开winodows文件问题
    centos7 python3 pip
    Python实战171203统计
    Python实战171202元组访问
  • 原文地址:https://www.cnblogs.com/zacky31/p/8745981.html
Copyright © 2011-2022 走看看