当查询具有两个字段时,jdbctemplate返回rowMapper

时间:2019-02-28 21:00:45

标签: spring jdbctemplate

我正在尝试为有两个字段可供选择的查询编写rowMapper。

查询将根据数据库上的用户名级别返回一个组列表。 甚至会返回一行的部门-100和空的部门和组列表,以获得正确的列表或只返回该列表。

查询:

SELECT a.dept, TRIM(c.group_name) 
FROM user_numbers a  
LEFT OUTER JOIN department b ON b.number = a.dept
LEFT OUTER JOIN yard_sets2 c ON c.user_name = b.name
WHERE (a.user_name) = "test"

此处是Java代码:

private JdbcTemplate webJdbcTemplate;

    public List<String> getStoreGroupsByUsername(final String username) {
    String sql = "SELECT a.dept, TRIM(c.group_name) "
            + "FROM " + PREFIX + "user_numbers a "
            + "LEFT OUTER JOIN " + PREFIX + "department b ON b.number = a.dept "
            + "LEFT OUTER JOIN " + PREFIX + "yard_sets2 c ON c.user_name = b.name "
            + "WHERE (a.user_name) = ? ";
        List<String> results = webJdbcTemplate.query(sql, new Object[]{username.toUpperCase()}, (rs, rownum) -> {
            // SKIP departments when value is -100
            if ("-100".equalsIgnoreCase(rs.getString("a.dept"))) {
                return null;
            } else {
                return rs.getString("a.dept");
            }
        });

   /* if(results.size() == 0) {
          // get the other query
        getStoreGroupsAndDeptNbrByUsername(username);
    }*/
        LOG.debug(results + "SAMAR");
        return results.stream()
                .filter(result -> result != null).collect(Collectors.toList());
}

这是我遇到的错误。似乎列名是错误的,但不是,如果我从错误消息中复制查询,它将在数据库上正常工作

  2019-02-28 08:37:55,911 ERROR org.springframework.boot.web.servlet.support.ErrorPageFilter - Forwarding to error page from request [/rest/storegroup/username/test] due to exception [PreparedStatementCallback; uncategorized SQLException for SQL [SELECT a.dept, TRIM(c.group_name) FROM misc_db:user_numbers a LEFT OUTER JOIN misc_db:department b ON b.number = a.dept LEFT OUTER JOIN misc_db:yard_sets2 c ON c.user_name = b.name WHERE (a.user_name) = ? ]; SQL state [IX000]; error code [-79738]; No such column name; nested exception is java.sql.SQLException: No such column name]
org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized SQLException for SQL [SELECT a.dept, TRIM(c.group_name) FROM misc_db:user_numbers a LEFT OUTER JOIN misc_db:department b ON b.number = a.dept LEFT OUTER JOIN misc_db:yard_sets2 c ON c.user_name = b.name WHERE (a.user_name) = ? ]; SQL state [IX000]; error code [-79738]; No such column name; nested exception is java.sql.SQLException: No such column name
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:89) ~[spring-jdbc-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81) ~[spring-jdbc-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81) ~[spring-jdbc-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.jdbc.core.JdbcTemplate.translateException(JdbcTemplate.java:1402) ~[spring-jdbc-5.0.8.RELEASE.jar:5.0.8.RELEASE]

0 个答案:

没有答案