简体   繁体   English

使用JdbcTemplate获取查询返回的行数

[英]Get number of rows returned from query with JdbcTemplate

I have what seems like a simple question but have not been able to figure it out or found a solution online. 我有一个看似简单的问题,但无法解决或在线找到解决方案。 What I am doing is writing a DAO method to get users under an account. 我正在做的是编写一种DAO方法,以使用户拥有一个帐户。 I include parameters to filter and order my query. 我包括用于过滤和排序查询的参数。 There are different tables involved and my query uses inner joins, left outer joins... .it's not that important. 这里涉及到不同的表,我的查询使用内部联接,左外部联接...。并不是那么重要。 What is important is that I would like to know how many rows my query is returning, even after limiting ( total regardless of limit ). 重要的是,即使限制后,我也想知道我的查询返回了多少行(总数不计限制)。 This is some code inside my method.. 这是我方法中的一些代码。

        List<User> users = new ArrayList<User>();

        UserRowCallbackHandler userHandler = new UserRowCallbackHandler(accountId);

        String query = null;

        if (limit == -1) {
            query = String.format("%s\nORDER BY %s",
                    sqlXml.getQuery("GET_USERS_BY_ACCOUNT_ID"),
                    orderBy);
        } else {
            query = String.format("%s\nORDER BY %s \nLIMIT %d, %d\n",
                    sqlXml.getQuery("GET_USERS_BY_ACCOUNT_ID"),
                    orderBy, offset, limit);
        }


        getJdbcTemplate().query(
                query, userHandler,
                accountId, filter);

        users.addAll(userHandler.getUsers());

What can I add to my query ( or how can I utilize my JbdcTemplate object ) to see the number of total rows my query returns, even after limiting?? 我可以向查询添加什么(或者如何利用我的JbdcTemplate对象)以查看查询返回的总行数,即使在限制之后也可以?

Ok, I'm not very happy with my solution but it works for now. 好的,我对我的解决方案不是很满意,但是目前可以使用。 The answer is that I will have to run 3 queries, which I guess isn't that much of a problem but I would have liked to only run a single query. 答案是,我将必须运行3个查询,我想这不是什么大问题,但我希望只运行一个查询。

The answer is to run the initial query ( without the limit ) and run a second query ( SELECT FOUND_ROWS() ) immediately afterwards. 答案是运行初始查询(无限制),然后立即运行第二个查询(SELECT FOUND_ROWS())。 Then run a 3rd query with limit specified. 然后运行指定限制的第三个查询。 I guess if there's not a limit specified I'd only have to run two queries but I'm using this to do server-side pagination so there will almost always be a limit. 我想如果没有指定限制,我只需要运行两个查询,但是我使用它来进行服务器端分页,因此几乎总是有一个限制。 Here's the code 这是代码

        UserRowCallbackHandler userHandler = new UserRowCallbackHandler(accountId);

        query = String.format("%s\nORDER BY %s",
                sqlXml.getQuery("GET_USERS_BY_ACCOUNT_ID"),
                orderBy);

        queryForTotal = "SELECT FOUND_ROWS()";

        getJdbcTemplate().query(
                query,
                userHandler, accountId, filter);

        filteredTotal = getJdbcTemplate().queryForInt(queryForTotal);

        if (limit != -1) {
            // re-initialize handler for clean data
            userHandler = new UserRowCallbackHandler(accountId);

            query = String.format("%s\nORDER BY %s \nLIMIT %d, %d\n",
                    sqlXml.getQuery("GET_USERS_BY_ACCOUNT_ID"),
                    orderBy, offset, limit);


            getJdbcTemplate().query(
                    query,
                    userHandler, accountId, filter);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM