四、案例
-
采用三层架构设计程序架构
表现层: MVC设计模式,主要用于与用户交互
业务层 : 处理具体业务
数据访问层 : 对数据库进行操作
-
数据库: MySQL关系型数据库
-
JDBC: 操作数据库
功能需求:
图书管理系统 增、删、改、查功能
定义存放实体类的包 (数据bean)
com.xxx.pojo | com.xxx.beans | com.xxx.domain
package com.xxx.pojo;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Book {
private int bookId;
private String bookName;
private String author;
private int price;
private Date pubDate;
private String publisher;
private String pubDateStr;
public int getBookId() {
return bookId;
}
public void setBookId(int bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public Date getPubDate() {
return pubDate;
}
public void setPubDate(Date pubDate) {
this.pubDate = pubDate;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getPubDateStr() {
return pubDateStr;
}
public void setPubDateStr(String pubDateStr) {
this.pubDateStr = pubDateStr;
}
@Override
public String toString() {
return "Book{" +
"bookId=" + bookId +
", bookName='" + bookName + '\'' +
", author='" + author + '\'' +
", price=" + price +
", pubDate=" + pubDate +
", publisher='" + publisher + '\'' +
", pubDateStr='" + pubDateStr + '\'' +
'}';
}
}
分页
package com.xxx.pojo;
import java.util.List;
public class PageInfo {
private int curPage = 1; // 记录当前页面
private int pageSize = 5; // 记录每页显示的记录数
private List<Book> books; // 记录当前页的书的集合 数据库查询出来
private int count; // 书的总记录数 数据库查出来
private int total; // 记录总页数 计算出来
public int getCurPage() {
return curPage;
}
public void setCurPage(int curPage) {
this.curPage = curPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public List<Book> getBooks() {
return books;
}
public void setBooks(List<Book> books) {
this.books = books;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getTotal() {
total = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
return total;
}
public void setTotal(int total) {
this.total = total;
}
@Override
public String toString() {
return "PageInfo{" +
"curPage=" + curPage +
", pageSize=" + pageSize +
", books=" + books +
", count=" + count +
", total=" + getTotal() +
'}';
}
}
定义存放servlet的包(控制器)
com.xxx.web.servlet | com.xxx.web.controller
package com.xxx.servlet;
import com.xxx.pojo.Book;
import com.xxx.response.ResponseResult;
import com.xxx.service.IBookService;
import com.xxx.service.impl.BookServiceImpl;
import com.xxx.utils.StringDateConverter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
@WebServlet("/book/add")
public class BookAddServlet extends HttpServlet {
IBookService service = new BookServiceImpl();
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 需要接受前端提交过来的数据
// 1.设置请求正文的字符编码
request.setCharacterEncoding("utf-8");
// 2.获取请求参数
String bookName = request.getParameter("bookName");
String author = request.getParameter("author");
String priceStr = request.getParameter("price");
String pubDateStr = request.getParameter("pubDate");
String publisher = request.getParameter("publisher");
// 3.后台验证
if(bookName == null || bookName.trim().length() == 0) {
ResponseResult result = new ResponseResult(99999, "非法的请求参数!");