搭建一个基于Javaweb的蛋糕商城需要以下步骤:
1. 确定需求
首先需要明确商城的功能需求,例如商品展示、购物车、订单管理、支付等等。
2. 设计数据库
根据需求设计数据库,并创建相应的表。常见的表包括商品表、用户表、订单表等。
3. 搭建环境
搭建Java Web环境,例如Tomcat、MySQL等。
4. 编写代码
根据需求编写代码,包括前端页面、后端逻辑等。
以下是一个简单的代码示例:
(1)创建商品表
```mysql
CREATE TABLE `goods` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`price` decimal(10,2) DEFAULT NULL,
`description` varchar(255) DEFAULT NULL,
`image` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
```
(2)创建用户表
```mysql
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
```
(3)创建订单表
```mysql
CREATE TABLE `order` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`goods_id` int(11) DEFAULT NULL,
`quantity` int(11) DEFAULT NULL,
`total_price` decimal(10,2) DEFAULT NULL,
`created_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
```
(4)编写商品展示页面
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>商品列表</title>
</head>
<body>
<h1>商品列表</h1>
<table border="1">
<thead>
<tr>
<th>ID</th>
<th>名称</th>
<th>价格</th>
<th>描述</th>
<th>图片</th>
</tr>
</thead>
<tbody>
<%
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/shop", "root", "password");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM goods");
while (rs.next()) {
%>
<tr>
<td><%=rs.getInt("id")%></td>
<td><%=rs.getString("name")%></td>
<td><%=rs.getBigDecimal("price")%></td>
<td><%=rs.getString("description")%></td>
<td><img src="<%=rs.getString("image")%>" width="100" height="100"></td>
</tr>
<%
}
rs.close();
stmt.close();
conn.close();
%>
</tbody>
</table>
</body>
</html>
```
(5)编写用户登录页面
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户登录</title>
</head>
<body>
<h1>用户登录</h1>
<form action="login" method="post">
<label for="username">用户名:</label>
<input type="text" id="username" name="username"><br>
<label for="password">密码:</label>
<input type="password" id="password" name="password"><br>
<input type="submit" value="登录">
</form>
</body>
</html>
```
(6)编写用户登录逻辑
```java
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/shop", "root", "password");
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM user WHERE username=? AND password=?");
stmt.setString(1, username);
stmt.setString(2, password);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
request.getSession().setAttribute("user_id", rs.getInt("id"));
response.sendRedirect("goods");
} else {
response.sendRedirect("login");
}
rs.close();
stmt.close();
conn.close();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
response.sendRedirect("login");
}
}
}
```
(7)编写购物车页面
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>购物车</title>
</head>
<body>
<h1>购物车</h1>
<table border="1">
<thead>
<tr>
<th>ID</th>
<th>名称</th>
<th>单价</th>
<th>数量</th>
<th>小计</th>
</tr>
</thead>
<tbody>
<%
int user_id = (int) request.getSession().getAttribute("user_id");
BigDecimal total_price = BigDecimal.ZERO;
if (user_id != 0) {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/shop", "root", "password");
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM `order` WHERE user_id=?");
stmt.setInt(1, user_id);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
PreparedStatement stmt2 = conn.prepareStatement("SELECT * FROM goods WHERE id=?");
stmt2.setInt(1, rs.getInt("goods_id"));
ResultSet rs2 = stmt2.executeQuery();
if (rs2.next()) {
%>
<tr>
<td><%=rs.getInt("id")%></td>
<td><%=rs2.getString("name")%></td>
<td><%=rs2.getBigDecimal("price")%></td>
<td><%=rs.getInt("quantity")%></td>
<td><%=rs.getBigDecimal("total_price")%></td>
</tr>
<%
total_price = total_price.add(rs.getBigDecimal("total_price"));
}
rs2.close();
stmt2.close();
}
rs.close();
stmt.close();
conn.close();
}
%>
</tbody>
<tfoot>
<tr>
<td colspan="4">总价:</td>
<td><%=total_price%></td>
</tr>
</tfoot>
</table>
</body>
</html>
```
(8)编写添加商品到购物车逻辑
```java
public class AddToCartServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int user_id = (int) request.getSession().getAttribute("user_id");
int goods_id = Integer.parseInt(request.getParameter("goods_id"));
int quantity = Integer.parseInt(request.getParameter("quantity"));
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/shop", "root", "password");
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM goods WHERE id=?");
stmt.setInt(1, goods_id);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
BigDecimal total_price = rs.getBigDecimal("price").multiply(new BigDecimal(quantity));
PreparedStatement stmt2 = conn.prepareStatement("INSERT INTO `order` (user_id, goods_id, quantity, total_price, created_time) VALUES (?, ?, ?, ?, NOW())");
stmt2.setInt(1, user_id);
stmt2.setInt(2, goods_id);
stmt2.setInt(3, quantity);
stmt2.setBigDecimal(4, total_price);
stmt2.executeUpdate();
stmt2.close();
}
rs.close();
stmt.close();
conn.close();
response.sendRedirect("cart");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
response.sendRedirect("goods");
}
}
}
```
以上是一个简单的示例,实际项目中还需要考虑安全性、性能优化等因素。