所以我试图将一个硬编码的成员插入到我的会员表中。我正在使用Spring Boot& Eclipse上的JDBC。
这是我的schema.sql:
CREATE TABLE Members
(
ID int not null,
LastName varchar(255) not null,
FirstName varchar(255) not null,
PhoneNumber integer not null,
created timestamp not null,
primary key(ID)
);
INSERT INTO MEMBERS (ID, LASTNAME, FIRSTNAME,PHONENUMBER, CREATED)
VALUES(1001, 'Max', 'Mad', 0547547547, sysdate());
我在DAO类中有一个findAll方法:
public List<Member> findAll(){
return jtemp.query("select * from members", new BeanPropertyRowMapper<Member>(Member.class));
}
当我运行它时,它会返回:
[Member [id=0, firstName=Mad, lastName=Max, phoneNumber=547547547, created=2017-10-31 18:57:21.606]]
正如您所见,由于某种原因未插入ID。
我的会员班是这样的:
public class Member {
private long id;
private String firstName;
private String lastName;
private long phoneNumber;
private Date created;
public Member(long id, String firstName, String lastName, long phoneNumber, Date created) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.phoneNumber = phoneNumber;
this.created = created;
}
public long getUserId() {
return id;
}
public void setUserId(long userId) {
this.id = userId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public long getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(long phoneNumber) {
this.phoneNumber = phoneNumber;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
@Override
public String toString() {
return "Member [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", phoneNumber="
+ phoneNumber + ", created=" + created + "]";
}
}
我该如何解决? 我试图插入不同的数字,但我总是在日志中得到零。
由于
答案 0 :(得分:0)
查询的列名必须与目标对象中的setter匹配。
您的查询有一个名为id
的列,但没有setId
方法。您应该将setUserId
重命名为setId
,或者在查询中将别名user_id
重命名为id
列。
来自BeanPropertyRowMapper documentation:
根据从结果集元数据获取的列名与相应属性的公共setter匹配来映射列值。名称可以直接匹配,也可以使用&#34; camel&#34;将名称与下划线分隔成相同的名称。情况下。