须知:
本篇教程仅限功能性开发,不包括真实业务逻辑,非常适合初学者上手开发。
手写代码从前台贯串后台,没有花哨的界面和友好性的js脚本提示
由于功能性较多,目前只手写了添加和查询功能。
修改和删除功能后续上线
—
有疑问、或者又更简单、更优秀的方法请在下方评论区留言!感谢支持!
1.项目分析
网上书店: 使用所学的javaweb知识
邮件的发送
在线支付
添加分类
查询分类
添加图书
查询书籍
分页显示和分类名称获取
权限控制
功能思维导图
项目原型预览
1、项目前台预览
1.1前台首页展示:
1.2内容页展示:
1.3分类展示:
1.4购物车展示:
1.5个人用户注册展示:
1.6个人用户登录展示:
并自动跳转到主页:
1.7我的订单展示:
1.2、项目后台预览
1.2.1 后台首页展示
1.2.2 后台添加分类
1.2.3 后台查询分类
- 2.4 后台添加图书
1.2.5 后台查询图书
2.编程准备
|--- 开发环境:
Java环境 1.8 JDK
安装|--- 链接:https://pan.baidu.com/s/1FSeR4KYZwl2dg6btBkN80Q
提取码:ftav
复制这段内容后打开百度网盘手机App,操作更方便哦
数据库环境: 5.6版本
安装|--- 链接:https://pan.baidu.com/s/16uuaxxu12td5EczlwGVAvQ
提取码:21pv
复制这段内容后打开百度网盘手机App,操作更方便哦
|--- 采用IDE:
Java's IDE: MyEclipse2016
软件|--- 链接:https://pan.baidu.com/s/1LZWkopNa6mEAmFndQjDSSg
提取码:g1gt
复制这段内容后打开百度网盘手机App,操作更方便哦
破解|--- 链接:https://pan.baidu.com/s/1OtE2jFCPMrEWH9rIrPYmAw
提取码:heva
复制这段内容后打开百度网盘手机App,操作更方便哦
Navicat for Mysql
软件|--- 链接:https://pan.baidu.com/s/1YMXyfPWZqXrp7NOblhyGGg
提取码:qufy
复制这段内容后打开百度网盘手机App,操作更方便哦
-
项目编码:
-
程序编码设置为UTF-8
步骤:
|-- window - perferences - General - Workspace - Text File Encoding(右侧)
-
jsp 模板编码设置UTF-8
|-- window - perferences - MyEclipse - File and Editors - JSP - Encoding(UnicodeUtf-8) (右侧)
-
导入Jar和配置文件:
所需哪些jar包?
|----1. MySQL驱动
|----2. dbcp和pool
|----3. dbUtils
|----4. beanUtils和logging配置文件
|----1. jdbc.properties大家可以通过apache 网站,访问
http://commons.apache.org
进行下载http://commons.apache.org/
大家也可以通过百度云链接进行下载当前文章所需Jar包:
链接:https://pan.baidu.com/s/1h-geYZTe9v9BO4cdeDmR3w
提取码:3jfz
复制这段内容后打开百度网盘手机App,操作更方便哦 -
编程思想
程序使用分层思想及三层架构的MVC模式进行开发
-
-
程序编码
5.1实体类 - 分类管理
package com.ambow.entity; import java.io.Serializable; @SuppressWarnings("serial") public class Category implements Serializable { private String id;//UUID private String name; private String description; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }
5.2 数据访问层 dao
package com.ambow.dao; import java.util.List; import com.ambow.entity.Category; public interface CategoryDao { /** * 添加分类 * @param category */ void save(Category category); /** * 查询所有分类 * @return 没找到返回null */ List<Category> getAllCategories(); /** * 根据主键查找主键 * @param categoryId * @return 没找到返回null */ Category getCategoryById(String categoryId); }
实现类
package com.ambow.dao.impl; import java.sql.SQLException; import java.util.List; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import com.ambow.dao.CategoryDao; import com.ambow.entity.Category; import com.ambow.util.DbcpUtils; public class CategoryDaoImpl implements CategoryDao { QueryRunner queryRunner = new QueryRunner(DbcpUtils.getDataSource()); @Override public void save(Category category) { try { queryRunner.update("insert into categories(id,name,description) values (?,?,?)",category.getId(),category.getName(),category.getDescription()); } catch (SQLException e) { throw new RuntimeException(e); } } @Override public List<Category> getAllCategories() { try { return queryRunner.query("select * from categories", new BeanListHandler<Category>(Category.class)); } catch (SQLException e) { throw new RuntimeException(e); } } @Override public Category getCategoryById(String categoryId) { try { return queryRunner.query("select * from categories where id = ?", new BeanHandler<Category>(Category.class),categoryId); } catch (SQLException e) { throw new RuntimeException(e); } } }
5.3 工具类:数据源连接池DbcpUtil
package com.ambow.util; import java.io.InputStream; import java.sql.Connection; import java.sql.SQLException; import java.util.Properties; import javax.sql.DataSource; import org.apache.commons.dbcp2.BasicDataSourceFactory; public class DbcpUtils { public static DataSource dataSource; static { try { InputStream in = DbcpUtils.class.getClassLoader().getResourceAsStream("jdbc.properties"); Properties properties = new Properties(); properties.load(in); dataSource = BasicDataSourceFactory.createDataSource(properties); } catch (Exception e) { throw new RuntimeException(e); } } public static DataSource getDataSource() { return dataSource; } public static Connection getConnection() { try { return dataSource.getConnection(); } catch (SQLException e) { throw new RuntimeException(e); } } }
配置文件:
driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql:///webbookstore username=root password=sorry
5.4 业务逻辑服务层 service
package com.ambow.service; import java.util.List; import com.ambow.commons.Page; import com.ambow.entity.Book; import com.ambow.entity.Category; public interface BusinessService { /*-------------------图书分类逻辑--------------------*/ /** * 添加分类 * @param category */ void addCategory(Category category); /** * 查询所有分类 * @return 没找到返回null */ List<Category> findAllCategories(); /** * 根据主键查找主键 * @param categoryId * @return 没找到返回null */ Category findCategoryById(String categoryId); }
实现类
package com.ambow.service.impl; import java.util.List; import java.util.UUID; import com.ambow.commons.Page; import com.ambow.dao.BookDao; import com.ambow.dao.CategoryDao; import com.ambow.dao.impl.BookDaoImpl; import com.ambow.dao.impl.CategoryDaoImpl; import com.ambow.entity.Book; import com.ambow.entity.Category; import com.ambow.service.BusinessService; /** * 业务逻辑接口的实现类 * @author Mryang * */ public class BusinessServiceImpl implements BusinessService { CategoryDao categoryDao = new CategoryDaoImpl(); BookDao bookDao = new BookDaoImpl(); /*-------------------图书分类逻辑--------------------*/ @Override public void addCategory(Category category) { category.setId(UUID.randomUUID().toString()); categoryDao.save(category); } @Override public List<Category> findAllCategories() { return categoryDao.getAllCategories(); } @Override public Category findCategoryById(String categoryId) { return categoryDao.getCategoryById(categoryId); }
5.5 控制器 servlet
package com.ambow.controller; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.io.Reader; import java.lang.reflect.InvocationTargetException; import java.nio.channels.FileChannel; import java.util.List; import java.util.UUID; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileItemIterator; import org.apache.commons.fileupload.FileItemStream; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.fileupload.util.FileItemHeadersImpl; import org.apache.commons.fileupload.util.Streams; import org.apache.commons.io.FilenameUtils; import com.ambow.commons.Page; import com.ambow.entity.Book; import com.ambow.entity.Category; import com.ambow.service.BusinessService; import com.ambow.service.impl.BusinessServiceImpl; import com.ambow.util.FilePathUtil; import com.ambow.util.FillBeanUtils; public class ControlServlet extends HttpServlet { private static final long serialVersionUID = 1L; BusinessService service = new BusinessServiceImpl(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String op = request.getParameter("op"); if("addCategory".equals(op)){ addCategory(request, response); }else if("showAllCategories".equals(op)){ showAllCategories(request, response); }else if("showAllBooks".equals(op)){ showAllBooks(request, response); } else{ System.out.println("error"); } } /***************************图书分类**********************************/ /** * 查询所有分类 * @param request * @param response * @throws ServletException * @throws IOException */ public void showAllCategories(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List<Category> categoryList = service.findAllCategories(); request.setAttribute("categoryList", categoryList); request.getRequestDispatcher("/admin/listCategory.jsp").forward(request, response); } /** * 添加分类 * @param request * @param response * @throws ServletException * @throws IOException */ public void addCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Category category = FillBeanUtils.fillBean(request,Category.class); service.addCategory(category); request.setAttribute("msg", "保存成功"); request.getRequestDispatcher("/message.jsp").forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
5.6 工具类 封装对象的工具类:FillBeanUtils
package com.ambow.util; import javax.servlet.http.HttpServletRequest; import org.apache.commons.beanutils.BeanUtils; /** * 使用自定义泛型进行封装JavaBean * @author Mryang */ public class FillBeanUtils { public static <T> T fillBean(HttpServletRequest request, Class<T> clazz) { try { T bean = clazz.newInstance(); BeanUtils.copyProperties(bean, request.getParameterMap()); return bean; } catch (Exception e) { throw new RuntimeException(e); } } }
5.7 前台页面: jsp
注: 在WebRoot下创建文件夹 命名为admin,在admin下创建jsp:index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ include file="/admin/header.jsp"%> <br /> <h2>欢迎光临趣读书屋</h2> </body> </html>
在admin下创建jsp:header.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; pageContext.setAttribute("basePath", basePath); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>趣读书屋后台管理</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <link rel="stylesheet" type="text/css" href="${basePath}css/main.css"> </head> <body> <br /> <br /> <h1>趣读书屋后台管理</h1> <br /> <a href="${basePath}admin/addCategory.jsp">添加分类</a> <a href="${basePath}servlet/ControlServlet?op=showAllCategories">查询分类</a> <a href="">添加书籍</a> <a href="">查询书籍</a> <a href="">待处理订单</a> <a href="">已处理订单</a> <br /> <hr/> </body> </html>
addCategory.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ include file="/admin/header.jsp"%> <form action="${basePath}/servlet/ControlServlet?op=addCategory" method="post"> <table border="1px" width="438px"> <tr> <td>添加分类</td> <td><input type="text" name="name"></td> </tr> <tr> <td>查询分类</td> <td><textarea rows="3" cols="38" name="description"></textarea> </td> </tr> <tr> <td colspan="2"><input type="submit" value="保 存"></td> </tr> </table> </form> </body> </html>
查询分页:listCategory.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ include file="/admin/header.jsp"%> <br /> <table border="1" width="438"> <tr> <th>选择</th> <th>分类名称</th> <th>分类描述</th> <th>操作</th> </tr> <c:forEach items="${categoryList}" var="cl" varStatus="vcl"> <tr class="${vcl.index%2==0?'odd':'even' }"> <td><input type="checkbox" name="ids" value="${cl.id}"> </td> <td>${cl.name }</td> <td>${cl.description }</td> <td> <a href="javascript:alert('略')">修改</a> <a href="javascript:alert('略')">删除</a> </td> </tr> </c:forEach> </table> <br /> </body> </html>
注: 在WebRoot下创建jsp:message.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ include file="/admin/header.jsp" %> <h1> ${msg } </h1> </body> </html>
5.8 在WebRoot下创建一个文件夹,命名为css,在css下创建一个文件,命名为main.css
@CHARSET "UTF-8"; body{ font-size: 12px; text-align: center; } table{ font-size: 12px; margin: 0 auto; } .odd{ background-color: f3c3f3; } .even{ background-color: c3f3c3; }
-
运行项目
-
效果
作者: 杨校
出处: https://blog.csdn.net/kese7952
分享是快乐的,也见证了个人成长历程,文章大多都是工作经验总结以及平时学习积累,基于自身认知不足之处在所难免,也请大家指正,共同进步。
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出, 如有问题, 可邮件(397583050@qq.com)咨询