使用带有参数的jdbcTemplate.query

问题描述

我在数据库讲座中有3个表--组。 我想在某一天获得某个小组的时间表。我尝试通过这种方式做到这一点:

@Repository
public class Schedule {

    private static final String GET_GROUP_DAY_SCHEDULE = "SELECT * FROM LECTURES " +
            "INNER JOIN LECTUREGROUPS ON LECTURES.ID = LECTUREGROUPS.LECTUREID " +
            "INNER JOIN GROUPS ON GROUPS.ID = LECTUREGROUPS.GROUPID " +
            "WHERE GROUPID = :GROUPID AND DATE = :DATE";

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<Lecture> getGroupDayLectures(int groupId,LocalDateTime dateTime) {
        MapSqlParameterSource parameters =  new MapSqlParameterSource()
                .addValue("groupid",groupId)
                .addValue("date",dateTime);
        return jdbcTemplate.query(GET_GROUP_DAY_SCHEDULE,new BeanPropertyRowMapper<>(Lecture.class),parameters);
    }
}

但是我在原始查询中遇到了异常

Caused by: org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of org.springframework.jdbc.core.namedparam.MapSqlParameterSource. Use setObject() with an explicit Types value to specify the type to use.

我该如何解决? 我还使用了变体

private static final String GET_GROUP_DAY_SCHEDULE = "SELECT * FROM LECTURES " +
            "INNER JOIN LECTUREGROUPS ON LECTURES.ID = LECTUREGROUPS.LECTUREID " +
            "INNER JOIN GROUPS ON GROUPS.ID = LECTUREGROUPS.GROUPID " +
            "WHERE GROUPID = ? AND DATE = ?";

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<Lecture> getGroupDayLectures(int groupId,LocalDateTime dateTime) {
        return jdbcTemplate.query(GET_GROUP_DAY_SCHEDULE,new Object[]{groupId,dateTime},new BeanPropertyRowMapper<>(Lecture.class));
    }

它可以工作,但仅返回列表中的1个演讲(必须为3个)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)