public class Record {
public Integer rid;
public User user;
public Book book;
public LocalDateTime borrowedAt;
public boolean returned;
public LocalDateTime returnedAt;
public Record(){
this.rid = rid;
this.user = user;
this.book = book;
this.borrowedAt = borrowedAt;
this.returned = returned;
this.returnedAt = returnedAt;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Record record = (Record) o;
return returned == record.returned && rid.equals(record.rid) && user.equals(record.user) && book.equals(record.book) && borrowedAt.equals(record.borrowedAt) && returnedAt.equals(record.returnedAt);
}
@Override
public int hashCode() {
return Objects.hash(rid, user, book, borrowedAt, returned, returnedAt);
}
@Override
public String toString() {
return "Record{" +
"rid=" + rid +
", user=" + user +
", book=" + book +
", borrowedAt=" + borrowedAt +
", returned=" + returned +
", returnedAt=" + returnedAt +
'}';
}
}
为什么要重写equals()方法和hashcode()方法呢?
先根据hashcode值在集合中分类,再根据equals()方法判断对象是否相同
public class Book {
public Integer bid;
public String name;
public Integer totalCount;
public Integer currentCount;
public Book() {}
public Book(Integer bid, String name, Integer totalCount, Integer currentCount) {
this.bid = bid;
this.name = name;
this.totalCount = totalCount;
this.currentCount = currentCount;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Book book = (Book) o;
return bid.equals(book.bid);
}
@Override
public int hashCode() {
return Objects.hash(bid);
}
@Override
public String toString() {
return "Book{" +
"bid=" + bid +
", name='" + name + '\'' +
", totalCount=" + totalCount +
", currentCount=" + currentCount +
'}';
}
}
public class User{
public Integer uid;
public String username;
public User(){
this.uid = uid;
this.username = username;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return uid.equals(user.uid) && username.equals(user.username);
}
@Override
public int hashCode() {
return Objects.hash(uid, username);
}
@Override
public String toString() {
return "User{" +
"uid=" + uid +
", username='" + username + '\'' +
'}';
}
}
public class Library {
public Record borrowBook(int uid,int bid)throws SQLException{
MysqlDataSource ds = new MysqlDataSource();
ds.setServerName("127.0.0.1");
ds.setPort(3306);
ds.setUser("root");
ds.setPassword("123456");
ds.setDatabaseName("db_11_26");
ds.setCharacterEncoding("utf8");
ds.setServerTimezone("Asia/Shanghai");
ds.setServerTimezone("Asia/Shanghai");
ds.setUseSSL(false);
Record record = new Record();
User user = new User();
Book book = new Book();
try (Connection c = ds.getConnection()) {
String sql = String.format("SELECT current_count FROM books WHERE bid = %d", bid);
System.out.println("DEBUG: " + sql);
try (PreparedStatement ps = c.prepareStatement(sql)) {
try (ResultSet rs = ps.executeQuery()) {
rs.next();
int currentCount = rs.getInt("current_count");
if (currentCount == 0) {
return null;
}
}
}
sql = String.format("UPDATE books SET current_count = current_count - 1 WHERE bid = %d", bid);
System.out.println("DEBUG: " + sql);
try (PreparedStatement ps = c.prepareStatement(sql)) {
ps.executeUpdate();
}
LocalDateTime now = LocalDateTime.now();
String nowStr = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
sql = String.format("INSERT INTO records (uid, bid, borrowed_at) VALUES (%d, %d, '%s')", uid, bid, nowStr);
System.out.println("DEBUG: " + sql);
try (PreparedStatement ps = c.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
ps.executeUpdate();
try (ResultSet rs = ps.getGeneratedKeys()) {
rs.next();
record.rid = rs.getInt(1);
}
}
sql = String.format("select * from users where uid = %d", uid);
System.out.println("DEBUG: " + sql);
try (PreparedStatement ps = c.prepareStatement(sql)) {
try (ResultSet rs = ps.executeQuery()) {
rs.next();
user.uid = uid;
user.username = rs.getString("username");
}
}
sql = String.format("select * from books where bid = %d", bid);
System.out.println("DEBUG: " + sql);
try (PreparedStatement ps = c.prepareStatement(sql)) {
try (ResultSet rs = ps.executeQuery()) {
rs.next();
book.bid = bid;
book.name = rs.getString("name");
book.totalCount = rs.getInt("total_count");
book.currentCount = rs.getInt("current_count");
}
}
record.user = user;
record.book = book;
record.borrowedAt = now;
record.returned = false;
record.returnedAt = null;
}
return record;
}
public static void main(String[] args) throws SQLException {
Library library = new Library();
Record record = library.borrowBook(2, 1);
System.out.println(record);
}
}