Java 开发网上蛋糕商城项目源码,可正常运行版
发布人:shili8
发布时间:2024-11-07 02:22
阅读次数:0
**Java 开发网上蛋糕商城项目源码**
###项目介绍本项目是一个简单的网上蛋糕商城系统,使用 Java语言开发。该系统包括用户注册、登录、商品浏览、购物车管理等功能。
###项目结构
markdowne-cake-shop/ |---- src/ | |---- main/ | | |---- java/ | | | |---- com/ | | | | |---- ecake/ | | | | | |---- bean/ | | | | | | |---- User.java| | | | | |---- service/ | | | | | | |---- UserService.java| | | | | |---- dao/ | | | | | | |---- UserDAO.java| | | | |---- com/ | | | | |---- ecake/ | | | | |---- web/ | | | | |---- index.jsp| | | | |---- login.jsp| | | | |---- product.jsp| | | | |---- cart.jsp| | |---- resources/ | | |---- application.properties
###项目源码#### User.java
java/** * @author [Your Name] */ public class User { private int id; private String username; private String password; public User() {} public User(int id, String username, String password) { this.id = id; this.username = username; this.password = password; } // getter and setter}
#### UserService.java
java/** * @author [Your Name] */ public class UserService { private UserDAO userDAO; public UserService() { this.userDAO = new UserDAO(); } public boolean registerUser(User user) { return userDAO.insertUser(user); } public boolean loginUser(String username, String password) { return userDAO.checkLogin(username, password); } }
#### UserDAO.java
java/** * @author [Your Name] */ public class UserDAO { private Connection conn; public UserDAO() { try { Class.forName("com.mysql.cj.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/e-cake-shop", "root", ""); } catch (SQLException e) { System.out.println(e.getMessage()); } } public boolean insertUser(User user) { String sql = "INSERT INTO users (username, password) VALUES (?, ?)"; try { PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, user.getUsername()); pstmt.setString(2, user.getPassword()); return pstmt.executeUpdate() ==1; } catch (SQLException e) { System.out.println(e.getMessage()); return false; } } public boolean checkLogin(String username, String password) { String sql = "SELECT * FROM users WHERE username = ? AND password = ?"; try { PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, username); pstmt.setString(2, password); ResultSet rs = pstmt.executeQuery(); return rs.next(); } catch (SQLException e) { System.out.println(e.getMessage()); return false; } } }
#### index.jsp
jsp<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML4.01 Transitional//EN" " /><html> <head> <meta content="text/html; charset=UTF-8"> <title>蛋糕商城</title> </head> <body> <h1>蛋糕商城</h1> <a href="login.jsp">登录</a> <a href="register.jsp">注册</a> </body> </html>
#### login.jsp
jsp<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML4.01 Transitional//EN" " /><html> <head> <meta content="text/html; charset=UTF-8"> <title>登录</title> </head> <body> <h1>登录</h1> <form action="login.do" method="post"> <input type="text" name="username" placeholder="用户名"> <input type="password" name="password" placeholder="密码"> <button type="submit">登录</button> </form> </body> </html>
#### product.jsp
jsp<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML4.01 Transitional//EN" " /><html> <head> <meta content="text/html; charset=UTF-8"> <title>商品列表</title> </head> <body> <h1>商品列表</h1> <ul> <li><a href="#">蛋糕A</a></li> <li><a href="#">蛋糕B</a></li> <li><a href="#">蛋糕C</a></li> </ul> </body> </html>
#### cart.jsp
jsp<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML4.01 Transitional//EN" " /><html> <head> <meta content="text/html; charset=UTF-8"> <title>购物车</title> </head> <body> <h1>购物车</h1> <ul> <li><a href="#">蛋糕A</a></li> <li><a href="#">蛋糕B</a></li> <li><a href="#">蛋糕C</a></li> </ul> </body> </html>
###项目配置#### application.properties
propertiesspring.datasource.url=jdbc:mysql://localhost:3306/e-cake-shopspring.datasource.username=rootspring.datasource.password= spring.jpa.hibernate.ddl-auto=update
### 运行项目1. 使用IDEA或Eclipse等IDE打开项目。
2. 在pom.xml文件中添加依赖项:
xml<dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.21</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
3. 在application.properties文件中配置数据库连接信息。
4. 运行项目,访问 />
### 总结本文介绍了一个简单的网上蛋糕商城系统的开发过程。该系统包括用户注册、登录、商品浏览、购物车管理等功能。使用Java语言和Spring Boot框架开发该系统,能够快速实现一个基本的商城系统。