12.5 持久层功能实现

12.5 持久层功能实现

PO类

User.java

/MyBookApp/src/com/domain/User.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.domain;
public class User {
private Integer id;
private String loginname;
private String password;
private String username;
private String phone;
private String address;
public User() {
}
// 此处省略getter和setter方法,请自己补上
@Override
public String toString() {
return "User [id=" + id + ", loginname=" + loginname + ", password=" + password + ", username=" + username
+ ", phone=" + phone + ", address=" + address + "]";
}
}

Book.java

/MyBookApp/src/com/domain/Book.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.domain;
import java.util.Date;
public class Book {
// id
private Integer id;
// 书名
private String name;
// 作者
private String author;
// 出版日期
private Date publicationdate;
// 出版社
private String publication;
// 价格
private Double price;
// 封面图片
private String image;
// 详细描述
private String remark;
public Book() {
}
// 此处省略getter和setter方法,请自己补上
@Override
public String toString() {
return "Book [id=" + id + ", name=" + name + ", author=" + author + ", publicationdate=" + publicationdate
+ ", publication=" + publication + ", price=" + price + ", image=" + image + ", remark=" + remark + "]";
}
}

Mapper接口

UserMapper.java

/MyBookApp/src/com/mapper/UserMapper.java
1
2
3
4
5
6
7
8
9
10
package com.mapper;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import com.domain.User;

public interface UserMapper {
@Select("select * from tb_user where loginname=#{loginname} and password=#{password}")
User selectUserByLoginnamePassword(@Param("loginname") String loginname, @Param("password") String password);
}

BookMapper.java

/MyBookApp/src/com/mapper/BookMapper.java
1
2
3
4
5
6
7
8
9
10
package com.mapper;

import java.util.List;
import org.apache.ibatis.annotations.Select;
import com.domain.Book;

public interface BookMapper {
@Select("select * from tb_book")
List<Book> selectAllBooks();
}

持久层包括和数据库表映射的User.javaBook.java两个Java Bean对象,并使用了MyBatis的注解映射了对应的SQL语句