简体   繁体   中英

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. 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??

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.

The answer is to run the initial query ( without the limit ) and run a second query ( SELECT FOUND_ROWS() ) immediately afterwards. 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);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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