项目中用到了java.util.Date属性的Model,但是在RowMapper中ResultSet获取getDate时,时间的时分秒都为0,为了解决这个0的问题,
rs应该用getTimestamp()而不用getDate()即可。
如下所示:
1.领域类
public class StudySchedule {
private int id;
private long studyTime;//学习时间
private String courseName;//课程名
private String userId;//用户登录id
private int coursePage;//总页数
private String courseView;//已学习页数
private Date lastStudyTime;//最后学习时间
public Date getLastStudyTime() {
return lastStudyTime;
}
public void setLastStudyTime(Date lastStudyTime) {
this.lastStudyTime = lastStudyTime;
}
……
}
2.对应的RowMapper类
public class StudyScheduleRowMapper implements RowMapper<StudySchedule>{
public StudySchedule mapRow(ResultSet rs, int rowNum) throws SQLException {
StudySchedule schedule=new StudySchedule();
schedule.setId(rs.getInt("id"));
schedule.setCourseName(rs.getString("course_name"));
schedule.setStudyTime(rs.getLong("study_time"));
schedule.setUserId(rs.getString("user_id"));
schedule.setCoursePage(rs.getInt("course_page"));
schedule.setCourseView(rs.getString("course_view"));
schedule.setCourseId(rs.getInt("course_id"));
//这种方式获取的时分秒是0
// schedule.setLastStudyTime(rs.getDate("last_study_time"));
//时间正常显示
schedule.setLastStudyTime(rs.getTimestamp("last_study_time"));
return schedule;
}
}
OK!