我需要在 Spring Boot Web 应用程序中从数据库中执行按日期选择。到目前为止,我拥有的是体育比赛列表以及相应的信息。
问题:我无法弄清楚我的选择查询如何将我的字符串类型(dateFrom = '2017-05-02' 和 dateTo = '2017-05-06')转换为日期,如 '2017-02-12' ?
Alos 如何在某些具有多个日期的竞赛中用多个然后一个日期填充我的 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"
]
}
我的比赛类(class):
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 formatter :
private static final String DATE_FORMAT = "dd-MM-yy";
// parsing date; Note you should handle ParseException
java.util.Date date = new SimpleDateFormat(DATE_FORMAT).parse(dateAsString);
// converting date to string
String dateAsString = new SimpleDateFormat(DATE_FORMAT).format(date);
请注意,SimpleDateFormat 不是线程安全的,因此最好使用 static final String DATE_FORMAT
而不是 static final DateFormatter
在某些情况下,转换日期和时间很棘手(时区呢?java.util.Date vs joda.time vs LocalDate from Java 8),但超出了范围。我建议如果可能的话使用 LocalDate,因为它是一种没有旧问题的新方法。
映射
您的数据库中有两个实体(Competition 和 Date-of-competition),而域模型中只有一个类 Competition
。最有可能的是,稍后您会想要向比赛日期添加其他信息( boolean 值完成、取消、得分等),因此最好立即创建 CompetitionInstance
类。
由于您具有一对多关系,因此您必须编写一些额外的内容来映射对象。通常这就是像 Hibernate 这样的 ORM 代替你做的事情。首先,在 sql 语句中添加“GROUP BY racing_id”。 然后使用 RowSetExtractor 而不是 RowMapper,如所述 here :
private static final class CompetitionMapExtractor implements ResultSetExtractor<List<Competition>> {
@Override
public List<Competition> extractData(ResultSet rs) throws SQLException {
List<Competition> result = new ArrayList<>(rs.getCount());
int previousCompetitionId = NEVER_EXIST; // normally -1 is good enough
while (rs.next()) {
// we have some dates with the same competition_id
// dates are grouped thanks to GROUP BY clause
if ( rs.getInt("id") != previousCompetitionId) {
Competition currentCompetition = new Competition(rs.getInt("id"),
rs.getString("name");
/* I prefer constructor initializers "o = new O(propertyValue)"
instead of snippet "o = new O(); o.setProperty(value)"
*/
result.add(currentCompetition);
previousCompetitionId = currentCompetition.getid();
} else {
currentCompetition.addDate(new CompetitionInstance(rs.getString("date")));
}
}
return result;
}
我认为Competition
有方法public void addDate(String date)
,它只是将一个新的CompetitionInstance添加到列表中。
更新:
1. DB 和 MapExtractor 中的列名称不同。我更喜欢更改查询:
SELECT c.id, c.name, d.date_time as date
from competition c
INNER JOIN date d ON c.competition_id = d.competition_id
WHERE date(d.date_time) BETWEEN ? AND ?"
2.我无法重现您遇到的日期问题。您很可能混淆了 java.util.Date
、java.sql.Date
和 java.sql.Timestamp
- 这是一个常见的错误。有很多answers可能您已经可以找到 one的them有用。
关于java - 使用 JDBC 模板从多个表中执行数据选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44672501/