【发布时间】:2017-11-24 03:29:23
【问题描述】:
我需要在我的 Spring Boot Web 应用程序中从我的数据库中按日期进行选择。到目前为止,我所拥有的是一份体育比赛列表以及相应的信息。
问题:我无法弄清楚我的选择查询如何将我的字符串类型(dateFrom = '2017-05-02' 和 dateTo = '2017-05-06')转换为日期,如 '2017-02-12' ?
还有如何在一些有多个日期的比赛中用然后一个日期填充我的 RowMapper。
我的数据库架构:
CREATE TABLE competition (
competition_id integer PRIMARY KEY,
nom varchar(128) NOT NULL,
);
CREATE TABLE date (
id integer PRIMARY KEY,
date_time timestamptz,
competition_id integer REFERENCES competition (competition_id)
);
Json 数据:
{
"id": "420",
"name": "SOCCER",
"dates": [
"2016-05-12T03:00:00.000Z"
"2016-05-12T04:00:00.000Z"
"2016-05-12T05:00:00.000Z"
]
},
{
"id": "220",
"name": "BASKETBALL",
"dates": [
"2016-05-12T03:00:00.000Z"
"2016-05-12T04:00:00.000Z"
]
}
我的比赛班:
public class Competition{
private int id;
private String name;
private String[] dates;
// setters ... getters
}
我的 RowMapper 类:
public class RowMapper implements RowMapper
{
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
Competition competition = new Competition();
competition.setId(rs.getInt("id"));
competition.setName(rs.getString("name"));
competition. // How to fill dates
return competition;
}
}
数据选择功能:
private static final String SELECT_STMT =
" select * from competition INNER JOIN date ON
+ " competition.competition_id = date.competition_id"
+ " WHERE date(date.date_time) BETWEEN ? AND ?"
;
public List<Competition> findByOptionsAll(String dateFrom, String dateTo ){
List<Competition> competitions = jdbcTemplate.query(SELECT_STMT, new
RowMapper(), dateFrom, dateTo);
return competitions ;
}
【问题讨论】:
-
您想将日期保留为
String,还是使用Date?在 Java 应用程序中?在数据库中? -
不,在 DB 中日期是 timestampz,但我只用 date(...) 提取日期。我还是有更换的麻烦吗? ?通过适当的值。