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));

    哈哈哈,搞定!

  • 相关阅读:
    webpack打包(2)
    webpack打包(1)
    angular(5自定义模块和ionic创建)
    angular(4)路由及其使用
    anjular(3 生命函数及请求)
    Angular(2)
    自学Angular(1)
    Typescript知识总结
    PLC数据采集与MES系统对接
    python格式化日期时间自动补0
  • 原文地址:https://www.cnblogs.com/zacky31/p/8745981.html
Copyright © 2011-2022 走看看