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 () { } @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 { 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 () { } @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.java
和Book.java
两个Java Bean
对象,并使用了MyBatis
的注解映射了对应的SQL
语句