须知:
本篇教程仅限功能性开发,不包括真实业务逻辑,非常适合初学者上手开发。
手写代码从前台贯串后台,没有花哨的界面和友好性的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 后台查询分类
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模式进行开发
![](https://img.alicdn.com/imgextra/i2/1850677525/TB21LmqvNuTBuNkHFNRXXc9qpXa_!!1850677525.png)
```java
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
```java
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 getAllCategories();
/**
* 根据主键查找主键
* @param categoryId
* @return 没找到返回null
*/
Category getCategoryById(String categoryId);
}
```
DAO实现类
```java
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 getAllCategories() {
try {
return queryRunner.query("select * from categories", new BeanListHandler(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.class),categoryId);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
```
5.3 工具类:数据源连接池DbcpUtil
```java
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);
}
}
}
```
配置文件:
```xml
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///webbookstore
username=root
password=sorry
```
5.4 业务逻辑服务层 service
```java
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 findAllCategories();
/**
* 根据主键查找主键
* @param categoryId
* @return 没找到返回null
*/
Category findCategoryById(String categoryId);
}
```
Service 实现类
```java
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 findAllCategories() {
return categoryDao.getAllCategories();
}
@Override
public Category findCategoryById(String categoryId) {
return categoryDao.getCategoryById(categoryId);
}
```
5.5 控制器 servlet
```java
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 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
```java
package com.ambow.util;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.beanutils.BeanUtils;
/**
* 使用自定义泛型进行封装JavaBean
* @author Mryang
*/
public class FillBeanUtils {
public static T fillBean(HttpServletRequest request, Class 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
```jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/admin/header.jsp"%>
欢迎光临趣读书屋