源码及数据库文件在文末链接中
电脑商城项目
✨欢迎大家访问我的个人博客:随风起の博客️详细信息如下
如果觉得本篇文章还不错的话,欢迎大家点赞+收藏❤️+评论
目录
一、项目介绍
1.项目技术
2.项目数据库
3.项目截图及说明
首页及购物车模块
用户模块
登录及注册模块
二、代码部分
1.application.properties
2.UserController
3. 单元测试
三、注意事项
1.修改路径
2.分页数据设置
四、源码链接
springboot + mybatis + maven + ajax
t_user:用户表
t_product:产品表
t_address:用户地址表
t_order:订单主表
t_cart:购物车
t_order_item:订单详情表
t_dict_district:中国省市区表
t_product_cateory:产品分类表
说明:除了注册、登录、首页、商品详情页及静态页面未设置拦截,其余功能都需用户进行登录才能操作。
商品详细页
点击加入购物车后,进入购物车界面
点击结算会将选中的商品进行结算
修改密码
修改个人资料
修改头像
收货地址
部分代码如下:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/store
spring.datasource.username=root
spring.datasource.password=123456
mybatis.mapper-locations=classpath:mapper/*.xml
user.address.max-count=20
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=100MB
mybatis.configuration.map-underscore-to-camel-case=true
pagehelper.helper-dialect=mysql
pagehelper.reasonable=true
package com.xxx.store.controller;
import com.xxx.store.controller.exception.*;
import com.xxx.store.entity.User;
import com.xxx.store.service.UserService;
import com.xxx.store.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@RestController
@RequestMapping("/users")
public class UserController extends BaseController{
@Autowired
private UserService userService;
@RequestMapping("regist")
public JsonResult regist(User user){
userService.regist(user);
return new JsonResult<>(OK);
}
@RequestMapping("login")
public JsonResult login(String username, String password, HttpSession session){
User login = userService.login(username, password);
session.setAttribute("uid",login.getUid());
session.setAttribute("username",login.getUsername());
System.out.println("用户id:"+getUidFromSession(session)+"密码:"+getUsernameFromSession(session));
return new JsonResult(OK,login);
}
@RequestMapping("change_password")
public JsonResult changePassword(String oldPassword,String newPassword,HttpSession session){
Integer uid=getUidFromSession(session);
String username = getUsernameFromSession(session);
userService.changePassword(uid,username,oldPassword,newPassword);
return new JsonResult(OK);
}
@RequestMapping("getInfo")
public JsonResult getInfo(HttpSession session){
User user = userService.getByUid(getUidFromSession(session));
return new JsonResult<>(OK, user);
}
@RequestMapping("changeInfo")
public JsonResult changeInfo(HttpSession session,User user){
userService.changeInfo(getUidFromSession(session),getUsernameFromSession(session),user);
return new JsonResult<>(OK);
}
public static final int AVATAR_SIZE=10*1024*1024;
public static final List AVATAR_TYPE=new ArrayList<>();
static {
AVATAR_TYPE.add("image/png");
AVATAR_TYPE.add("image/jpeg");
AVATAR_TYPE.add("image/gif");
AVATAR_TYPE.add("image/bmp");
}
@RequestMapping("changeAvatar")
public JsonResult changeAvatar(HttpSession session, MultipartFile file){
if (file.isEmpty()){
throw new FileEmptyException("文件为空");
}
if (file.getSize()>AVATAR_SIZE){
throw new FileSizeException("文件大小超出限制");
}
if (!AVATAR_TYPE.contains(file.getContentType())){
throw new FileTypeException("文件类型不支持");
}
String upload = "E:\\JavaCode\\store\\src\\main\\resources\\static\\upload";
File dir = new File(upload);
if (!dir.exists()){
dir.mkdir();
}
String originalFilename = file.getOriginalFilename();
//获取后缀
String suffix=originalFilename.substring(originalFilename.lastIndexOf("."));
String filename=UUID.randomUUID().toString()+suffix;
File dest = new File(dir,filename);//空文件
try {
file.transferTo(dest);
}catch (FileStateException e){
throw new FileStateException("文件状态异常");
} catch (IOException e) {
throw new FileUploadException("文件读写异常");
}
String avatar="/upload/"+filename;
userService.changeAvatar(getUidFromSession(session),avatar,getUsernameFromSession(session));
return new JsonResult<>(OK,avatar);
}
}
订单Mapper测试
package com.xxx.store.mapper;
import com.xxx.store.entity.Cart;
import com.xxx.store.entity.Order;
import com.xxx.store.entity.OrderItem;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Date;
@SpringBootTest
public class OrderMapperTests {
@Autowired
OrderMapper orderMapper;
@Test
void insertOrder(){
Order order = new Order();
order.setUid(14);
order.setRecvName("456");
order.setRecvPhone("19345231589");
orderMapper.insertOrder(order);
}
@Test
void insertOrderItem(){
OrderItem orderItem = new OrderItem();
orderItem.setOid(1);
orderItem.setPid(10000001);
orderItem.setTitle("施耐德(Schneider) K15 经典款圆珠笔 (5支混色装)");
orderMapper.insertOrderItem(orderItem);
}
}
这里是一些使用时的注意事项
config文件中的LoginInterceptorConfigurer的资源地址,这里配置addResourceHandler和addResourceLocations,使得可以从磁盘中读取图片、视频、音频等,使用时注意改到自己的磁盘路径,这里我使用的是项目下的static/upload的绝对路径
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/upload/**")
.addResourceLocations("file:E:/JavaCode/store/src/main/resources/static/upload/");
}
UserController中创建upload文件夹的位置,这里我使用的是项目下的static/upload的绝对路径
在CartController中我设置的是一页展示三条数据,每次显示两页
@RequestMapping({"","/"})
public JsonResult> getCart(@RequestParam(value = "pn",defaultValue = "1")Integer pn,HttpSession session){
PageHelper.startPage(pn,3);
List cart = cartService.getCartByUid(getUidFromSession(session));
PageInfo page = new PageInfo<>(cart,2);
return new JsonResult<>(OK,page);
}
链接:https://pan.baidu.com/s/15XCUlqeIV3eUsILkJ3cJgQ
提取码:f31d