simple jdbctemplate.query()应该返回一个列表,但不是

dffbzjpn  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(299)

我使用的是spring 4.0(boot),代码如下:

String sql = "SELECT emp_id,name,role FROM employees";
        List<Employee> employees = jdbcTemplate.query(sql,(rs, rowNum)-> 
        new Employee(rs.getInt("emp_id"), rs.getString("name"),
                rs.getString("role")));         
        employees.forEach(employee -> {log.info(employee.toString());
            log.info("part a");});

代码看起来简单明了,但问题是员工根本不返回日志中的任何内容。这不意味着雇员中的所有内容都是空的吗?
记录器本身应该没有问题,因为在这段代码工作之前,日志记录就已经完成了。
数据库输入正确。当我通过mysql客户端进行查询时,我可以在那里看到它们。
我犯了什么样的java错误(如果是这样的话)?
编辑:这是我执行简单数据库查询的全部功能,上面的代码来自于此:

@Override
    public void run(String... args) throws Exception {
        log.info("Creating tables");

        jdbcTemplate.execute("DROP TABLE IF EXISTS employee");
        jdbcTemplate.execute("CREATE TABLE employee (emp_id int, name varchar(100), role varchar(100))");

        log.info("Inserting Baggins Hopkins");
        int rowsAffected = jdbcTemplate.update("INSERT INTO EMPLOYEE(EMP_ID, NAME, ROLE)"
                + " VALUES(1,'Baggins Hopkins','thief')");
        log.info("rows affected: "+ Integer.toString(rowsAffected));
        int rowsAffected1 = jdbcTemplate.update("INSERT INTO EMPLOYEE(EMP_ID, NAME, ROLE)"
                + " VALUES(2,'Doolous Hopkins','robber')");
        log.info("rows affected: "+ Integer.toString(rowsAffected1));
        log.info("Querying for employee");
        String sql = "SELECT emp_id,name,role FROM employee";
        List<Employee> employees = jdbcTemplate.query(sql,(rs, rowNum)-> 
        new Employee(rs.getInt("emp_id"), rs.getString("name"),
                rs.getString("role")));
        log.info("Part A:");
        log.info(String.valueOf(employees.isEmpty()));
//      List<Employee> employees = new ArrayList<Employee>();
//      final List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql);
//      for(final Map row : rows) {
//          Employee employee = new Employee();
//          employee.setId((int)row.get("emp_id"));
//          employee.setName((String)row.get("name"));
//          employee.setRole((String)row.get("role"));
//      };

        employees.forEach(employee -> {log.info(employee.toString());
            log.info("part a");});

    }

这是发生错误时的日志:

:: Spring Boot ::        (v2.1.0.RELEASE)

2018-11-07 22:08:36.599  INFO 5476 --- [           main] c.j.jdbctest1.JdbcTest1Application       : Starting JdbcTest1Application on KitKat with PID 5476 (C:\Users\Nano\Downloads\jdbc-test1\jdbc-test1\target\classes started by Nano in C:\Users\Nano\Downloads\jdbc-test1\jdbc-test1)
2018-11-07 22:08:36.599  INFO 5476 --- [           main] c.j.jdbctest1.JdbcTest1Application       : No active profile set, falling back to default profiles: default
2018-11-07 22:08:37.458  INFO 5476 --- [           main] c.j.jdbctest1.JdbcTest1Application       : Started JdbcTest1Application in 1.156 seconds (JVM running for 2.08)
2018-11-07 22:08:37.474  INFO 5476 --- [           main] c.j.jdbctest1.JdbcTest1Application       : Creating tables
2018-11-07 22:08:37.474  INFO 5476 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2018-11-07 22:08:37.614  INFO 5476 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2018-11-07 22:08:38.873  INFO 5476 --- [           main] c.j.jdbctest1.JdbcTest1Application       : Inserting Baggins Hopkins
2018-11-07 22:08:39.042  INFO 5476 --- [           main] c.j.jdbctest1.JdbcTest1Application       : rows affected: 1
2018-11-07 22:08:39.183  INFO 5476 --- [           main] c.j.jdbctest1.JdbcTest1Application       : rows affected: 1
2018-11-07 22:08:39.183  INFO 5476 --- [           main] c.j.jdbctest1.JdbcTest1Application       : Querying for employee
2018-11-07 22:08:39.214  INFO 5476 --- [           main] c.j.jdbctest1.JdbcTest1Application       : Part A:
2018-11-07 22:08:39.214  INFO 5476 --- [           main] c.j.jdbctest1.JdbcTest1Application       : true
2018-11-07 22:08:39.230  INFO 5476 --- [       Thread-1] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2018-11-07 22:08:39.230  INFO 5476 --- [       Thread-1] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.
hrysbysz

hrysbysz1#

您可以使用其他方式获取员工列表:

private static final RowMapper<Employee> ROW_MAPPER = BeanPropertyRowMapper.newInstance(Employee.class);

String sql = "SELECT emp_id,name,role FROM employees";
List<Employee> employees = jdbcTemplate.query(sql, ROW_MAPPER);

List<Employee> employees = jdbcTemplate.queryForList(sql, Employee.class);
0yycz8jy

0yycz8jy2#

原版海报回答:
这是一个简单的语法错误。正如评论员指出的那样,我一直插错了表格。。。我的sql查询语法错误。
以前,我是在“employees”中插入,但我应该在“employees”中插入
但是,返回包含map的列表的queryforlist()仍然返回null。我在上面的帖子中对此进行了评论。我不知道这有什么问题(我以前没有尝试过使用list>)。但问题解决了。

相关问题