文章目录
- Java课程设计项目-servlet+jsp美食系统
- 一、项目介绍
- 二、技术介绍
- 2.1 环境需要
- 2.2 技术栈
- 环境需要
- 三、功能实现
- 3.1登录注册
- 3.2首页菜品展示、轮播图
- 3.3美食菜品分类、查询
- 3.4作品动态、个人简介、菜品收藏
- 3.5创建菜谱、添加步骤
- 四、系统代码展示
- 4.1项目架构(MVC)
- 4.2部分代码展示
- 获取源码
Java课程设计项目-servlet+jsp美食系统
一、项目介绍
项目功能包含:
- 用户登录、注册
- 首页菜品展示、轮播图
- 美食菜品分类、查询
- 作品动态、个人简介、菜品收藏
- 创建菜谱、添加步骤
二、技术介绍
2.1 环境需要
- 运行环境:最好是java jdk 1.8,我们在这个平台上运行的。其他版本理论上也可以。
- IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA;
- tomcat环境:Tomcat 7.x,8.x,9.x版本均可
- 硬件环境:windows 7/8/10 1G内存以上;或者 Mac OS;
- 数据库:MySql 5.7版本;
- 是否Maven项目:否;
2.2 技术栈
环境需要
- Serverlet+JSP+CSS+JavaScript+mysql
三、功能实现
3.1登录注册
3.2首页菜品展示、轮播图
3.3美食菜品分类、查询
3.4作品动态、个人简介、菜品收藏
3.5创建菜谱、添加步骤
四、系统代码展示
4.1项目架构(MVC)
4.2部分代码展示
RecipeServiceImpl
package yjf.psyd.service.impl;import java.io.File;
import java.util.List;import yjf.psyd.bean.Recipe;
import yjf.psyd.bean.RecipeStep;
import yjf.psyd.bean.User;
import yjf.psyd.dao.RecipeDao;
import yjf.psyd.dao.impl.RecipeDaoImpl;
import yjf.psyd.service.RecipeService;public class RecipeServiceImpl implements RecipeService {// 声明Dao层对象,多态RecipeDao rd = new RecipeDaoImpl();// 创建菜谱@Overridepublic int createRecipeService(String createDate, String title, String info, String material,List<String> stepInfos, List<String> categorys, String coverFilePath, List<String> stepFilesPath, User user) {return rd.createRecipeDao(createDate, title, info, material, stepInfos, categorys, coverFilePath, stepFilesPath,user);}// 菜谱详情@Overridepublic Recipe recipeDetailService(String recipeId,User user) {return rd.recipeDetailDao(recipeId,user);}// 菜谱步骤详情@Overridepublic RecipeStep recipeStepDetailService(String recipeId) {return rd.recipeStepDetailDao(recipeId);}// 删除菜谱@Overridepublic int DeleteRecipeService(String recipeId) {return rd.DeleteRecipeDao(recipeId);}// 插入收藏菜谱@Overridepublic int insertCollectionRecipe(String recipeId,User user) {return rd.insertCollectionRecipeDao(recipeId,user);}// 删除收藏菜谱@Overridepublic int deleteCollectionRecipe(String recipeId, User user) {return rd.deleteCollectionRecipeDao(recipeId,user);}}
PageDaoImpl
package yjf.psyd.dao.impl;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;import yjf.psyd.bean.CategoryPage;
import yjf.psyd.bean.CategoryPageDetail;
import yjf.psyd.bean.HomePage;
import yjf.psyd.bean.HomePageDetail;
import yjf.psyd.bean.Page;
import yjf.psyd.bean.PageDetail;
import yjf.psyd.bean.SearchPage;
import yjf.psyd.bean.SearchPageDetail;
import yjf.psyd.bean.User;
import yjf.psyd.dao.PageDao;
import yjf.psyd.util.DbConnection;public class PageDaoImpl implements PageDao {// 请求首页PopRecipe数据@Overridepublic Page indexPopRecipeDao(int index) {// 声明jdbc变量Connection conn = null;PreparedStatement ps = null;ResultSet rs = null;// 声明对象、变量Page p = new Page();int count = -1;List<PageDetail> pageDetail = new ArrayList<>();try {// 获取链接conn = DbConnection.getConnection();// 创建sql命令String sql = "select count(1) from recipe";String sql1 = "SELECT * FROM recipe,user_createrecipe WHERE recipe.id=user_createrecipe.createRecipe ORDER BY recipe.id DESC limit ?,10;";// 创建sql命令对象ps = conn.prepareStatement(sql);// 执行sql语句rs = ps.executeQuery();if (rs != null) {while (rs.next()) {p.setTotleCount(rs.getInt(1));}}ps = conn.prepareStatement(sql1);// 给占位符赋值ps.setInt(1, index);rs = ps.executeQuery();if (rs != null) {while (rs.next()) {PageDetail pd = new PageDetail(rs.getInt("id"), rs.getString("title"), rs.getString("coverPath"),rs.getInt("userId"), rs.getString("username"));pageDetail.add(pd);p.setPageDetail(pageDetail);}}} catch (Exception e) {e.printStackTrace();} finally {// 关闭链接DbConnection.closeDB(conn, ps, rs);}// 返回结果return p;}// 请求homepage分页数据@Overridepublic HomePage homePageDao(String userId, int createCp, int collectionCp) {// 声明jdbc变量Connection conn = null;PreparedStatement ps = null;ResultSet rs = null;// 声明对象、变量HomePage hp = new HomePage();List<HomePageDetail> createDetail = new ArrayList<>();List<HomePageDetail> collectionDetail = new ArrayList<>();try {// 获取链接conn = DbConnection.getConnection();// 创建sql命令String sql = "SELECT count(1) FROM user,user_createrecipe,recipe where user.id=user_createrecipe.userId and recipe.id = user_createrecipe.createRecipe and user.id = ?;";String sql1 = "SELECT * FROM user,user_createrecipe,recipe where user.id=user_createrecipe.userId and recipe.id = user_createrecipe.createRecipe and user.id = ? ORDER BY recipe.id DESC limit ?,6;";String sql2 = "SELECT count(1) FROM user,user_collection,recipe where user.id=user_collection.userId and recipe.id = user_collection.collection and user.id = ?;";String sql3 = "SELECT * FROM user,user_collection,recipe where user.id=user_collection.userId and recipe.id = user_collection.collection and user.id = ? ORDER BY recipe.id DESC limit ?,6;";String sql4 = "SELECT * FROM user WHERE id = ?;";// 创建sql命令对象ps = conn.prepareStatement(sql);// 给占位符赋值ps.setString(1, userId);// 执行sql语句rs = ps.executeQuery();if (rs != null) {while (rs.next()) {hp.setCreateTotleCount(rs.getInt(1));}}ps = conn.prepareStatement(sql1);// 给占位符赋值ps.setString(1, userId);ps.setInt(2, (createCp - 1) * 6);// 执行sql语句rs = ps.executeQuery();if (rs != null) {while (rs.next()) {HomePageDetail hpd = new HomePageDetail(rs.getInt("recipe.id"), rs.getString("title"),rs.getString("coverPath"));createDetail.add(hpd);hp.setCreateDetail(createDetail);}}ps = conn.prepareStatement(sql2);// 给占位符赋值ps.setString(1, userId);// 执行sql语句rs = ps.executeQuery();if (rs != null) {while (rs.next()) {hp.setCollectionTotleCount(rs.getInt(1));}}ps = conn.prepareStatement(sql3);// 给占位符赋值ps.setString(1, userId);ps.setInt(2, (collectionCp - 1) * 6);// 执行sql语句rs = ps.executeQuery();if (rs != null) {while (rs.next()) {HomePageDetail hpd1 = new HomePageDetail(rs.getInt("recipe.id"), rs.getString("title"),rs.getString("coverPath"));collectionDetail.add(hpd1);hp.setCollectionDetail(collectionDetail);}}ps = conn.prepareStatement(sql4);// 给占位符赋值ps.setString(1, userId);// 执行sql语句rs = ps.executeQuery();if (rs != null) {while (rs.next()) {hp.setUsername(rs.getString("username"));hp.setCreateDate(rs.getString("createDate"));}}} catch (Exception e) {e.printStackTrace();} finally {// 关闭链接DbConnection.closeDB(conn, ps, rs);}// 返回结果return hp;}// 请求category分页数据@Overridepublic CategoryPage categoryPageDao(String item, int categoryCp) {// 声明jdbc变量Connection conn = null;PreparedStatement ps = null;ResultSet rs = null;// 声明对象、变量CategoryPage cp = new CategoryPage();List<CategoryPageDetail> categoryPageDetail = new ArrayList<>();try {// 获取链接conn = DbConnection.getConnection();// 创建sql命令String sql = "SELECT count(*) FROM recipe,recipe_category,category WHERE recipe.id = recipe_category.recipeId and recipe_category.category = category.id and category.id = ?";String sql1 = "SELECT * FROM recipe,user_createrecipe,recipe_category,category WHERE recipe.id = recipe_category.recipeId and recipe_category.category = category.id and recipe.id = user_createrecipe.createRecipe and category.id = ? ORDER BY recipeId DESC LIMIT ?,8";// 创建sql命令对象ps = conn.prepareStatement(sql);// 给占位符赋值ps.setString(1, item);// 执行sql语句rs = ps.executeQuery();if (rs != null) {while (rs.next()) {cp.setTotleCount(rs.getInt(1));}}ps = conn.prepareStatement(sql1);// 给占位符赋值ps.setString(1, item);ps.setInt(2, (categoryCp - 1) * 8);rs = ps.executeQuery();if (rs != null) {while (rs.next()) {CategoryPageDetail cpd = new CategoryPageDetail(rs.getInt("recipeId"), rs.getString("title"),rs.getString("coverPath"), rs.getInt("userId"), rs.getString("username"));categoryPageDetail.add(cpd);cp.setCategoryPageDetail(categoryPageDetail);}}} catch (Exception e) {e.printStackTrace();} finally {// 关闭链接DbConnection.closeDB(conn, ps, rs);}// 返回结果return cp;}// 请求搜索分页@Overridepublic SearchPage searchPageDao(int searchCp,String searchKeyword) {// 声明jdbc变量Connection conn = null;PreparedStatement ps = null;ResultSet rs = null;// 声明对象、变量SearchPage sp = new SearchPage();List<SearchPageDetail> searchPageDetail = new ArrayList<>();try {// 获取链接conn = DbConnection.getConnection();// 创建sql命令String sql = "SELECT count(1) FROM recipe,user_createrecipe WHERE title LIKE '%"+searchKeyword+"%' AND recipe.id = user_createrecipe.createRecipe";String sql1 = "SELECT * FROM recipe,user_createrecipe WHERE title LIKE '%"+searchKeyword+"%' AND recipe.id = user_createrecipe.createRecipe ORDER BY recipe.id DESC LIMIT ?,8"; // 创建sql命令对象ps = conn.prepareStatement(sql);// 执行sql语句rs = ps.executeQuery();if (rs != null) {while (rs.next()) {sp.setTotleCount(rs.getInt(1));}}ps = conn.prepareStatement(sql1);// 给占位符赋值ps.setInt(1, (searchCp - 1) * 8);rs = ps.executeQuery();if (rs != null) {while (rs.next()) {SearchPageDetail spd = new SearchPageDetail(rs.getInt("recipe.id"), rs.getString("title"),rs.getString("coverPath"), rs.getInt("userId"), rs.getString("username"));searchPageDetail.add(spd);sp.setSearchPageDetail(searchPageDetail);}}} catch (Exception e) {e.printStackTrace();} finally {// 关闭链接DbConnection.closeDB(conn, ps, rs);}// 返回结果return sp;}
}
RecipeCollectionServlet
package yjf.psyd.servlet;import java.io.IOException;
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 javax.servlet.http.HttpSession;import com.mysql.cj.Session;import yjf.psyd.bean.User;
import yjf.psyd.service.RecipeService;
import yjf.psyd.service.impl.RecipeServiceImpl;@WebServlet("/recipeCollection")
public class RecipeCollectionServlet extends HttpServlet {private static final long serialVersionUID = 1L;/*** @see HttpServlet#HttpServlet()*/public RecipeCollectionServlet() {super();}// 处理菜谱收藏功能protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {// 1、获取请求信息String status = req.getParameter("status");String RecipeId = req.getParameter("RecipeId");// 获取session对象HttpSession hs = req.getSession();// 把session中的值传到user对象中User user = (User) hs.getAttribute("user");RecipeService rs = new RecipeServiceImpl();// 2、判断status状态if (status.equals("false")) {// 3、处理请求结果;插入收藏菜谱int index = rs.insertCollectionRecipe(RecipeId, user);if(index>0) {resp.getWriter().write("{\"index\":" + index + "}");}} else {// 3、处理请求结果:删除收藏菜谱int index = rs.deleteCollectionRecipe(RecipeId, user);if(index>0) {resp.getWriter().write("{\"index\":" + index + "}");}}}}
获取源码