Merge branch 'main' of https://bdgit.educoder.net/pycos5i64/mallsystem
@ -0,0 +1,8 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
@ -0,0 +1,29 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<!-- 编译器配置组件 -->
|
||||
<component name="CompilerConfiguration">
|
||||
<!-- 注解处理配置 -->
|
||||
<annotationProcessing>
|
||||
<!-- Maven 默认注解处理器配置 -->
|
||||
<profile name="Maven default annotation processors profile" enabled="true">
|
||||
<!-- 生成的源代码输出目录 -->
|
||||
<sourceOutputDir name="target/generated-sources/annotations" />
|
||||
<!-- 生成的测试源代码输出目录 -->
|
||||
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
|
||||
<!-- 输出路径相对于内容根目录 -->
|
||||
<outputRelativeToContentRoot value="true" />
|
||||
<!-- 应用此配置的模块名称 -->
|
||||
<module name="springboot" />
|
||||
</profile>
|
||||
</annotationProcessing>
|
||||
</component>
|
||||
|
||||
<!-- Java 编译器设置 -->
|
||||
<component name="JavacSettings">
|
||||
<!-- 附加编译器选项覆盖 -->
|
||||
<option name="ADDITIONAL_OPTIONS_OVERRIDE">
|
||||
<!-- 为名为 "springboot" 的模块指定编译器选项 -->
|
||||
<module name="springboot" options="-parameters" />
|
||||
</option>
|
||||
</component>
|
||||
</project>
|
||||
|
After Width: | Height: | Size: 3.0 KiB |
|
After Width: | Height: | Size: 3.0 KiB |
|
After Width: | Height: | Size: 3.0 KiB |
|
After Width: | Height: | Size: 411 KiB |
|
After Width: | Height: | Size: 411 KiB |
|
After Width: | Height: | Size: 325 KiB |
|
After Width: | Height: | Size: 3.1 KiB |
|
After Width: | Height: | Size: 3.0 KiB |
|
After Width: | Height: | Size: 3.6 KiB |
|
After Width: | Height: | Size: 2.9 KiB |
|
After Width: | Height: | Size: 3.1 KiB |
|
After Width: | Height: | Size: 36 KiB |
|
After Width: | Height: | Size: 53 KiB |
|
After Width: | Height: | Size: 44 KiB |
|
After Width: | Height: | Size: 30 KiB |
|
After Width: | Height: | Size: 323 KiB |
|
After Width: | Height: | Size: 974 KiB |
|
After Width: | Height: | Size: 900 KiB |
|
After Width: | Height: | Size: 982 KiB |
@ -0,0 +1,90 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.example</groupId>
|
||||
<artifactId>springboot</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.5.9</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!--mysql-->
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
<version>2.2.1</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.github.pagehelper</groupId>
|
||||
<artifactId>pagehelper-spring-boot-starter</artifactId>
|
||||
<version>1.4.6</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.mybatis</groupId>
|
||||
<artifactId>mybatis</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-all</artifactId>
|
||||
<version>5.8.18</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.auth0</groupId>
|
||||
<artifactId>java-jwt</artifactId>
|
||||
<version>4.3.0</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<fork>true</fork>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>public</id>
|
||||
<name>aliyun nexus</name>
|
||||
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
|
||||
<releases>
|
||||
<enabled>true</enabled>
|
||||
</releases>
|
||||
</repository>
|
||||
</repositories>
|
||||
</project>
|
||||
@ -0,0 +1,18 @@
|
||||
package com.example;
|
||||
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
// SpringBoot应用启动类
|
||||
@SpringBootApplication
|
||||
@MapperScan("com.example.mapper") // 扫描Mapper接口所在的包
|
||||
public class SpringbootApplication {
|
||||
|
||||
// 程序入口
|
||||
public static void main(String[] args) {
|
||||
// 启动SpringBoot应用
|
||||
SpringApplication.run(SpringbootApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,9 @@
|
||||
package com.example.common;
|
||||
|
||||
public interface Constants {
|
||||
|
||||
String TOKEN = "token";
|
||||
|
||||
String USER_DEFAULT_PASSWORD = "123";
|
||||
|
||||
}
|
||||
@ -0,0 +1,90 @@
|
||||
package com.example.common;
|
||||
|
||||
import com.example.common.enums.ResultCodeEnum;
|
||||
|
||||
// 统一API响应结果封装类
|
||||
// 用于标准化所有接口的返回格式
|
||||
public class Result_ {
|
||||
// 状态码
|
||||
private String code;
|
||||
// 状态描述信息
|
||||
private String msg;
|
||||
// 返回的数据内容
|
||||
private Object data;
|
||||
// 私有构造函数 - 用于创建带数据的成功结果
|
||||
// @param data 要返回的数据
|
||||
private Result_(Object data) {
|
||||
this.data = data;
|
||||
}
|
||||
// 无参构造函数
|
||||
public Result_() {
|
||||
}
|
||||
// 创建成功的空结果(无数据)
|
||||
// @return 成功结果对象
|
||||
public static Result_ success() {
|
||||
Result_ tResult = new Result_();
|
||||
tResult.setCode(ResultCodeEnum.SUCCESS.code);
|
||||
tResult.setMsg(ResultCodeEnum.SUCCESS.msg);
|
||||
return tResult;
|
||||
}
|
||||
// 创建成功的带数据结果
|
||||
// @param data 要返回的数据对象
|
||||
// @return 成功结果对象
|
||||
public static Result_ success(Object data) {
|
||||
Result_ tResult = new Result_(data);
|
||||
tResult.setCode(ResultCodeEnum.SUCCESS.code);
|
||||
tResult.setMsg(ResultCodeEnum.SUCCESS.msg);
|
||||
return tResult;
|
||||
}
|
||||
// 创建系统错误结果
|
||||
// @return 系统错误结果对象
|
||||
public static Result_ error() {
|
||||
Result_ tResult = new Result_();
|
||||
tResult.setCode(ResultCodeEnum.SYSTEM_ERROR.code);
|
||||
tResult.setMsg(ResultCodeEnum.SYSTEM_ERROR.msg);
|
||||
return tResult;
|
||||
}
|
||||
// 创建自定义错误结果
|
||||
// @param code 错误码
|
||||
// @param msg 错误信息
|
||||
// @return 错误结果对象
|
||||
public static Result_ error(String code, String msg) {
|
||||
Result_ tResult = new Result_();
|
||||
tResult.setCode(code);
|
||||
tResult.setMsg(msg);
|
||||
return tResult;
|
||||
}
|
||||
// 根据枚举创建错误结果
|
||||
// @param resultCodeEnum 结果码枚举
|
||||
// @return 错误结果对象
|
||||
public static Result_ error(ResultCodeEnum resultCodeEnum) {
|
||||
Result_ tResult = new Result_();
|
||||
tResult.setCode(resultCodeEnum.code);
|
||||
tResult.setMsg(resultCodeEnum.msg);
|
||||
return tResult;
|
||||
}
|
||||
// Getter 和 Setter 方法
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(String code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public String getMsg() {
|
||||
return msg;
|
||||
}
|
||||
|
||||
public void setMsg(String msg) {
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
public Object getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(Object data) {
|
||||
this.data = data;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,25 @@
|
||||
package com.example.common.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.cors.CorsConfiguration;
|
||||
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
||||
import org.springframework.web.filter.CorsFilter;
|
||||
|
||||
/**
|
||||
* 跨域配置
|
||||
*/
|
||||
@Configuration
|
||||
public class CorsConfig {
|
||||
|
||||
@Bean
|
||||
public CorsFilter corsFilter() {
|
||||
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
||||
CorsConfiguration corsConfiguration = new CorsConfiguration();
|
||||
corsConfiguration.addAllowedOrigin("*"); // 1 设置访问源地址
|
||||
corsConfiguration.addAllowedHeader("*"); // 2 设置访问源请求头
|
||||
corsConfiguration.addAllowedMethod("*"); // 3 设置访问源请求方法
|
||||
source.registerCorsConfiguration("/**", corsConfiguration); // 4 对接口配置跨域设置
|
||||
return new CorsFilter(source);//// 5 创建并返回CORS过滤器
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,10 @@
|
||||
package com.example.common.enums;
|
||||
|
||||
public enum RoleEnum {
|
||||
// 管理员
|
||||
ADMIN,
|
||||
// 商家
|
||||
BUSINESS,
|
||||
// 用户
|
||||
USER,
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
package com.example.common.enums;
|
||||
|
||||
public enum StatusEnum {
|
||||
CHECKING("审核中"),
|
||||
CHECK_OK("审核通过"),
|
||||
CHECK_ON("审核不通过")
|
||||
;
|
||||
|
||||
public String status;
|
||||
|
||||
StatusEnum(String status) {
|
||||
this.status = status;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,99 @@
|
||||
package com.example.controller;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.example.common.Result_;
|
||||
import com.example.common.enums.ResultCodeEnum;
|
||||
import com.example.common.enums.RoleEnum;
|
||||
import com.example.entity.Account;
|
||||
import com.example.service.AdminService;
|
||||
import com.example.service.BusinessService;
|
||||
import com.example.service.UserService;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* 基础前端接口
|
||||
*/
|
||||
@RestController
|
||||
public class WebController {
|
||||
|
||||
@Resource
|
||||
private AdminService adminService;
|
||||
|
||||
@Resource
|
||||
private BusinessService businessService;
|
||||
|
||||
@Resource
|
||||
private UserService userService;
|
||||
|
||||
@GetMapping("/")
|
||||
public Result_ hello() {
|
||||
return Result_.success("访问成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 登录
|
||||
*/
|
||||
@PostMapping("/login")
|
||||
public Result_ login(@RequestBody Account account) {
|
||||
if (ObjectUtil.isEmpty(account.getUsername()) || ObjectUtil.isEmpty(account.getPassword())
|
||||
|| ObjectUtil.isEmpty(account.getRole())) {
|
||||
return Result_.error(ResultCodeEnum.PARAM_LOST_ERROR);
|
||||
}
|
||||
if (RoleEnum.ADMIN.name().equals(account.getRole())) {
|
||||
account = adminService.login(account);
|
||||
}
|
||||
if (RoleEnum.BUSINESS.name().equals(account.getRole())) {
|
||||
account = businessService.login(account);
|
||||
}
|
||||
if (RoleEnum.USER.name().equals(account.getRole())) { // 用户登录
|
||||
account = userService.login(account);
|
||||
}
|
||||
return Result_.success(account);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册
|
||||
*/
|
||||
@PostMapping("/register")
|
||||
public Result_ register(@RequestBody Account account) {
|
||||
if (StrUtil.isBlank(account.getUsername()) || StrUtil.isBlank(account.getPassword())
|
||||
|| ObjectUtil.isEmpty(account.getRole())) {
|
||||
return Result_.error(ResultCodeEnum.PARAM_LOST_ERROR);
|
||||
}
|
||||
if (RoleEnum.ADMIN.name().equals(account.getRole())) {
|
||||
adminService.register(account);
|
||||
}
|
||||
if (RoleEnum.BUSINESS.name().equals(account.getRole())) {
|
||||
businessService.register(account);
|
||||
}
|
||||
if (RoleEnum.USER.name().equals(account.getRole())) { // 用户注册
|
||||
userService.register(account);
|
||||
}
|
||||
return Result_.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改密码
|
||||
*/
|
||||
@PutMapping("/updatePassword")
|
||||
public Result_ updatePassword(@RequestBody Account account) {
|
||||
if (StrUtil.isBlank(account.getUsername()) || StrUtil.isBlank(account.getPassword())
|
||||
|| ObjectUtil.isEmpty(account.getNewPassword())) {
|
||||
return Result_.error(ResultCodeEnum.PARAM_LOST_ERROR);
|
||||
}
|
||||
if (RoleEnum.ADMIN.name().equals(account.getRole())) { // 管理员修改密码
|
||||
adminService.updatePassword(account);
|
||||
}
|
||||
if (RoleEnum.BUSINESS.name().equals(account.getRole())) { // 商家修改密码
|
||||
businessService.updatePassword(account);
|
||||
}
|
||||
if (RoleEnum.USER.name().equals(account.getRole())) { // 用户修改密码
|
||||
userService.updatePassword(account);
|
||||
}
|
||||
return Result_.success();
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,102 @@
|
||||
package com.example.entity;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
// 管理员实体类
|
||||
public class Admin extends Account implements Serializable {
|
||||
// 序列化版本ID
|
||||
private static final long serialVersionUID = 1L;
|
||||
// 管理员ID
|
||||
private Integer id;
|
||||
// 用户名
|
||||
private String username;
|
||||
// 密码
|
||||
private String password;
|
||||
// 姓名
|
||||
private String name;
|
||||
// 电话
|
||||
private String phone;
|
||||
// 邮箱
|
||||
private String email;
|
||||
// 头像
|
||||
private String avatar;
|
||||
// 角色标识
|
||||
private String role;
|
||||
|
||||
// 获取管理员ID
|
||||
@Override
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
// 设置管理员ID
|
||||
@Override
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
// 获取用户名
|
||||
@Override
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
// 设置用户名
|
||||
@Override
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
// 获取密码
|
||||
@Override
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
// 设置密码
|
||||
@Override
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
// 获取姓名
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
// 设置姓名
|
||||
@Override
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
// 获取电话
|
||||
public String getPhone() {
|
||||
return phone;
|
||||
}
|
||||
// 设置电话
|
||||
public void setPhone(String phone) {
|
||||
this.phone = phone;
|
||||
}
|
||||
// 获取邮箱
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
// 设置邮箱
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
// 获取头像
|
||||
@Override
|
||||
public String getAvatar() {
|
||||
return avatar;
|
||||
}
|
||||
// 设置头像
|
||||
@Override
|
||||
public void setAvatar(String avatar) {
|
||||
this.avatar = avatar;
|
||||
}
|
||||
// 获取角色标识
|
||||
@Override
|
||||
public String getRole() {
|
||||
return role;
|
||||
}
|
||||
// 设置角色标识
|
||||
@Override
|
||||
public void setRole(String role) {
|
||||
this.role = role;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,123 @@
|
||||
package com.example.entity;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
// 功能:商家实体类
|
||||
public class Business extends Account implements Serializable {
|
||||
// 序列化版本ID
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
// 商家ID
|
||||
private Integer id;
|
||||
// 用户名
|
||||
private String username;
|
||||
// 密码
|
||||
private String password;
|
||||
// 商家名称
|
||||
private String name;
|
||||
// 联系电话
|
||||
private String phone;
|
||||
// 邮箱
|
||||
private String email;
|
||||
// 头像
|
||||
private String avatar;
|
||||
// 角色标识
|
||||
private String role;
|
||||
// 商家描述
|
||||
private String description;
|
||||
// 商家状态
|
||||
private String status;
|
||||
|
||||
// 获取商家ID
|
||||
@Override
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
// 设置商家ID
|
||||
@Override
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
// 获取用户名
|
||||
@Override
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
// 设置用户名
|
||||
@Override
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
// 获取密码
|
||||
@Override
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
// 设置密码
|
||||
@Override
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
// 获取商家名称
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
// 设置商家名称
|
||||
@Override
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
// 获取联系电话
|
||||
public String getPhone() {
|
||||
return phone;
|
||||
}
|
||||
// 设置联系电话
|
||||
public void setPhone(String phone) {
|
||||
this.phone = phone;
|
||||
}
|
||||
// 获取邮箱
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
// 设置邮箱
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
// 获取头像
|
||||
@Override
|
||||
public String getAvatar() {
|
||||
return avatar;
|
||||
}
|
||||
// 设置头像
|
||||
@Override
|
||||
public void setAvatar(String avatar) {
|
||||
this.avatar = avatar;
|
||||
}
|
||||
// 获取角色标识
|
||||
@Override
|
||||
public String getRole() {
|
||||
return role;
|
||||
}
|
||||
// 设置角色标识
|
||||
@Override
|
||||
public void setRole(String role) {
|
||||
this.role = role;
|
||||
}
|
||||
// 获取商家描述
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
// 设置商家描述
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
// 获取商家状态
|
||||
public String getStatus() {
|
||||
return status;
|
||||
}
|
||||
// 设置商家状态
|
||||
public void setStatus(String status) {
|
||||
this.status = status;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,48 @@
|
||||
package com.example.entity;
|
||||
|
||||
// 功能:推荐系统关联关系数据传输对象
|
||||
public class RelateDTO {
|
||||
/** 用户id */
|
||||
private Integer useId;
|
||||
/** 商品id */
|
||||
private Integer goodsId;
|
||||
/** 指数(评分/偏好度) */
|
||||
private Integer index;
|
||||
|
||||
// 构造函数
|
||||
public RelateDTO(Integer useId, Integer goodsId, Integer index) {
|
||||
this.useId = useId;
|
||||
this.goodsId = goodsId;
|
||||
this.index = index;
|
||||
}
|
||||
|
||||
// 获取用户ID
|
||||
public Integer getUseId() {
|
||||
return useId;
|
||||
}
|
||||
|
||||
// 设置用户ID
|
||||
public void setUseId(Integer useId) {
|
||||
this.useId = useId;
|
||||
}
|
||||
|
||||
// 获取商品ID
|
||||
public Integer getGoodsId() {
|
||||
return goodsId;
|
||||
}
|
||||
|
||||
// 设置商品ID
|
||||
public void setGoodsId(Integer goodsId) {
|
||||
this.goodsId = goodsId;
|
||||
}
|
||||
|
||||
// 获取指数(评分/偏好度)
|
||||
public Integer getIndex() {
|
||||
return index;
|
||||
}
|
||||
|
||||
// 设置指数(评分/偏好度)
|
||||
public void setIndex(Integer index) {
|
||||
this.index = index;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,46 @@
|
||||
package com.example.exception;
|
||||
|
||||
import com.example.common.enums.ResultCodeEnum;
|
||||
|
||||
// 自定义异常类 - 用于业务异常处理
|
||||
public class CustomException extends RuntimeException {
|
||||
// 异常代码
|
||||
private String code;
|
||||
// 异常信息
|
||||
private String msg;
|
||||
|
||||
/**
|
||||
* 通过枚举构造自定义异常
|
||||
* @param resultCodeEnum 结果码枚举
|
||||
*/
|
||||
public CustomException(ResultCodeEnum resultCodeEnum) {
|
||||
this.code = resultCodeEnum.code;
|
||||
this.msg = resultCodeEnum.msg;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过代码和信息构造自定义异常
|
||||
* @param code 异常代码
|
||||
* @param msg 异常信息
|
||||
*/
|
||||
public CustomException(String code, String msg) {
|
||||
this.code = code;
|
||||
this.msg = msg;
|
||||
}
|
||||
// 获取异常代码
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
// 设置异常代码
|
||||
public void setCode(String code) {
|
||||
this.code = code;
|
||||
}
|
||||
// 获取异常信息
|
||||
public String getMsg() {
|
||||
return msg;
|
||||
}
|
||||
// 设置异常信息
|
||||
public void setMsg(String msg) {
|
||||
this.msg = msg;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,46 @@
|
||||
package com.example.exception;
|
||||
|
||||
import cn.hutool.log.Log;
|
||||
import cn.hutool.log.LogFactory;
|
||||
import com.example.common.Result_;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
// 全局异常处理器 - 统一处理控制器层异常
|
||||
@ControllerAdvice(basePackages="com.example.controller")
|
||||
public class GlobalExceptionHandler {
|
||||
// 日志记录器
|
||||
private static final Log log = LogFactory.get();
|
||||
|
||||
/**
|
||||
* 统一异常处理@ExceptionHandler,主要用于处理所有Exception异常
|
||||
* @param request HTTP请求对象
|
||||
* @param e 异常对象
|
||||
* @return 统一的错误结果
|
||||
*/
|
||||
@ExceptionHandler(Exception.class)
|
||||
@ResponseBody // 返回json串
|
||||
public Result_ error(HttpServletRequest request, Exception e){
|
||||
// 记录异常日志,便于排查问题
|
||||
log.error("异常信息:", e);
|
||||
// 返回通用的系统错误信息(避免暴露敏感信息给前端)
|
||||
return Result_.error();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 处理自定义业务异常
|
||||
* @param request HTTP请求对象
|
||||
* @param e 自定义异常对象
|
||||
* @return 包含特定错误代码和信息的返回结果
|
||||
*/
|
||||
@ExceptionHandler(CustomException.class)
|
||||
@ResponseBody // 返回json串
|
||||
public Result_ customError(HttpServletRequest request, CustomException e){
|
||||
// 直接返回自定义异常中的错误代码和信息
|
||||
return Result_.error(e.getCode(), e.getMsg());
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,24 @@
|
||||
package com.example.mapper;
|
||||
|
||||
import com.example.entity.Address;
|
||||
import java.util.List;
|
||||
|
||||
// 地址数据访问接口
|
||||
public interface AddressMapper {
|
||||
|
||||
// 新增地址
|
||||
int insert(Address address);
|
||||
|
||||
// 根据ID删除地址
|
||||
int deleteById(Integer id);
|
||||
|
||||
// 根据ID修改地址
|
||||
int updateById(Address address);
|
||||
|
||||
// 根据ID查询地址
|
||||
Address selectById(Integer id);
|
||||
|
||||
// 查询所有地址(可条件查询)
|
||||
List<Address> selectAll(Address address);
|
||||
|
||||
}
|
||||
@ -0,0 +1,28 @@
|
||||
package com.example.mapper;
|
||||
|
||||
import com.example.entity.Admin;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
import java.util.List;
|
||||
|
||||
// 管理员数据访问接口
|
||||
public interface AdminMapper {
|
||||
|
||||
// 新增管理员
|
||||
int insert(Admin admin);
|
||||
|
||||
// 根据ID删除管理员
|
||||
int deleteById(Integer id);
|
||||
|
||||
// 根据ID修改管理员信息
|
||||
int updateById(Admin admin);
|
||||
|
||||
// 根据ID查询管理员
|
||||
Admin selectById(Integer id);
|
||||
|
||||
// 查询所有管理员(可条件查询)
|
||||
List<Admin> selectAll(Admin admin);
|
||||
|
||||
// 根据用户名查询管理员(用于登录验证)
|
||||
@Select("select * from admin where username = #{username}")
|
||||
Admin selectByUsername(String username);
|
||||
}
|
||||
@ -0,0 +1,28 @@
|
||||
package com.example.mapper;
|
||||
|
||||
import com.example.entity.Business;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
import java.util.List;
|
||||
|
||||
// 商家数据访问接口
|
||||
public interface BusinessMapper {
|
||||
|
||||
// 新增商家
|
||||
int insert(Business business);
|
||||
|
||||
// 根据ID删除商家
|
||||
int deleteById(Integer id);
|
||||
|
||||
// 根据ID修改商家信息
|
||||
int updateById(Business business);
|
||||
|
||||
// 根据ID查询商家
|
||||
Business selectById(Integer id);
|
||||
|
||||
// 查询所有商家(可条件查询)
|
||||
List<Business> selectAll(Business business);
|
||||
|
||||
// 根据用户名查询商家(用于登录验证)
|
||||
@Select("select * from business where username = #{username}")
|
||||
Business selectByUsername(String username);
|
||||
}
|
||||
@ -0,0 +1,40 @@
|
||||
package com.example.mapper;
|
||||
|
||||
import com.example.entity.Goods;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
import java.util.List;
|
||||
|
||||
// 商品数据访问接口
|
||||
public interface GoodsMapper {
|
||||
|
||||
// 新增商品
|
||||
int insert(Goods goods);
|
||||
|
||||
// 根据ID删除商品
|
||||
int deleteById(Integer id);
|
||||
|
||||
// 根据ID修改商品
|
||||
int updateById(Goods goods);
|
||||
|
||||
// 根据ID查询商品
|
||||
Goods selectById(Integer id);
|
||||
|
||||
// 查询所有商品(可条件查询)
|
||||
List<Goods> selectAll(Goods goods);
|
||||
|
||||
// 通过分类查询商品
|
||||
@Select("select * from goods where type_id = #{id}")
|
||||
List<Goods> selectByTypeId(Integer id);
|
||||
|
||||
// 根据商家ID查询该商家的所有商品
|
||||
@Select("select * from goods where business_id = #{id}")
|
||||
List<Goods> selectByBusinessId(Integer id);
|
||||
|
||||
// 查询销量前15的商品
|
||||
@Select("select * from goods order by count desc limit 15")
|
||||
List<Goods> selectTop15();
|
||||
|
||||
// 根据商品名称搜索商品
|
||||
@Select("select * from goods where name like concat('%', #{name}, '%')")
|
||||
List<Goods> selectByName(String name);
|
||||
}
|
||||
@ -0,0 +1,24 @@
|
||||
package com.example.mapper;
|
||||
|
||||
import com.example.entity.Notice;
|
||||
import java.util.List;
|
||||
|
||||
// 公告数据访问接口
|
||||
public interface NoticeMapper {
|
||||
|
||||
// 新增公告
|
||||
int insert(Notice notice);
|
||||
|
||||
// 根据ID删除公告
|
||||
int deleteById(Integer id);
|
||||
|
||||
// 根据ID修改公告
|
||||
int updateById(Notice notice);
|
||||
|
||||
// 根据ID查询公告
|
||||
Notice selectById(Integer id);
|
||||
|
||||
// 查询所有公告(可条件查询)
|
||||
List<Notice> selectAll(Notice notice);
|
||||
|
||||
}
|
||||
@ -0,0 +1,26 @@
|
||||
package com.example.mapper;
|
||||
|
||||
import com.example.entity.Orders;
|
||||
import java.util.List;
|
||||
|
||||
// 订单数据访问接口
|
||||
public interface OrdersMapper {
|
||||
|
||||
// 新增订单
|
||||
int insert(Orders orders);
|
||||
|
||||
// 根据ID删除订单
|
||||
int deleteById(Integer id);
|
||||
|
||||
// 根据ID修改订单
|
||||
int updateById(Orders orders);
|
||||
|
||||
// 根据ID查询订单
|
||||
Orders selectById(Integer id);
|
||||
|
||||
// 查询所有订单(可条件查询)
|
||||
List<Orders> selectAll(Orders orders);
|
||||
|
||||
// 查询所有已完成订单
|
||||
List<Orders> selectAllOKOrders();
|
||||
}
|
||||
@ -0,0 +1,24 @@
|
||||
package com.example.mapper;
|
||||
|
||||
import com.example.entity.Type;
|
||||
import java.util.List;
|
||||
|
||||
// 商品分类数据访问接口
|
||||
public interface TypeMapper {
|
||||
|
||||
// 新增分类
|
||||
int insert(Type type);
|
||||
|
||||
// 根据ID删除分类
|
||||
int deleteById(Integer id);
|
||||
|
||||
// 根据ID修改分类
|
||||
int updateById(Type type);
|
||||
|
||||
// 根据ID查询分类
|
||||
Type selectById(Integer id);
|
||||
|
||||
// 查询所有分类(可条件查询)
|
||||
List<Type> selectAll(Type type);
|
||||
|
||||
}
|
||||
@ -0,0 +1,28 @@
|
||||
package com.example.mapper;
|
||||
|
||||
import com.example.entity.User;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
import java.util.List;
|
||||
|
||||
// 用户数据访问接口
|
||||
public interface UserMapper {
|
||||
|
||||
// 新增用户
|
||||
int insert(User user);
|
||||
|
||||
// 根据ID删除用户
|
||||
int deleteById(Integer id);
|
||||
|
||||
// 根据ID修改用户
|
||||
int updateById(User user);
|
||||
|
||||
// 根据ID查询用户
|
||||
User selectById(Integer id);
|
||||
|
||||
// 查询所有用户(可条件查询)
|
||||
List<User> selectAll(User user);
|
||||
|
||||
// 根据用户名查询用户(用于登录验证)
|
||||
@Select("select * from user where username = #{username}")
|
||||
User selectByUsername(String username);
|
||||
}
|
||||
@ -0,0 +1,70 @@
|
||||
package com.example.service;
|
||||
|
||||
import com.example.common.enums.RoleEnum;
|
||||
import com.example.entity.Account;
|
||||
import com.example.entity.Address;
|
||||
import com.example.mapper.AddressMapper;
|
||||
import com.example.utils.TokenUtils;
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
// 功能:地址业务处理
|
||||
@Service
|
||||
public class AddressService {
|
||||
|
||||
@Resource
|
||||
private AddressMapper addressMapper;
|
||||
|
||||
// 新增地址
|
||||
public void add(Address address) {
|
||||
addressMapper.insert(address);
|
||||
}
|
||||
|
||||
// 根据ID删除地址
|
||||
public void deleteById(Integer id) {
|
||||
addressMapper.deleteById(id);
|
||||
}
|
||||
|
||||
// 批量删除地址
|
||||
public void deleteBatch(List<Integer> ids) {
|
||||
for (Integer id : ids) {
|
||||
addressMapper.deleteById(id);
|
||||
}
|
||||
}
|
||||
|
||||
// 修改地址
|
||||
public void updateById(Address address) {
|
||||
addressMapper.updateById(address);
|
||||
}
|
||||
|
||||
// 根据ID查询地址
|
||||
public Address selectById(Integer id) {
|
||||
return addressMapper.selectById(id);
|
||||
}
|
||||
|
||||
// 查询所有地址
|
||||
public List<Address> selectAll(Address address) {
|
||||
Account currentUser = TokenUtils.getCurrentUser();
|
||||
// 普通用户只能查询自己的地址
|
||||
if (RoleEnum.USER.name().equals(currentUser.getRole())) {
|
||||
address.setUserId(currentUser.getId());
|
||||
}
|
||||
return addressMapper.selectAll(address);
|
||||
}
|
||||
|
||||
// 分页查询地址
|
||||
public PageInfo<Address> selectPage(Address address, Integer pageNum, Integer pageSize) {
|
||||
Account currentUser = TokenUtils.getCurrentUser();
|
||||
// 普通用户只能查询自己的地址
|
||||
if (RoleEnum.USER.name().equals(currentUser.getRole())) {
|
||||
address.setUserId(currentUser.getId());
|
||||
}
|
||||
PageHelper.startPage(pageNum, pageSize);
|
||||
List<Address> list = addressMapper.selectAll(address);
|
||||
return PageInfo.of(list);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,127 @@
|
||||
package com.example.service;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.example.common.Constants;
|
||||
import com.example.common.enums.ResultCodeEnum;
|
||||
import com.example.common.enums.RoleEnum;
|
||||
import com.example.common.enums.StatusEnum;
|
||||
import com.example.entity.Account;
|
||||
import com.example.entity.Business;
|
||||
import com.example.exception.CustomException;
|
||||
import com.example.mapper.BusinessMapper;
|
||||
import com.example.utils.TokenUtils;
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
// 商家业务处理服务
|
||||
@Service
|
||||
public class BusinessService {
|
||||
|
||||
@Resource
|
||||
private BusinessMapper businessMapper;
|
||||
|
||||
// 新增商家
|
||||
public void add(Business business) {
|
||||
// 检查用户名是否已存在
|
||||
Business dbBusiness = businessMapper.selectByUsername(business.getUsername());
|
||||
if (ObjectUtil.isNotNull(dbBusiness)) {
|
||||
throw new CustomException(ResultCodeEnum.USER_EXIST_ERROR);
|
||||
}
|
||||
// 设置默认密码
|
||||
if (ObjectUtil.isEmpty(business.getPassword())) {
|
||||
business.setPassword(Constants.USER_DEFAULT_PASSWORD);
|
||||
}
|
||||
// 设置默认商家名称
|
||||
if (ObjectUtil.isEmpty(business.getName())) {
|
||||
business.setName(business.getUsername());
|
||||
}
|
||||
// 设置默认审核状态
|
||||
if (ObjectUtil.isEmpty(business.getStatus())) {
|
||||
business.setStatus(StatusEnum.CHECKING.status);
|
||||
}
|
||||
// 设置商家角色
|
||||
business.setRole(RoleEnum.BUSINESS.name());
|
||||
// 保存到数据库
|
||||
businessMapper.insert(business);
|
||||
}
|
||||
|
||||
// 根据ID删除商家
|
||||
public void deleteById(Integer id) {
|
||||
businessMapper.deleteById(id);
|
||||
}
|
||||
|
||||
// 批量删除商家
|
||||
public void deleteBatch(List<Integer> ids) {
|
||||
for (Integer id : ids) {
|
||||
businessMapper.deleteById(id);
|
||||
}
|
||||
}
|
||||
|
||||
// 修改商家信息
|
||||
public void updateById(Business business) {
|
||||
businessMapper.updateById(business);
|
||||
}
|
||||
|
||||
// 根据ID查询商家
|
||||
public Business selectById(Integer id) {
|
||||
return businessMapper.selectById(id);
|
||||
}
|
||||
|
||||
// 查询所有商家
|
||||
public List<Business> selectAll(Business business) {
|
||||
return businessMapper.selectAll(business);
|
||||
}
|
||||
|
||||
// 分页查询商家
|
||||
public PageInfo<Business> selectPage(Business business, Integer pageNum, Integer pageSize) {
|
||||
PageHelper.startPage(pageNum, pageSize);
|
||||
List<Business> list = businessMapper.selectAll(business);
|
||||
return PageInfo.of(list);
|
||||
}
|
||||
|
||||
// 商家登录
|
||||
public Account login(Account account) {
|
||||
// 验证用户名是否存在
|
||||
Account dbBusiness = businessMapper.selectByUsername(account.getUsername());
|
||||
if (ObjectUtil.isNull(dbBusiness)) {
|
||||
throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
|
||||
}
|
||||
// 验证密码是否正确
|
||||
if (!account.getPassword().equals(dbBusiness.getPassword())) {
|
||||
throw new CustomException(ResultCodeEnum.USER_ACCOUNT_ERROR);
|
||||
}
|
||||
// 生成登录token
|
||||
String tokenData = dbBusiness.getId() + "-" + RoleEnum.BUSINESS.name();
|
||||
String token = TokenUtils.createToken(tokenData, dbBusiness.getPassword());
|
||||
dbBusiness.setToken(token);
|
||||
return dbBusiness;
|
||||
}
|
||||
|
||||
// 商家注册
|
||||
public void register(Account account) {
|
||||
Business business = new Business();
|
||||
BeanUtils.copyProperties(account, business);
|
||||
add(business);
|
||||
}
|
||||
|
||||
// 修改密码
|
||||
public void updatePassword(Account account) {
|
||||
// 验证用户是否存在
|
||||
Business dbBusiness = businessMapper.selectByUsername(account.getUsername());
|
||||
if (ObjectUtil.isNull(dbBusiness)) {
|
||||
throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
|
||||
}
|
||||
// 验证原密码是否正确
|
||||
if (!account.getPassword().equals(dbBusiness.getPassword())) {
|
||||
throw new CustomException(ResultCodeEnum.PARAM_PASSWORD_ERROR);
|
||||
}
|
||||
// 更新密码
|
||||
dbBusiness.setPassword(account.getNewPassword());
|
||||
businessMapper.updateById(dbBusiness);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,76 @@
|
||||
package com.example.service;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.example.common.enums.RoleEnum;
|
||||
import com.example.entity.Account;
|
||||
import com.example.entity.Cart;
|
||||
import com.example.mapper.CartMapper;
|
||||
import com.example.utils.TokenUtils;
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
// 功能:购物车业务处理
|
||||
|
||||
@Service
|
||||
public class CartService {
|
||||
|
||||
@Resource
|
||||
private CartMapper cartMapper;
|
||||
|
||||
// 新增商品到购物车
|
||||
public void add(Cart cart) {
|
||||
// 检查用户是否已添加过该商品
|
||||
Cart dbCart = cartMapper.selectByUserIdAndGoodsId(cart.getUserId(), cart.getGoodsId());
|
||||
if (ObjectUtil.isNotEmpty(dbCart)) {
|
||||
// 已存在:数量+1
|
||||
dbCart.setNum(dbCart.getNum() + 1);
|
||||
cartMapper.updateById(dbCart);
|
||||
} else {
|
||||
// 不存在:新增记录
|
||||
cartMapper.insert(cart);
|
||||
}
|
||||
}
|
||||
|
||||
// 根据ID删除购物车商品
|
||||
public void deleteById(Integer id) {
|
||||
cartMapper.deleteById(id);
|
||||
}
|
||||
|
||||
// 批量删除购物车商品
|
||||
public void deleteBatch(List<Integer> ids) {
|
||||
for (Integer id : ids) {
|
||||
cartMapper.deleteById(id);
|
||||
}
|
||||
}
|
||||
|
||||
// 修改购物车商品信息
|
||||
public void updateById(Cart cart) {
|
||||
cartMapper.updateById(cart);
|
||||
}
|
||||
|
||||
// 根据ID查询购物车商品
|
||||
public Cart selectById(Integer id) {
|
||||
return cartMapper.selectById(id);
|
||||
}
|
||||
|
||||
// 查询所有购物车商品
|
||||
public List<Cart> selectAll(Cart cart) {
|
||||
return cartMapper.selectAll(cart);
|
||||
}
|
||||
|
||||
// 分页查询购物车商品
|
||||
public PageInfo<Cart> selectPage(Cart cart, Integer pageNum, Integer pageSize) {
|
||||
// 权限控制:普通用户只能查看自己的购物车
|
||||
Account currentUser = TokenUtils.getCurrentUser();
|
||||
if (RoleEnum.USER.name().equals(currentUser.getRole())) {
|
||||
cart.setUserId(currentUser.getId());
|
||||
}
|
||||
PageHelper.startPage(pageNum, pageSize);
|
||||
List<Cart> list = cartMapper.selectAll(cart);
|
||||
return PageInfo.of(list);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,75 @@
|
||||
package com.example.service;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.example.common.enums.ResultCodeEnum;
|
||||
import com.example.common.enums.RoleEnum;
|
||||
import com.example.entity.Account;
|
||||
import com.example.entity.Collect;
|
||||
import com.example.exception.CustomException;
|
||||
import com.example.mapper.CollectMapper;
|
||||
import com.example.utils.TokenUtils;
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
// 功能:收藏业务处理
|
||||
@Service
|
||||
public class CollectService {
|
||||
|
||||
@Resource
|
||||
private CollectMapper collectMapper;
|
||||
|
||||
// 新增收藏
|
||||
public void add(Collect collect) {
|
||||
// 检查是否已收藏该商品
|
||||
Collect dbCollect = collectMapper.selectByUserIdAndGoodsId(collect.getUserId(), collect.getGoodsId());
|
||||
if (ObjectUtil.isNotEmpty(dbCollect)) {
|
||||
// 已收藏:提示不能重复收藏
|
||||
throw new CustomException(ResultCodeEnum.COLLECT_ALREADY_ERROR);
|
||||
}
|
||||
// 新增收藏记录
|
||||
collectMapper.insert(collect);
|
||||
}
|
||||
|
||||
// 根据ID删除收藏
|
||||
public void deleteById(Integer id) {
|
||||
collectMapper.deleteById(id);
|
||||
}
|
||||
|
||||
// 批量删除收藏
|
||||
public void deleteBatch(List<Integer> ids) {
|
||||
for (Integer id : ids) {
|
||||
collectMapper.deleteById(id);
|
||||
}
|
||||
}
|
||||
|
||||
// 修改收藏信息
|
||||
public void updateById(Collect collect) {
|
||||
collectMapper.updateById(collect);
|
||||
}
|
||||
|
||||
// 根据ID查询收藏
|
||||
public Collect selectById(Integer id) {
|
||||
return collectMapper.selectById(id);
|
||||
}
|
||||
|
||||
// 查询所有收藏
|
||||
public List<Collect> selectAll(Collect collect) {
|
||||
return collectMapper.selectAll(collect);
|
||||
}
|
||||
|
||||
// 分页查询收藏
|
||||
public PageInfo<Collect> selectPage(Collect collect, Integer pageNum, Integer pageSize) {
|
||||
// 权限控制:普通用户只能查看自己的收藏
|
||||
Account currentUser = TokenUtils.getCurrentUser();
|
||||
if (RoleEnum.USER.name().equals(currentUser.getRole())) {
|
||||
collect.setUserId(currentUser.getId());
|
||||
}
|
||||
PageHelper.startPage(pageNum, pageSize);
|
||||
List<Collect> list = collectMapper.selectAll(collect);
|
||||
return PageInfo.of(list);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,64 @@
|
||||
package com.example.service;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.example.entity.Account;
|
||||
import com.example.entity.Notice;
|
||||
import com.example.mapper.NoticeMapper;
|
||||
import com.example.utils.TokenUtils;
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import javax.annotation.Resource;
|
||||
import org.springframework.stereotype.Service;
|
||||
import java.util.List;
|
||||
|
||||
// 公告信息表业务处理
|
||||
@Service
|
||||
public class NoticeService {
|
||||
|
||||
@Resource
|
||||
private NoticeMapper noticeMapper;
|
||||
|
||||
// 新增公告
|
||||
public void add(Notice notice) {
|
||||
// 自动设置公告日期为今天
|
||||
notice.setTime(DateUtil.today());
|
||||
// 自动设置发布人为当前登录用户
|
||||
Account currentUser = TokenUtils.getCurrentUser();
|
||||
notice.setUser(currentUser.getUsername());
|
||||
noticeMapper.insert(notice);
|
||||
}
|
||||
|
||||
// 根据ID删除公告
|
||||
public void deleteById(Integer id) {
|
||||
noticeMapper.deleteById(id);
|
||||
}
|
||||
|
||||
// 批量删除公告
|
||||
public void deleteBatch(List<Integer> ids) {
|
||||
for (Integer id : ids) {
|
||||
noticeMapper.deleteById(id);
|
||||
}
|
||||
}
|
||||
|
||||
// 修改公告信息
|
||||
public void updateById(Notice notice) {
|
||||
noticeMapper.updateById(notice);
|
||||
}
|
||||
|
||||
// 根据ID查询公告
|
||||
public Notice selectById(Integer id) {
|
||||
return noticeMapper.selectById(id);
|
||||
}
|
||||
|
||||
// 查询所有公告
|
||||
public List<Notice> selectAll(Notice notice) {
|
||||
return noticeMapper.selectAll(notice);
|
||||
}
|
||||
|
||||
// 分页查询公告
|
||||
public PageInfo<Notice> selectPage(Notice notice, Integer pageNum, Integer pageSize) {
|
||||
PageHelper.startPage(pageNum, pageSize);
|
||||
List<Notice> list = noticeMapper.selectAll(notice);
|
||||
return PageInfo.of(list);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,57 @@
|
||||
package com.example.service;
|
||||
|
||||
import com.example.entity.Type;
|
||||
import com.example.mapper.TypeMapper;
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
// 功能:分类信息表处理业务
|
||||
@Service
|
||||
public class TypeService {
|
||||
|
||||
@Resource
|
||||
private TypeMapper typeMapper;
|
||||
|
||||
// 新增分类
|
||||
public void add(Type type) {
|
||||
typeMapper.insert(type);
|
||||
}
|
||||
|
||||
// 根据ID删除分类
|
||||
public void deleteById(Integer id) {
|
||||
typeMapper.deleteById(id);
|
||||
}
|
||||
|
||||
// 批量删除分类
|
||||
public void deleteBatch(List<Integer> ids) {
|
||||
for (Integer id : ids) {
|
||||
typeMapper.deleteById(id);
|
||||
}
|
||||
}
|
||||
|
||||
// 修改分类信息
|
||||
public void updateById(Type type) {
|
||||
typeMapper.updateById(type);
|
||||
}
|
||||
|
||||
// 根据ID查询分类
|
||||
public Type selectById(Integer id) {
|
||||
return typeMapper.selectById(id);
|
||||
}
|
||||
|
||||
// 查询所有分类
|
||||
public List<Type> selectAll(Type type) {
|
||||
return typeMapper.selectAll(type);
|
||||
}
|
||||
|
||||
// 分页查询分类
|
||||
public PageInfo<Type> selectPage(Type type, Integer pageNum, Integer pageSize) {
|
||||
PageHelper.startPage(pageNum, pageSize);
|
||||
List<Type> list = typeMapper.selectAll(type);
|
||||
return PageInfo.of(list);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,121 @@
|
||||
package com.example.service;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.example.common.Constants;
|
||||
import com.example.common.enums.ResultCodeEnum;
|
||||
import com.example.common.enums.RoleEnum;
|
||||
import com.example.entity.Account;
|
||||
import com.example.entity.User;
|
||||
import com.example.exception.CustomException;
|
||||
import com.example.mapper.UserMapper;
|
||||
import com.example.utils.TokenUtils;
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import com.github.pagehelper.PageInfo;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
// 功能:普通用户业务处理
|
||||
@Service
|
||||
public class UserService {
|
||||
|
||||
@Resource
|
||||
private UserMapper userMapper;
|
||||
|
||||
// 新增用户
|
||||
public void add(User user) {
|
||||
// 检查用户名是否已存在
|
||||
User dbUser = userMapper.selectByUsername(user.getUsername());
|
||||
if (ObjectUtil.isNotNull(dbUser)) {
|
||||
throw new CustomException(ResultCodeEnum.USER_EXIST_ERROR);
|
||||
}
|
||||
// 设置默认密码
|
||||
if (ObjectUtil.isEmpty(user.getPassword())) {
|
||||
user.setPassword(Constants.USER_DEFAULT_PASSWORD);
|
||||
}
|
||||
// 设置默认姓名
|
||||
if (ObjectUtil.isEmpty(user.getName())) {
|
||||
user.setName(user.getUsername());
|
||||
}
|
||||
// 设置用户角色
|
||||
user.setRole(RoleEnum.USER.name());
|
||||
userMapper.insert(user);
|
||||
}
|
||||
|
||||
// 根据ID删除用户
|
||||
public void deleteById(Integer id) {
|
||||
userMapper.deleteById(id);
|
||||
}
|
||||
|
||||
// 批量删除用户
|
||||
public void deleteBatch(List<Integer> ids) {
|
||||
for (Integer id : ids) {
|
||||
userMapper.deleteById(id);
|
||||
}
|
||||
}
|
||||
|
||||
// 修改用户信息
|
||||
public void updateById(User user) {
|
||||
userMapper.updateById(user);
|
||||
}
|
||||
|
||||
// 根据ID查询用户
|
||||
public User selectById(Integer id) {
|
||||
return userMapper.selectById(id);
|
||||
}
|
||||
|
||||
// 查询所有用户
|
||||
public List<User> selectAll(User user) {
|
||||
return userMapper.selectAll(user);
|
||||
}
|
||||
|
||||
// 分页查询用户
|
||||
public PageInfo<User> selectPage(User user, Integer pageNum, Integer pageSize) {
|
||||
PageHelper.startPage(pageNum, pageSize);
|
||||
List<User> list = userMapper.selectAll(user);
|
||||
return PageInfo.of(list);
|
||||
}
|
||||
|
||||
// 用户登录
|
||||
public Account login(Account account) {
|
||||
// 验证用户名是否存在
|
||||
Account dbUser = userMapper.selectByUsername(account.getUsername());
|
||||
if (ObjectUtil.isNull(dbUser)) {
|
||||
throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
|
||||
}
|
||||
// 验证密码是否正确
|
||||
if (!account.getPassword().equals(dbUser.getPassword())) {
|
||||
throw new CustomException(ResultCodeEnum.USER_ACCOUNT_ERROR);
|
||||
}
|
||||
// 生成登录token
|
||||
String tokenData = dbUser.getId() + "-" + RoleEnum.USER.name();
|
||||
String token = TokenUtils.createToken(tokenData, dbUser.getPassword());
|
||||
dbUser.setToken(token);
|
||||
return dbUser;
|
||||
}
|
||||
|
||||
// 用户注册
|
||||
public void register(Account account) {
|
||||
User user = new User();
|
||||
BeanUtils.copyProperties(account, user);
|
||||
add(user);
|
||||
}
|
||||
|
||||
// 修改密码
|
||||
public void updatePassword(Account account) {
|
||||
// 验证用户是否存在
|
||||
User dbUser = userMapper.selectByUsername(account.getUsername());
|
||||
if (ObjectUtil.isNull(dbUser)) {
|
||||
throw new CustomException(ResultCodeEnum.USER_NOT_EXIST_ERROR);
|
||||
}
|
||||
// 验证原密码是否正确
|
||||
if (!account.getPassword().equals(dbUser.getPassword())) {
|
||||
throw new CustomException(ResultCodeEnum.PARAM_PASSWORD_ERROR);
|
||||
}
|
||||
// 更新密码
|
||||
dbUser.setPassword(account.getNewPassword());
|
||||
userMapper.updateById(dbUser);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,92 @@
|
||||
package com.example.utils;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.auth0.jwt.JWT;
|
||||
import com.auth0.jwt.algorithms.Algorithm;
|
||||
import com.example.common.Constants;
|
||||
import com.example.common.enums.RoleEnum;
|
||||
import com.example.entity.Account;
|
||||
import com.example.service.AdminService;
|
||||
import com.example.service.BusinessService;
|
||||
import com.example.service.UserService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.Date;
|
||||
|
||||
// Token工具类 - 负责JWT令牌的生成和验证
|
||||
@Component
|
||||
public class TokenUtils {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(TokenUtils.class);
|
||||
|
||||
// 静态服务对象 - 用于在静态方法中调用服务
|
||||
private static AdminService staticAdminService;
|
||||
private static BusinessService staticBusinessService;
|
||||
private static UserService staticUserService;
|
||||
|
||||
@Resource
|
||||
AdminService adminService;
|
||||
|
||||
@Resource
|
||||
BusinessService businessService;
|
||||
|
||||
@Resource
|
||||
UserService userService;
|
||||
|
||||
// 初始化静态服务对象 - Spring容器启动后自动执行
|
||||
@PostConstruct
|
||||
public void setUserService() {
|
||||
staticAdminService = adminService;
|
||||
staticBusinessService = businessService;
|
||||
staticUserService = userService;
|
||||
}
|
||||
|
||||
// 生成JWT Token
|
||||
public static String createToken(String data, String sign) {
|
||||
return JWT.create()
|
||||
.withAudience(data) // 将userId-role保存到token中
|
||||
.withExpiresAt(DateUtil.offsetHour(new Date(), 2)) // 2小时后token过期
|
||||
.sign(Algorithm.HMAC256(sign)); // 用密码作为密钥签名
|
||||
}
|
||||
|
||||
// 获取当前登录的用户信息
|
||||
public static Account getCurrentUser() {
|
||||
try {
|
||||
// 获取当前HTTP请求
|
||||
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
||||
// 从请求头获取Token
|
||||
String token = request.getHeader(Constants.TOKEN);
|
||||
|
||||
if (ObjectUtil.isNotEmpty(token)) {
|
||||
// 解析Token获取用户信息
|
||||
String userRole = JWT.decode(token).getAudience().get(0);
|
||||
String userId = userRole.split("-")[0]; // 提取用户ID
|
||||
String role = userRole.split("-")[1]; // 提取用户角色
|
||||
|
||||
// 根据角色类型返回对应的用户信息
|
||||
if (RoleEnum.ADMIN.name().equals(role)) {
|
||||
return staticAdminService.selectById(Integer.valueOf(userId));
|
||||
}
|
||||
if (RoleEnum.BUSINESS.name().equals(role)) {
|
||||
return staticBusinessService.selectById(Integer.valueOf(userId));
|
||||
}
|
||||
if (RoleEnum.USER.name().equals(role)) {
|
||||
return staticUserService.selectById(Integer.valueOf(userId));
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// 记录错误日志
|
||||
log.error("获取当前用户信息出错", e);
|
||||
}
|
||||
// 返回空的账号对象(未登录状态)
|
||||
return new Account();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,31 @@
|
||||
server:
|
||||
port: 9090
|
||||
|
||||
# 数据库配置
|
||||
spring:
|
||||
datasource:
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
username: root #你本地的数据库用户名
|
||||
password: Hjy041213 #你本地的数据库密码
|
||||
url: jdbc:mysql://localhost:3306/campus?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT%2b8&allowPublicKeyRetrieval=true
|
||||
servlet:
|
||||
multipart:
|
||||
max-file-size: 100MB
|
||||
max-request-size: 100MB
|
||||
|
||||
# 配置mybatis实体和xml映射
|
||||
mybatis:
|
||||
mapper-locations: classpath:mapper/*.xml
|
||||
type-aliases-package: com.example.entity
|
||||
configuration:
|
||||
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
|
||||
map-underscore-to-camel-case: true
|
||||
|
||||
# 分页
|
||||
pagehelper:
|
||||
helper-dialect: mysql
|
||||
reasonable: true
|
||||
support-methods-arguments: true
|
||||
params: count=countSql
|
||||
|
||||
ip: localhost
|
||||
@ -0,0 +1,86 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<!-- 购物车表数据映射文件 -->
|
||||
<mapper namespace="com.example.mapper.CartMapper">
|
||||
|
||||
<!-- 定义购物车表基础字段列表 -->
|
||||
<sql id="Base_Column_List">
|
||||
id,user_id,business_id,goods_id,num
|
||||
</sql>
|
||||
|
||||
<!-- 查询所有购物车商品(关联查询商家和商品信息) -->
|
||||
<select id="selectAll" resultType="com.example.entity.Cart">
|
||||
select cart.*,
|
||||
business.name as businessName, <!-- 商家名称 -->
|
||||
goods.img as goodsImg, <!-- 商品图片 -->
|
||||
goods.name as goodsName, <!-- 商品名称 -->
|
||||
goods.price as goodsPrice, <!-- 商品价格 -->
|
||||
goods.unit as goodUnit <!-- 商品单位 -->
|
||||
from cart
|
||||
left join business on cart.business_id = business.id <!-- 关联商家表 -->
|
||||
left join goods on cart.goods_id = goods.id <!-- 关联商品表 -->
|
||||
<where>
|
||||
<if test="id != null"> and id= #{id}</if>
|
||||
<if test="userId != null"> and user_id = #{userId}</if>
|
||||
<if test="businessId != null"> and business_id = #{businessId}</if>
|
||||
<if test="goodsId != null"> and goods_id = #{goodsId}</if>
|
||||
<if test="num != null"> and num = #{num}</if>
|
||||
</where>
|
||||
</select>
|
||||
|
||||
<!-- 根据ID查询单个购物车商品 -->
|
||||
<select id="selectById" resultType="com.example.entity.Cart">
|
||||
select
|
||||
<include refid="Base_Column_List" />
|
||||
from cart
|
||||
where id = #{id}
|
||||
</select>
|
||||
|
||||
<!-- 根据ID删除购物车商品 -->
|
||||
<delete id="deleteById">
|
||||
delete from cart
|
||||
where id = #{id}
|
||||
</delete>
|
||||
|
||||
<!-- 新增购物车商品 -->
|
||||
<insert id="insert" parameterType="com.example.entity.Cart" useGeneratedKeys="true">
|
||||
insert into cart
|
||||
<trim prefix="(" suffix=")" suffixOverrides=",">
|
||||
<if test="id != null">id,</if>
|
||||
<if test="userId != null">user_id,</if>
|
||||
<if test="businessId != null">business_id,</if>
|
||||
<if test="goodsId != null">goods_id,</if>
|
||||
<if test="num != null">num,</if>
|
||||
</trim>
|
||||
<trim prefix="values (" suffix=")" suffixOverrides=",">
|
||||
<if test="id != null">#{id},</if>
|
||||
<if test="userId != null">#{userId},</if>
|
||||
<if test="businessId != null">#{businessId},</if>
|
||||
<if test="goodsId != null">#{goodsId},</if>
|
||||
<if test="num != null">#{num},</if>
|
||||
</trim>
|
||||
</insert>
|
||||
|
||||
<!-- 根据ID修改购物车商品信息 -->
|
||||
<update id="updateById" parameterType="com.example.entity.Cart">
|
||||
update cart
|
||||
<set>
|
||||
<if test="userId != null">
|
||||
user_id = #{userId},
|
||||
</if>
|
||||
<if test="businessId != null">
|
||||
business_id = #{businessId},
|
||||
</if>
|
||||
<if test="goodsId != null">
|
||||
goods_id = #{goodsId},
|
||||
</if>
|
||||
<if test="num != null">
|
||||
num = #{num}, <!-- 商品数量 -->
|
||||
</if>
|
||||
</set>
|
||||
where id = #{id}
|
||||
</update>
|
||||
|
||||
</mapper>
|
||||
@ -0,0 +1,80 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<!-- 收藏表数据映射文件 -->
|
||||
<mapper namespace="com.example.mapper.CollectMapper">
|
||||
|
||||
<!-- 定义收藏表基础字段列表 -->
|
||||
<sql id="Base_Column_List">
|
||||
id,user_id,business_id,goods_id
|
||||
</sql>
|
||||
|
||||
<!-- 查询所有收藏记录(关联查询商家和商品信息) -->
|
||||
<select id="selectAll" resultType="com.example.entity.Collect">
|
||||
select collect.*,
|
||||
business.name as businessName, <!-- 商家名称 -->
|
||||
goods.img as goodsImg, <!-- 商品图片 -->
|
||||
goods.name as goodsName, <!-- 商品名称 -->
|
||||
goods.price as goodsPrice, <!-- 商品价格 -->
|
||||
goods.unit as goodUnit <!-- 商品单位 -->
|
||||
from collect
|
||||
left join business on collect.business_id = business.id <!-- 关联商家表 -->
|
||||
left join goods on collect.goods_id = goods.id <!-- 关联商品表 -->
|
||||
<where>
|
||||
<if test="id != null"> and id= #{id}</if>
|
||||
<if test="userId != null"> and user_id = #{userId}</if>
|
||||
<if test="businessId != null"> and business_id = #{businessId}</if>
|
||||
<if test="goodsId != null"> and goods_id = #{goodsId}</if>
|
||||
</where>
|
||||
</select>
|
||||
|
||||
<!-- 根据ID查询单个收藏记录 -->
|
||||
<select id="selectById" resultType="com.example.entity.Collect">
|
||||
select
|
||||
<include refid="Base_Column_List" />
|
||||
from collect
|
||||
where id = #{id}
|
||||
</select>
|
||||
|
||||
<!-- 根据ID删除收藏记录 -->
|
||||
<delete id="deleteById">
|
||||
delete from collect
|
||||
where id = #{id}
|
||||
</delete>
|
||||
|
||||
<!-- 新增收藏记录 -->
|
||||
<insert id="insert" parameterType="com.example.entity.Collect" useGeneratedKeys="true">
|
||||
insert into collect
|
||||
<trim prefix="(" suffix=")" suffixOverrides=",">
|
||||
<if test="id != null">id,</if>
|
||||
<if test="userId != null">user_id,</if>
|
||||
<if test="businessId != null">business_id,</if>
|
||||
<if test="goodsId != null">goods_id,</if>
|
||||
</trim>
|
||||
<trim prefix="values (" suffix=")" suffixOverrides=",">
|
||||
<if test="id != null">#{id},</if>
|
||||
<if test="userId != null">#{userId},</if>
|
||||
<if test="businessId != null">#{businessId},</if>
|
||||
<if test="goodsId != null">#{goodsId},</if>
|
||||
</trim>
|
||||
</insert>
|
||||
|
||||
<!-- 根据ID修改收藏记录 -->
|
||||
<update id="updateById" parameterType="com.example.entity.Collect">
|
||||
update collect
|
||||
<set>
|
||||
<if test="userId != null">
|
||||
user_id = #{userId},
|
||||
</if>
|
||||
<if test="businessId != null">
|
||||
business_id = #{businessId},
|
||||
</if>
|
||||
<if test="goodsId != null">
|
||||
goods_id = #{goodsId},
|
||||
</if>
|
||||
</set>
|
||||
where id = #{id}
|
||||
</update>
|
||||
|
||||
</mapper>
|
||||
@ -0,0 +1,94 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<!-- 评论表数据映射文件 -->
|
||||
<mapper namespace="com.example.mapper.CommentMapper">
|
||||
|
||||
<!-- 定义评论表基础字段列表 -->
|
||||
<sql id="Base_Column_List">
|
||||
id,user_id,business_id,goods_id,time,content
|
||||
</sql>
|
||||
|
||||
<!-- 查询所有评论(关联查询商家、商品和用户信息) -->
|
||||
<select id="selectAll" resultType="com.example.entity.Comment">
|
||||
select comment.*,
|
||||
business.name as businessName, <!-- 商家名称 -->
|
||||
goods.img as goodsImg, <!-- 商品图片 -->
|
||||
goods.name as goodsName, <!-- 商品名称 -->
|
||||
goods.price as goodsPrice, <!-- 商品价格 -->
|
||||
goods.unit as goodUnit, <!-- 商品单位 -->
|
||||
user.name as userName <!-- 用户昵称 -->
|
||||
from comment
|
||||
left join business on comment.business_id = business.id <!-- 关联商家表 -->
|
||||
left join goods on comment.goods_id = goods.id <!-- 关联商品表 -->
|
||||
left join user on comment.user_id = user.id <!-- 关联用户表 -->
|
||||
<where>
|
||||
<if test="id != null"> and id= #{id}</if>
|
||||
<if test="userId != null"> and user_id = #{userId}</if>
|
||||
<if test="businessId != null"> and comment.business_id = #{businessId}</if>
|
||||
<if test="goodsId != null"> and goods_id = #{goodsId}</if>
|
||||
<if test="time != null"> and time = #{time}</if>
|
||||
<if test="content != null"> and content like concat('%', #{content}, '%') </if> <!-- 评论内容模糊搜索 -->
|
||||
</where>
|
||||
</select>
|
||||
|
||||
<!-- 根据ID查询单个评论 -->
|
||||
<select id="selectById" resultType="com.example.entity.Comment">
|
||||
select
|
||||
<include refid="Base_Column_List" />
|
||||
from comment
|
||||
where id = #{id}
|
||||
</select>
|
||||
|
||||
<!-- 根据ID删除评论 -->
|
||||
<delete id="deleteById">
|
||||
delete from comment
|
||||
where id = #{id}
|
||||
</delete>
|
||||
|
||||
<!-- 新增评论 -->
|
||||
<insert id="insert" parameterType="com.example.entity.Comment" useGeneratedKeys="true">
|
||||
insert into comment
|
||||
<trim prefix="(" suffix=")" suffixOverrides=",">
|
||||
<if test="id != null">id,</if>
|
||||
<if test="userId != null">user_id,</if>
|
||||
<if test="businessId != null">business_id,</if>
|
||||
<if test="goodsId != null">goods_id,</if>
|
||||
<if test="time != null">time,</if>
|
||||
<if test="content != null">content,</if>
|
||||
</trim>
|
||||
<trim prefix="values (" suffix=")" suffixOverrides=",">
|
||||
<if test="id != null">#{id},</if>
|
||||
<if test="userId != null">#{userId},</if>
|
||||
<if test="businessId != null">#{businessId},</if>
|
||||
<if test="goodsId != null">#{goodsId},</if>
|
||||
<if test="time != null">#{time},</if>
|
||||
<if test="content != null">#{content},</if>
|
||||
</trim>
|
||||
</insert>
|
||||
|
||||
<!-- 根据ID修改评论信息 -->
|
||||
<update id="updateById" parameterType="com.example.entity.Comment">
|
||||
update comment
|
||||
<set>
|
||||
<if test="userId != null">
|
||||
user_id = #{userId},
|
||||
</if>
|
||||
<if test="businessId != null">
|
||||
business_id = #{businessId},
|
||||
</if>
|
||||
<if test="goodsId != null">
|
||||
goods_id = #{goodsId},
|
||||
</if>
|
||||
<if test="time != null">
|
||||
time = #{time},
|
||||
</if>
|
||||
<if test="content != null">
|
||||
content = #{content}, <!-- 评论内容 -->
|
||||
</if>
|
||||
</set>
|
||||
where id = #{id}
|
||||
</update>
|
||||
|
||||
</mapper>
|
||||
@ -0,0 +1,114 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<!-- 订单表数据映射文件 -->
|
||||
<mapper namespace="com.example.mapper.OrdersMapper">
|
||||
|
||||
<!-- 定义订单表基础字段列表 -->
|
||||
<sql id="Base_Column_List">
|
||||
id,user_id,business_id,goods_id,num,price,address_id,status,order_id
|
||||
</sql>
|
||||
|
||||
<!-- 查询所有订单(关联查询商家、商品和地址信息) -->
|
||||
<select id="selectAll" resultType="com.example.entity.Orders">
|
||||
select orders.*,
|
||||
business.name as businessName, <!-- 商家名称 -->
|
||||
goods.img as goodsImg, <!-- 商品图片 -->
|
||||
goods.name as goodsName, <!-- 商品名称 -->
|
||||
goods.price as goodsPrice, <!-- 商品单价 -->
|
||||
goods.unit as goodsUnit, <!-- 商品单位 -->
|
||||
address.username as username, <!-- 收货人姓名 -->
|
||||
address.useraddress as useraddress, <!-- 收货地址 -->
|
||||
address.phone as phone <!-- 收货人电话 -->
|
||||
from orders
|
||||
left join business on orders.business_id = business.id <!-- 关联商家表 -->
|
||||
left join goods on orders.goods_id = goods.id <!-- 关联商品表 -->
|
||||
left join address on orders.address_id = address.id <!-- 关联地址表 -->
|
||||
<where>
|
||||
<if test="id != null"> and id= #{id}</if>
|
||||
<if test="userId != null"> and orders.user_id = #{userId}</if>
|
||||
<if test="businessId != null"> and orders.business_id = #{businessId}</if>
|
||||
<if test="goodsId != null"> and goods_id = #{goodsId}</if>
|
||||
<if test="num != null"> and num = #{num}</if> <!-- 购买数量 -->
|
||||
<if test="price != null"> and price = #{price}</if> <!-- 订单金额 -->
|
||||
<if test="addressId != null"> and address_id = #{addressId}</if>
|
||||
<if test="status != null"> and status = #{status}</if> <!-- 订单状态 -->
|
||||
<if test="orderId != null"> and order_id = #{orderId}</if> <!-- 订单编号 -->
|
||||
</where>
|
||||
</select>
|
||||
|
||||
<!-- 根据ID查询单个订单 -->
|
||||
<select id="selectById" resultType="com.example.entity.Orders">
|
||||
select
|
||||
<include refid="Base_Column_List" />
|
||||
from orders
|
||||
where id = #{id}
|
||||
</select>
|
||||
|
||||
<!-- 根据ID删除订单 -->
|
||||
<delete id="deleteById">
|
||||
delete from orders
|
||||
where id = #{id}
|
||||
</delete>
|
||||
|
||||
<!-- 新增订单 -->
|
||||
<insert id="insert" parameterType="com.example.entity.Orders" useGeneratedKeys="true">
|
||||
insert into orders
|
||||
<trim prefix="(" suffix=")" suffixOverrides=",">
|
||||
<if test="id != null">id,</if>
|
||||
<if test="userId != null">user_id,</if>
|
||||
<if test="businessId != null">business_id,</if>
|
||||
<if test="goodsId != null">goods_id,</if>
|
||||
<if test="num != null">num,</if>
|
||||
<if test="price != null">price,</if>
|
||||
<if test="addressId != null">address_id,</if>
|
||||
<if test="status != null">status,</if>
|
||||
<if test="orderId != null">order_id,</if>
|
||||
</trim>
|
||||
<trim prefix="values (" suffix=")" suffixOverrides=",">
|
||||
<if test="id != null">#{id},</if>
|
||||
<if test="userId != null">#{userId},</if>
|
||||
<if test="businessId != null">#{businessId},</if>
|
||||
<if test="goodsId != null">#{goodsId},</if>
|
||||
<if test="num != null">#{num},</if>
|
||||
<if test="price != null">#{price},</if>
|
||||
<if test="addressId != null">#{addressId},</if>
|
||||
<if test="status != null">#{status},</if>
|
||||
<if test="orderId != null">#{orderId},</if>
|
||||
</trim>
|
||||
</insert>
|
||||
|
||||
<!-- 根据ID修改订单信息 -->
|
||||
<update id="updateById" parameterType="com.example.entity.Orders">
|
||||
update orders
|
||||
<set>
|
||||
<if test="userId != null">
|
||||
user_id = #{userId}, <!-- 用户ID -->
|
||||
</if>
|
||||
<if test="businessId != null">
|
||||
business_id = #{businessId}, <!-- 商家ID -->
|
||||
</if>
|
||||
<if test="goodsId != null">
|
||||
goods_id = #{goodsId}, <!-- 商品ID -->
|
||||
</if>
|
||||
<if test="num != null">
|
||||
num = #{num}, <!-- 购买数量 -->
|
||||
</if>
|
||||
<if test="price != null">
|
||||
price = #{price}, <!-- 订单金额 -->
|
||||
</if>
|
||||
<if test="addressId != null">
|
||||
address_id = #{addressId}, <!-- 地址ID -->
|
||||
</if>
|
||||
<if test="status != null">
|
||||
status = #{status}, <!-- 订单状态 -->
|
||||
</if>
|
||||
<if test="orderId != null">
|
||||
order_id = #{orderId}, <!-- 订单编号 -->
|
||||
</if>
|
||||
</set>
|
||||
where id = #{id}
|
||||
</update>
|
||||
|
||||
</mapper>
|
||||
@ -0,0 +1,98 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<!-- 用户表数据映射文件 -->
|
||||
<mapper namespace="com.example.mapper.UserMapper">
|
||||
|
||||
<!-- 定义用户表基础字段列表 -->
|
||||
<sql id="Base_Column_List">
|
||||
id,username,password,name,phone,email,avatar,role
|
||||
</sql>
|
||||
|
||||
<!-- 查询所有用户(支持条件查询) -->
|
||||
<select id="selectAll" resultType="com.example.entity.User">
|
||||
select
|
||||
<include refid="Base_Column_List" />
|
||||
from user
|
||||
<where>
|
||||
<if test="id != null"> and id= #{id}</if>
|
||||
<if test="username != null"> and username like concat('%', #{username}, '%')</if> <!-- 用户名模糊搜索 -->
|
||||
<if test="password != null"> and password= #{password}</if>
|
||||
<if test="name != null"> and name= #{name}</if>
|
||||
<if test="phone != null"> and phone= #{phone}</if>
|
||||
<if test="email != null"> and email= #{email}</if>
|
||||
<if test="avatar != null"> and avatar= #{avatar}</if>
|
||||
<if test="role != null"> and role= #{role}</if>
|
||||
</where>
|
||||
</select>
|
||||
|
||||
<!-- 根据ID查询单个用户 -->
|
||||
<select id="selectById" resultType="com.example.entity.User">
|
||||
select
|
||||
<include refid="Base_Column_List" />
|
||||
from user
|
||||
where id = #{id}
|
||||
</select>
|
||||
|
||||
<!-- 根据ID删除用户 -->
|
||||
<delete id="deleteById">
|
||||
delete from user
|
||||
where id = #{id}
|
||||
</delete>
|
||||
|
||||
<!-- 新增用户 -->
|
||||
<insert id="insert" parameterType="com.example.entity.User" useGeneratedKeys="true">
|
||||
insert into user
|
||||
<trim prefix="(" suffix=")" suffixOverrides=",">
|
||||
<if test="id != null">id,</if>
|
||||
<if test="username != null">username,</if>
|
||||
<if test="password != null">password,</if>
|
||||
<if test="name != null">name,</if>
|
||||
<if test="phone != null">phone,</if>
|
||||
<if test="email != null">email,</if>
|
||||
<if test="avatar != null">avatar,</if>
|
||||
<if test="role != null">role,</if>
|
||||
</trim>
|
||||
<trim prefix="values (" suffix=")" suffixOverrides=",">
|
||||
<if test="id != null">#{id},</if>
|
||||
<if test="username != null">#{username},</if>
|
||||
<if test="password != null">#{password},</if>
|
||||
<if test="name != null">#{name},</if>
|
||||
<if test="phone != null">#{phone},</if>
|
||||
<if test="email != null">#{email},</if>
|
||||
<if test="avatar != null">#{avatar},</if>
|
||||
<if test="role != null">#{role},</if>
|
||||
</trim>
|
||||
</insert>
|
||||
|
||||
<!-- 根据ID修改用户信息 -->
|
||||
<update id="updateById" parameterType="com.example.entity.User">
|
||||
update user
|
||||
<set>
|
||||
<if test="username != null">
|
||||
username = #{username}, <!-- 用户名 -->
|
||||
</if>
|
||||
<if test="password != null">
|
||||
password = #{password}, <!-- 密码 -->
|
||||
</if>
|
||||
<if test="name != null">
|
||||
name = #{name}, <!-- 用户昵称 -->
|
||||
</if>
|
||||
<if test="phone != null">
|
||||
phone = #{phone}, <!-- 手机号码 -->
|
||||
</if>
|
||||
<if test="email != null">
|
||||
email = #{email}, <!-- 邮箱地址 -->
|
||||
</if>
|
||||
<if test="avatar != null">
|
||||
avatar = #{avatar}, <!-- 用户头像 -->
|
||||
</if>
|
||||
<if test="role != null">
|
||||
role = #{role}, <!-- 用户角色 -->
|
||||
</if>
|
||||
</set>
|
||||
where id = #{id}
|
||||
</update>
|
||||
|
||||
</mapper>
|
||||