用Java开发简易电子商务网站通常会选择Spring Boot作为后端框架,搭配MySQL存储业务数据,前端可以采用简单的Thymeleaf模板或者分离式前端方案,整体开发效率较高且生态成熟。下面以基础功能实现为例介绍完整流程。

一、技术选型与环境搭建
核心依赖选择Spring Boot 2.x版本,持久层使用MyBatis,数据库用MySQL 8.0,构建工具选择Maven。首先在Maven的pom.xml中添加核心依赖:
<dependencies>
<!-- Spring Boot Web核心依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- MyBatis整合Spring Boot -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<!-- MySQL驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Thymeleaf模板引擎 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
二、数据库表设计
简易电商网站需要核心的几张表,分别是商品表、用户表、购物车表和订单表,以下是核心表的结构设计:
| 表名 | 核心字段 | 字段说明 |
|---|---|---|
| product | id, name, price, stock, description | 商品ID、商品名称、单价、库存、商品描述 |
| user | id, username, password, phone | 用户ID、用户名、密码、手机号 |
| cart | id, user_id, product_id, quantity | 购物车ID、用户ID、商品ID、购买数量 |
| order | id, user_id, total_price, create_time, status | 订单ID、用户ID、订单总价、创建时间、订单状态 |
三、核心功能实现
1. 商品展示功能
首先编写商品实体类和Mapper接口,实现商品列表查询功能:
// 商品实体类
public class Product {
private Integer id;
private String name;
private Double price;
private Integer stock;
private String description;
// 省略getter和setter方法
}
// 商品Mapper接口
@Mapper
public interface ProductMapper {
// 查询所有商品
@Select("SELECT * FROM product")
List<Product> selectAllProducts();
// 根据ID查询商品
@Select("SELECT * FROM product WHERE id = #{id}")
Product selectProductById(Integer id);
}
对应的Controller层代码:
@RestController
@RequestMapping("/product")
public class ProductController {
@Autowired
private ProductMapper productMapper;
// 获取商品列表接口
@GetMapping("/list")
public List<Product> getProductList() {
return productMapper.selectAllProducts();
}
// 获取商品详情接口
@GetMapping("/detail/{id}")
public Product getProductDetail(@PathVariable Integer id) {
return productMapper.selectProductById(id);
}
}
2. 购物车功能
购物车功能需要实现添加商品、修改数量、查询购物车列表三个核心操作:
// 购物车实体类
public class Cart {
private Integer id;
private Integer userId;
private Integer productId;
private Integer quantity;
// 省略getter和setter方法
}
// 购物车Mapper接口
@Mapper
public interface CartMapper {
// 添加商品到购物车
@Insert("INSERT INTO cart(user_id, product_id, quantity) VALUES(#{userId}, #{productId}, #{quantity})")
void addCartItem(Cart cart);
// 根据用户ID查询购物车列表
@Select("SELECT c.*, p.name, p.price FROM cart c JOIN product p ON c.product_id = p.id WHERE c.user_id = #{userId}")
List<Map<String, Object>> selectCartByUserId(Integer userId);
// 修改购物车商品数量
@Update("UPDATE cart SET quantity = #{quantity} WHERE id = #{id} AND user_id = #{userId}")
void updateCartQuantity(@Param("id") Integer id, @Param("userId") Integer userId, @Param("quantity") Integer quantity);
}
3. 订单生成功能
订单生成需要校验库存、计算总价、生成订单记录,以下是核心逻辑代码:
@Service
public class OrderService {
@Autowired
private CartMapper cartMapper;
@Autowired
private ProductMapper productMapper;
@Autowired
private OrderMapper orderMapper;
// 生成订单方法
public Integer createOrder(Integer userId) {
// 查询用户购物车
List<Map<String, Object>> cartList = cartMapper.selectCartByUserId(userId);
if (cartList == null || cartList.isEmpty()) {
throw new RuntimeException("购物车为空,无法生成订单");
}
Double totalPrice = 0.0;
// 校验库存并计算总价
for (Map<String, Object> item : cartList) {
Integer productId = (Integer) item.get("product_id");
Integer quantity = (Integer) item.get("quantity");
Double price = (Double) item.get("price");
Product product = productMapper.selectProductById(productId);
if (product.getStock() < quantity) {
throw new RuntimeException("商品" + product.getName() + "库存不足");
}
totalPrice += price * quantity;
}
// 生成订单
Order order = new Order();
order.setUserId(userId);
order.setTotalPrice(totalPrice);
order.setCreateTime(new Date());
order.setStatus(0); // 0表示待支付
orderMapper.insertOrder(order);
// 清空购物车
cartMapper.deleteCartByUserId(userId);
return order.getId();
}
}
四、开发注意事项
- 数据库操作时注意添加事务控制,尤其是订单生成这类涉及多表操作的场景,避免数据不一致
- 用户密码存储不要明文保存,建议使用BCrypt等加密算法处理
- 商品库存修改时要加行锁或者使用乐观锁,防止超卖问题
- 接口返回的数据建议统一封装成固定格式,方便前端处理
以上就是一个简易电子商务网站的核心实现过程,开发者可以根据需求扩展支付、物流查询等功能,逐步完善整个系统。
Java电子商务网站Spring_BootMySQLMyBatis修改时间:2026-07-17 09:57:35