0%

12.5 持久层功能实现

12.5 持久层功能实现

User.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
package org.fkit.domain;
import java.io.Serializable;
public class User implements Serializable
{
private static final long serialVersionUID = 1L;
private Integer id; // id
private String loginname; // 登录名
private String password; // 密码
private String username; // 用户名
private String phone; // 电话
private String address; // 地址
public User()
{
super();
// TODO Auto-generated constructor stub
}
// 此处省略getter和setter方法,请自己补上
@Override
public String toString()
{
return "User [id=" + id + ", loginname=" + loginname + ", password="
+ password + ", username=" + username + ", phone=" + phone
+ ", address=" + address + "]";
}
}

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
29
30
31
32
33
34
35
36
37
38
39
package org.fkit.domain;
import java.io.Serializable;
import java.util.Date;
// CREATE TABLE tb_book (
// id INT(11) PRIMARY KEY AUTO_INCREMENT,
// name VARCHAR(54),
// author VARCHAR (54),
// publicationdate DATE ,
// publication VARCHAR (150),
// price DOUBLE ,
// image VARCHAR (54),
// remark VARCHAR (600)
// );
public class Book implements Serializable
{
private static final long serialVersionUID = 1L;
private Integer id; // id
private String name; // 书名
private String author; // 作者
private String publication; // 出版社
private Date publicationdate; // 出版日期
private Double price; // 价格
private String image; // 封面图片
private String remark; // 详细描述
public Book()
{
super();
// TODO Auto-generated constructor stub
}
// 此处省略getter和setter方法,请自己补上
@Override
public String toString()
{
return "Book [id=" + id + ", name=" + name + ", author=" + author
+ ", publication=" + publication + ", publicationdate="
+ publicationdate + ", price=" + price + ", image=" + image
+ ", remark=" + remark + "]";
}
}

UserMapper.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package org.fkit.mapper;

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

/**
* UserMapper接口
*/
public interface UserMapper
{
/**
* 根据登录名和密码查询用户
* @param String loginname
* @param String password
* @return 找到返回User对象,没有找到返回null
*/
@Select("select * from tb_user where loginname = #{loginname} and password = #{password}")
User findWithLoginnameAndPassword(
@Param("loginname") String loginname,
@Param("password") String password
);
}

BookMapper.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package org.fkit.mapper;

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

/**
* BookMapper接口
*/
public interface BookMapper
{
/**
* 查询所有图书
* @return 图书对象集合
*/
@Select(" select * from tb_book ")
List<Book> findAll();
}

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

原文链接: 12.5 持久层功能实现