需要源码小伙伴可以在我的微信小程序:”MINIIT资源库“获取,作者承诺免费
介绍一下这个简单的项目包含了书籍展示、添加书籍以及删除书籍功能,虽然项目很简单,但是作为小白的我很想和大家分享互动,欢迎大家参考交流大神指教
下面展示我的主要功能代码,Dao模式:
这是Dao层的实现类:
package com.BookManage.Dao.impl;
import com.BookManage.Dao.BaseDao;
import com.BookManage.Dao.BookDao;
import com.BookManage.Entity.Book;
import com.BookManage.Util.ConnectionUtil;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class BookDaoImpl extends BaseDao implements BookDao {
/**
* 通过有参构造传入connection
*
* @param connection
*/
public BookDaoImpl(Connection connection) {
super(connection);
}
@Override
public List getBook() throws Exception {
//先创建sql语句
String sql="select * from bookmanage";
//创建结果集对象
ResultSet resultSet=null;
try{
resultSet=doQuery(sql);
//创建集合接收封装结果集的Book对象
List bookList=new ArrayList<>();
//循环读取resultSet,实例一个Book对象接收
while (resultSet.next()){
Book book=new Book();
book.setB_id(resultSet.getInt("b_id"));
book.setB_name(resultSet.getString("b_name"));
book.setB_author(resultSet.getString("b_author"));
book.setB_time(resultSet.getDate("b_time"));
book.setB_type(resultSet.getInt("b_type"));
bookList.add(book);
}
return bookList;
}catch (Exception e){
throw e;
}finally {
ConnectionUtil.closeAll(null,null,resultSet);
}
}
@Override
public int addBook(Book book) throws Exception {
String sql="INSERT INTO `bookmanage` (`b_name`,`b_author`,`b_time`,`b_type`) VALUES(?,?,?,?)";
//创建集合接收前端数据
Object [] params={book.getB_name(),book.getB_author(),book.getB_time(),book.getB_type()};
try {
return doUpdate(sql,params);
} catch (SQLException e) {
throw e;
}
}
@Override
public int delBookById(int bid) throws SQLException {
String sql ="delete from bookmanage where b_id=?";
int result=0;
try {
Object[] params={bid};
result=this.doUpdate(sql,params);
} catch (SQLException e) {
e.printStackTrace();
throw e;
}
return result;
}
}
这是Service层的实现类:
package com.BookManage.Service.impl;
import com.BookManage.Dao.BookDao;
import com.BookManage.Dao.impl.BookDaoImpl;
import com.BookManage.Entity.Book;
import com.BookManage.Service.BookService;
import com.BookManage.Util.ConnectionUtil;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
public class BookServiceImpl implements BookService {
@Override
public List getBook() {
//获得数据库连接并传入BookDaoImpl
Connection connection=ConnectionUtil.getConnection();
BookDao bookDao=new BookDaoImpl(connection);
try {
return bookDao.getBook();
} catch (Exception e) {
e.printStackTrace();
}finally {
//关闭数据库连接
ConnectionUtil.closeAll(connection,null,null);
}
return null;
}
@Override
public boolean addBook(Book book) {
Connection connection=ConnectionUtil.getConnection();
BookDao bookDao=new BookDaoImpl(connection);
try {
return bookDao.addBook(book)>0;
} catch (Exception e) {
e.printStackTrace();
}finally {
ConnectionUtil.closeAll(connection,null,null);
}
return false;
}
@Override
public boolean delBook(int bid) throws SQLException {
Connection connection=null;
try {
connection=ConnectionUtil.getConnection();
BookDao bd=new BookDaoImpl(connection);
return bd.delBookById(bid)>0;
} catch (SQLException e) {
e.printStackTrace();
throw e;
}finally {
ConnectionUtil.closeAll(connection,null,null);
}
}
}
这是与实现与前端交互的Servlet:
package com.BookManage.Servlet;
import com.BookManage.Entity.Book;
import com.BookManage.Service.BookService;
import com.BookManage.Service.impl.BookServiceImpl;
import com.alibaba.fastjson.JSON;
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.Console;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@WebServlet(name = "BookServlet",urlPatterns = "/BookServlet")
public class BookServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//接收操作请求
String opr = req.getParameter("opr");
resp.setContentType("application/json;charset=utf-8");
BookService bookService=new BookServiceImpl();
if ("all".equals(opr)) {
List book = bookService.getBook();
String json= JSON.toJSONStringWithDateFormat(book,"yyyy-MM-dd");
resp.getWriter().print(json);
}else if("add".equals(opr)){
String bname=req.getParameter("bname");
String bauthor=req.getParameter("bauthor");
String btime=req.getParameter("btime");
String btype=req.getParameter("btype");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date date=null;
try {
date=formatter.parse(btime);
Book book=new Book(bname,bauthor,date,Integer.parseInt(btype));
boolean result = bookService.addBook(book);
Map stringHashMap = new HashMap();
stringHashMap.put("result",result);
String json=JSON.toJSONString(stringHashMap);
resp.getWriter().print(json);
} catch (Exception e) {
e.printStackTrace();
}
}else if("deleteBook".equals(opr)){
String bid=req.getParameter("bid");
System.out.print(bid);
try {
boolean result=bookService.delBook(Integer.parseInt(bid));
Map stringHashMap = new HashMap();
stringHashMap.put("result",result);
String json=JSON.toJSONString(stringHashMap);
resp.getWriter().print(json);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
前端用JSON实现前后端交互:
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2021/7/4
Time: 11:08
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
$Title$
图书信息
图书名称
图书作者
购买时间
图书分类
操作
新增图书信息