通过一个案例,知道为什么,对象里面的东西是会随着后续赋值而改变的.
while(rs.next()) {
Book book=new Book();
book.setId(rs.getInt(1));
book.setName(rs.getString(2));
book.setAuthor(rs.getString(3));
book.setPrice(rs.getFloat(4));
book.setPublisher(rs.getString(5));
books.add(book);
}
return books;
而如果代码是这样的,结果就是另外一个样子
Book book=new Book();
while(rs.next()) {
book.setId(rs.getInt(1));
book.setName(rs.getString(2));
book.setAuthor(rs.getString(3));
book.setPrice(rs.getFloat(4));
book.setPublisher(rs.getString(5));
books.add(book);
}
return books;