我在 Spring 处理一些dao文件,我的beanpropertyrowmapper有一些问题。它正确Map了几个属性,但对于一个属性,它返回null。为什么会这样?下面是我的读条目dao方法:
@Override
public Optional<Activity> read(Long id) {
try {
return Optional.ofNullable(jdbcTemplate.queryForObject("SELECT * FROM activities WHERE id=?", new BeanPropertyRowMapper<>(Activity.class), id));
} catch (EmptyResultDataAccessException e) {
return Optional.empty();
}
}
这是我的模型:
public class Activity implements Entity<Long> {
private Long id;
private Long groupId;
private Long classRoomId;
private Long lessonNumber;
private Long courseId;
public Activity() {
}
public Activity(Long id, Long groupId, Long classRoomId, Long lessonNumber, Long courseId) {
this.id = id;
this.groupId = groupId;
this.classRoomId = classRoomId;
this.lessonNumber = lessonNumber;
this.courseId = courseId;
}
public Activity(Long groupId, Long classRoomId, Long lessonNumber, Long courseId) {
this.groupId = groupId;
this.classRoomId = classRoomId;
this.lessonNumber = lessonNumber;
this.courseId = courseId;
}
public Long getGroupId() {
return groupId;
}
public void setGroupId(Long groupId) {
this.groupId = groupId;
}
public Long getClassRoomId() {
return classRoomId;
}
public void setClassRoomId(Long classRoomId) {
this.classRoomId = classRoomId;
}
public Long getLessonNumber() {
return lessonNumber;
}
public void setLessonNumber(Long lessonNumber) {
this.lessonNumber = lessonNumber;
}
public Long getCourseId() {
return courseId;
}
public void setCourseId(Long courseId) {
this.courseId = courseId;
}
@Override
public Long getId() {
return id;
}
@Override
public void setId(Long id) {
this.id = id;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Activity activity = (Activity) o;
if (groupId != null ? !groupId.equals(activity.groupId) : activity.groupId != null) return false;
if (classRoomId != null ? !classRoomId.equals(activity.classRoomId) : activity.classRoomId != null)
return false;
if (lessonNumber != null ? !lessonNumber.equals(activity.lessonNumber) : activity.lessonNumber != null)
return false;
return courseId != null ? courseId.equals(activity.courseId) : activity.courseId == null;
}
@Override
public int hashCode() {
int result = groupId != null ? groupId.hashCode() : 0;
result = 31 * result + (classRoomId != null ? classRoomId.hashCode() : 0);
result = 31 * result + (lessonNumber != null ? lessonNumber.hashCode() : 0);
result = 31 * result + (courseId != null ? courseId.hashCode() : 0);
return result;
}
@Override
public String toString() {
return String.format("id is %s, groupId is %s, classRoomId is %s, lessonNumber is %s, courseId is %s",
this.id, this.groupId, this.classRoomId, this.lessonNumber, this.courseId);
}
}
下面是我的junit测试方法:
@Test
void read() throws Exception {
Activity expected = new Activity(100L, 100L,1L,100L);
Long id = activityDao.save(expected).getId();
if(activityDao.read(id).isPresent()){
Activity actual = activityDao.read(id).get();
assertEquals(expected, actual);
}else {
fail("Not found");
}
}
下面是我的junit测试方法返回的结果:
org.opentest4j.assertionfailederror:预期:id为null,groupid为100,教室id为100,lessonnumber为1,courseid为100实际:id为1,groupid为100,教室id为null,lessonnumber为1,courseid为100
编辑:这是我的数据库表:在这里输入图像描述
编辑:稍微更改了dao方法,以便属性和表列匹配:
@Override
public Optional<Activity> read(Long id) {
try {
return Optional.ofNullable(jdbcTemplate.queryForObject("SELECT a.id as id, a.group_id as groupId, a.classroom_id as classRoomId, a.lessonnumber as lessonNumber," +
" a.course_id as courseId FROM activities a WHERE id=?", new BeanPropertyRowMapper<>(Activity.class), id));
} catch (EmptyResultDataAccessException e) {
return Optional.empty();
}
}
然而,这是行不通的。
暂无答案!
目前还没有任何答案,快来回答吧!