提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
- 一、项目目录结构
- 二、StoreApplication.javapom.xml、application.properties
- 三、interceptor、config、util(工具类返回一串json数据)
- 四、entity+vo
- 五、 mapper(接口文件)
- 六、mapper(映射文件)
- 七、service
- 1. ex(错误抛出)
- UserNotFoundException.java
- UsernameDuplicatedException.java
- UpdateException.java
- ServiceException.java
- ProductNotFoundException.java
- PasswordNotMatchException.java
- InsertException.java
- DeleteException.java
- CartNotFoundException.java
- AddressNotFoundException.java
- AddressCountLimitException.java
- AccessDeniedException.java
- 2. 接口文件
- 3. impl(接口实现类)
- 八、controller
- 总结
提示:项目源码
https://gitee.com/alfred-tfk/store_-pc
一、项目目录结构
二、StoreApplication.javapom.xml、application.properties
StoreApplication.java
package com.lll.store;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
//@MapperScan 指定当前项目中的Mapper接口路径的位置,再项目启动的时候会自动加载所有的接口
@MapperScan("com.lll.store.mapper")
public class StoreApplication {
public static void main(String[] args) {
SpringApplication.run(StoreApplication.class, args);
}
}
pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.lll</groupId>
<artifactId>store</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>store</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/store?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
#定义变量在AddressServiceImpl中使用@Value("${user.address.max-count}")
user.address.max-count=20
mybatis.mapper-locations=classpath:mapper/*.xml
#上传头像最大容量
spring.servlet.multipart.max-request-size=10MB
spring.servlet.multipart.max-file-size=15MB
#Redis服务器地址
spring.redis.host=127.0.0.1
#Reids服务器链接端口
spring.redis.port=6379
#Redis服务器连接密码(默认为空)
spring.rabbitmq.password=123456
三、interceptor、config、util(工具类返回一串json数据)
1. interceptor
Loginlnterceptor.java(拦截器)
package com.lll.store.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/** 定义一个拦截器 */
public class Loginlnterceptor implements HandlerInterceptor {
/**
* 检测全局session对象中是否有uid数据,如果有则放行,如果没有重定向到登录页面
* @param request 请求对象
* @param response 响应对象
* @param handler 处理器(url+Controller:映射)
* @return 如果返回值为true表示放行当前的请求,如果返回值为false则表示拦截当前的请求
* @throws Exception
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// HttpServletResponse对象来获取session对象
Object obj = request.getSession().getAttribute("uid");
if (obj == null){
// 说明用户没有登录过系统,则重定向到login.html页面
response.sendRedirect("/web/login.html");
// 结束后续的调用
return false;
}
// 请求放行
return true;
}
}
2. config
LoginlnterceptorConfigurer.java
package com.lll.store.config;
import com.lll.store.interceptor.Loginlnterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.ArrayList;
import java.util.List;
@Configuration //加载当前的拦截器并进行注册
/** 处理器拦截器的注册 */
public class LoginlnterceptorConfigurer implements WebMvcConfigurer {
/** 配置拦截器 */
@Override
public void addInterceptors(InterceptorRegistry registry) {
//创建自定义的拦截器对象
HandlerInterceptor interceptor = new Loginlnterceptor();
//配置白名单:存放在一个List集合
List<String> patterns = new ArrayList<>();
patterns.add("/bootstrap3/**");
patterns.add("/css/**");
patterns.add("/images/**");
patterns.add("/js/**");
patterns.add("/web/register.html");
patterns.add("/web/login.html");
patterns.add("/web/password.html");
patterns.add("/web/index.html");
patterns.add("/web/product.html");
patterns.add("/users/reg");
patterns.add("/users/login");
patterns.add("/districts/**");
patterns.add("products/**");
// 完成拦截器的注册
registry.addInterceptor(interceptor).addPathPatterns("/**").excludePathPatterns(patterns);
//表示要拦截的url是什么
}
}
RedisConfig.java(配置redis)
package com.lll.store.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.time.Duration;
/**
* 自定义Redis配置类,进行序列化以及RedisTemplate设置
*/
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
/**
* 定制Redis API模板RedisTemplate
*
* @param redisConnectionFactory
* @return
*/
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
// 使用JSON格式序列化对象,对缓存数据key和value进行转换
Jackson2JsonRedisSerializer jsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
// 解决查询缓存转换异常问题
ObjectMapper om = new ObjectMapper();
// 指定要序列化的域,feild,get和set,以及修饰符范围,ANY是都有包括private和public
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如string,Integer等会异常
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jsonRedisSerializer.setObjectMapper(om);
// 设置RedisTemplate模板API的序列化方式为JSON
template.setDefaultSerializer(jsonRedisSerializer);
return template;
}
/**
* 定制Redis缓存管理RedisCacheManager,实现自定义序列化并设置缓存时效
* @param redisConnectionFactory
* @return
*/
@Bean
public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory){
// 分别创建String和JSON格式化序列对象,对缓存数据key和value进行转换
RedisSerializer<String> stringRedisSerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer jsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
// 解决查询缓存转换异常的问题
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jsonRedisSerializer.setObjectMapper(om);
// 定制缓存数据序列化方式及时效
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofDays(7)) //设置缓存有效期为1天
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(stringRedisSerializer))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jsonRedisSerializer))
.disableCachingNullValues(); // 对空数据不进行缓存
RedisCacheManager cacheManager = RedisCacheManager.builder(redisConnectionFactory).cacheDefaults(config).build();
return cacheManager;
}
}
3. util
JsonResult.java
package com.lll.store.util;
import lombok.Data;
import java.io.Serializable;
/**
* Json格式的数据进行响应Serializable
*/
public class JsonResult<E> implements Serializable {
//状态码
private Integer state;
//描述信息
private String message;
//数据泛型
private E data;
public JsonResult(Integer state) {
this.state = state;
}
public JsonResult(Throwable e) {
this.message = e.getMessage();
}
public JsonResult(Integer state, E data) {
this.state = state;
this.data = data;
}
public JsonResult() {
}
public Integer getState() {
return state;
}
public void setState(Integer state) {
this.state = state;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public E getData() {
return data;
}
public void setData(E data) {
this.data = data;
}
}
四、entity+vo
User.java
package com.lll.store.entity;
import lombok.Data;
import org.springframework.stereotype.Component;
import java.io.Serializable;
/**
* 用户实体类
*/
@Data
public class User extends BaseEntity implements Serializable {
private Integer uid;
private String username;
private String password;
private String salt;
private String phone;
private String email;
private Integer gender;
private String avatar;
private Integer isDelete;
}
BaseEntity.java
package com.lll.store.entity;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
/**
* 作为实体类的基类
*/
@Data
public class BaseEntity implements Serializable {
private String createdUser;
private Date createdTime;
private String modifiedUser;
private Date modifiedTime;
}
Address.java
package com.lll.store.entity;
import java.io.Serializable;
/** 收货地址数据的实体类 */
public class Address extends BaseEntity implements Serializable {
private Integer aid;
private Integer uid;
private String name;
private String provinceName;
private String provinceCode;
private String cityName;
private String cityCode;
private String areaName;
private String areaCode;
private String zip;
private String address;
private String phone;
private String tel;
private String tag;
private Integer isDefault;
public Integer getAid() {
return aid;
}
public void setAid(Integer aid) {
this.aid = aid;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getProvinceName() {
return provinceName;
}
public void setProvinceName(String provinceName) {
this.provinceName = provinceName;
}
public String getProvinceCode() {
return provinceCode;
}
public void setProvinceCode(String provinceCode) {
this.provinceCode = provinceCode;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public String getCityCode() {
return cityCode;
}
public void setCityCode(String cityCode) {
this.cityCode = cityCode;
}
public String getAreaName() {
return areaName;
}
public void setAreaName(String areaName) {
this.areaName = areaName;
}
public String getAreaCode() {
return areaCode;
}
public void setAreaCode(String areaCode) {
this.areaCode = areaCode;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getTag() {
return tag;
}
public void setTag(String tag) {
this.tag = tag;
}
public Integer getIsDefault() {
return isDefault;
}
public void setIsDefault(Integer isDefault) {
this.isDefault = isDefault;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Address)) return false;
Address address1 = (Address) o;
if (getAid() != null ? !getAid().equals(address1.getAid()) : address1.getAid() != null) return false;
if (getUid() != null ? !getUid().equals(address1.getUid()) : address1.getUid() != null) return false;
if (getName() != null ? !getName().equals(address1.getName()) : address1.getName() != null) return false;
if (getProvinceName() != null ? !getProvinceName().equals(address1.getProvinceName()) : address1.getProvinceName() != null)
return false;
if (getProvinceCode() != null ? !getProvinceCode().equals(address1.getProvinceCode()) : address1.getProvinceCode() != null)
return false;
if (getCityName() != null ? !getCityName().equals(address1.getCityName()) : address1.getCityName() != null)
return false;
if (getCityCode() != null ? !getCityCode().equals(address1.getCityCode()) : address1.getCityCode() != null)
return false;
if (getAreaName() != null ? !getAreaName().equals(address1.getAreaName()) : address1.getAreaName() != null)
return false;
if (getAreaCode() != null ? !getAreaCode().equals(address1.getAreaCode()) : address1.getAreaCode() != null)
return false;
if (getZip() != null ? !getZip().equals(address1.getZip()) : address1.getZip() != null) return false;
if (getAddress() != null ? !getAddress().equals(address1.getAddress()) : address1.getAddress() != null)
return false;
if (getPhone() != null ? !getPhone().equals(address1.getPhone()) : address1.getPhone() != null) return false;
if (getTel() != null ? !getTel().equals(address1.getTel()) : address1.getTel() != null) return false;
if (getTag() != null ? !getTag().equals(address1.getTag()) : address1.getTag() != null) return false;
return getIsDefault() != null ? getIsDefault().equals(address1.getIsDefault()) : address1.getIsDefault() == null;
}
@Override
public int hashCode() {
int result = getAid() != null ? getAid().hashCode() : 0;
result = 31 * result + (getUid() != null ? getUid().hashCode() : 0);
result = 31 * result + (getName() != null ? getName().hashCode() : 0);
result = 31 * result + (getProvinceName() != null ? getProvinceName().hashCode() : 0);
result = 31 * result + (getProvinceCode() != null ? getProvinceCode().hashCode() : 0);
result = 31 * result + (getCityName() != null ? getCityName().hashCode() : 0);
result = 31 * result + (getCityCode() != null ? getCityCode().hashCode() : 0);
result = 31 * result + (getAreaName() != null ? getAreaName().hashCode() : 0);
result = 31 * result + (getAreaCode() != null ? getAreaCode().hashCode() : 0);
result = 31 * result + (getZip() != null ? getZip().hashCode() : 0);
result = 31 * result + (getAddress() != null ? getAddress().hashCode() : 0);
result = 31 * result + (getPhone() != null ? getPhone().hashCode() : 0);
result = 31 * result + (getTel() != null ? getTel().hashCode() : 0);
result = 31 * result + (getTag() != null ? getTag().hashCode() : 0);
result = 31 * result + (getIsDefault() != null ? getIsDefault().hashCode() : 0);
return result;
}
@Override
public String toString() {
return "Address{" +
"aid=" + aid +
", uid=" + uid +
", name='" + name + '\'' +
", provinceName='" + provinceName + '\'' +
", provinceCode='" + provinceCode + '\'' +
", cityName='" + cityName + '\'' +
", cityCode='" + cityCode + '\'' +
", areaName='" + areaName + '\'' +
", areaCode='" + areaCode + '\'' +
", zip='" + zip + '\'' +
", address='" + address + '\'' +
", phone='" + phone + '\'' +
", tel='" + tel + '\'' +
", tag='" + tag + '\'' +
", isDefault=" + isDefault +
"} " + super.toString();
}
}
Product.java
package com.lll.store.entity;
import java.io.Serializable;
/** 商品数据的实体类 */
public class Product extends BaseEntity implements Serializable {
private Integer id;
private Integer categoryId;
private String itemType;
private String title;
private String sellPoint;
private Long price;
private Integer num;
private String image;
private Integer status;
private Integer priority;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getCategoryId() {
return categoryId;
}
public void setCategoryId(Integer categoryId) {
this.categoryId = categoryId;
}
public String getItemType() {
return itemType;
}
public void setItemType(String itemType) {
this.itemType = itemType;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSellPoint() {
return sellPoint;
}
public void setSellPoint(String sellPoint) {
this.sellPoint = sellPoint;
}
public Long getPrice() {
return price;
}
public void setPrice(Long price) {
this.price = price;
}
public Integer getNum() {
return num;
}
public void setNum(Integer num) {
this.num = num;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public Integer getPriority() {
return priority;
}
public void setPriority(Integer priority) {
this.priority = priority;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Product)) return false;
Product product = (Product) o;
if (getId() != null ? !getId().equals(product.getId()) : product.getId() != null) return false;
if (getCategoryId() != null ? !getCategoryId().equals(product.getCategoryId()) : product.getCategoryId() != null)
return false;
if (getItemType() != null ? !getItemType().equals(product.getItemType()) : product.getItemType() != null)
return false;
if (getTitle() != null ? !getTitle().equals(product.getTitle()) : product.getTitle() != null) return false;
if (getSellPoint() != null ? !getSellPoint().equals(product.getSellPoint()) : product.getSellPoint() != null)
return false;
if (getPrice() != null ? !getPrice().equals(product.getPrice()) : product.getPrice() != null) return false;
if (getNum() != null ? !getNum().equals(product.getNum()) : product.getNum() != null) return false;
if (getImage() != null ? !getImage().equals(product.getImage()) : product.getImage() != null) return false;
if (getStatus() != null ? !getStatus().equals(product.getStatus()) : product.getStatus() != null) return false;
return getPriority() != null ? getPriority().equals(product.getPriority()) : product.getPriority() == null;
}
@Override
public int hashCode() {
int result = getId() != null ? getId().hashCode() : 0;
result = 31 * result + (getCategoryId() != null ? getCategoryId().hashCode() : 0);
result = 31 * result + (getItemType() != null ? getItemType().hashCode() : 0);
result = 31 * result + (getTitle() != null ? getTitle().hashCode() : 0);
result = 31 * result + (getSellPoint() != null ? getSellPoint().hashCode() : 0);
result = 31 * result + (getPrice() != null ? getPrice().hashCode() : 0);
result = 31 * result + (getNum() != null ? getNum().hashCode() : 0);
result = 31 * result + (getImage() != null ? getImage().hashCode() : 0);
result = 31 * result + (getStatus() != null ? getStatus().hashCode() : 0);
result = 31 * result + (getPriority() != null ? getPriority().hashCode() : 0);
return result;
}
@Override
public String toString() {
return "Product{" +
"id=" + id +
", categoryId=" + categoryId +
", itemType='" + itemType + '\'' +
", title='" + title + '\'' +
", sellPoint='" + sellPoint + '\'' +
", price=" + price +
", num=" + num +
", image='" + image + '\'' +
", status=" + status +
", priority=" + priority +
"} " + super.toString();
}
}
District.java
package com.lll.store.entity;
import java.io.Serializable;
/** 省/市/区数据的实体类 */
public class District implements Serializable {
private Integer id;
private String parent;
private String code;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getParent() {
return parent;
}
public void setParent(String parent) {
this.parent = parent;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof District)) return false;
District district = (District) o;
if (getId() != null ? !getId().equals(district.getId()) : district.getId() != null) return false;
if (getParent() != null ? !getParent().equals(district.getParent()) : district.getParent() != null)
return false;
if (getCode() != null ? !getCode().equals(district.getCode()) : district.getCode() != null) return false;
return getName() != null ? getName().equals(district.getName()) : district.getName() == null;
}
@Override
public int hashCode() {
int result = getId() != null ? getId().hashCode() : 0;
result = 31 * result + (getParent() != null ? getParent().hashCode() : 0);
result = 31 * result + (getCode() != null ? getCode().hashCode() : 0);
result = 31 * result + (getName() != null ? getName().hashCode() : 0);
return result;
}
@Override
public String toString() {
return "District{" +
"id=" + id +
", parent='" + parent + '\'' +
", code='" + code + '\'' +
", name='" + name + '\'' +
'}';
}
}
Cart.java
package com.lll.store.entity;
import java.io.Serializable;
/** 购物车数据的实体类 */
public class Cart extends BaseEntity implements Serializable {
private Integer cid;
private Integer uid;
private Integer pid;
private Long price;
private Integer num;
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public Long getPrice() {
return price;
}
public void setPrice(Long price) {
this.price = price;
}
public Integer getNum() {
return num;
}
public void setNum(Integer num) {
this.num = num;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Cart)) return false;
Cart cart = (Cart) o;
if (getCid() != null ? !getCid().equals(cart.getCid()) : cart.getCid() != null) return false;
if (getUid() != null ? !getUid().equals(cart.getUid()) : cart.getUid() != null) return false;
if (getPid() != null ? !getPid().equals(cart.getPid()) : cart.getPid() != null) return false;
if (getPrice() != null ? !getPrice().equals(cart.getPrice()) : cart.getPrice() != null) return false;
return getNum() != null ? getNum().equals(cart.getNum()) : cart.getNum() == null;
}
@Override
public int hashCode() {
int result = getCid() != null ? getCid().hashCode() : 0;
result = 31 * result + (getUid() != null ? getUid().hashCode() : 0);
result = 31 * result + (getPid() != null ? getPid().hashCode() : 0);
result = 31 * result + (getPrice() != null ? getPrice().hashCode() : 0);
result = 31 * result + (getNum() != null ? getNum().hashCode() : 0);
return result;
}
@Override
public String toString() {
return "Cart{" +
"cid=" + cid +
", uid=" + uid +
", pid=" + pid +
", price=" + price +
", num=" + num +
"} " + super.toString();
}
}
Order.java
package com.lll.store.entity;
import java.io.Serializable;
import java.util.Date;
/** 订单数据的实体类 */
public class Order extends BaseEntity implements Serializable {
private Integer oid;
private Integer uid;
private String recvName;
private String recvPhone;
private String recvProvince;
private String recvCity;
private String recvArea;
private String recvAddress;
private Long totalPrice;
private Integer status;
private Date orderTime;
private Date payTime;
public Integer getOid() {
return oid;
}
public void setOid(Integer oid) {
this.oid = oid;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public String getRecvName() {
return recvName;
}
public void setRecvName(String recvName) {
this.recvName = recvName;
}
public String getRecvPhone() {
return recvPhone;
}
public void setRecvPhone(String recvPhone) {
this.recvPhone = recvPhone;
}
public String getRecvProvince() {
return recvProvince;
}
public void setRecvProvince(String recvProvince) {
this.recvProvince = recvProvince;
}
public String getRecvCity() {
return recvCity;
}
public void setRecvCity(String recvCity) {
this.recvCity = recvCity;
}
public String getRecvArea() {
return recvArea;
}
public void setRecvArea(String recvArea) {
this.recvArea = recvArea;
}
public String getRecvAddress() {
return recvAddress;
}
public void setRecvAddress(String recvAddress) {
this.recvAddress = recvAddress;
}
public Long getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(Long totalPrice) {
this.totalPrice = totalPrice;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public Date getOrderTime() {
return orderTime;
}
public void setOrderTime(Date orderTime) {
this.orderTime = orderTime;
}
public Date getPayTime() {
return payTime;
}
public void setPayTime(Date payTime) {
this.payTime = payTime;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Order)) return false;
Order order = (Order) o;
if (getOid() != null ? !getOid().equals(order.getOid()) : order.getOid() != null) return false;
if (getUid() != null ? !getUid().equals(order.getUid()) : order.getUid() != null) return false;
if (getRecvName() != null ? !getRecvName().equals(order.getRecvName()) : order.getRecvName() != null)
return false;
if (getRecvPhone() != null ? !getRecvPhone().equals(order.getRecvPhone()) : order.getRecvPhone() != null)
return false;
if (getRecvProvince() != null ? !getRecvProvince().equals(order.getRecvProvince()) : order.getRecvProvince() != null)
return false;
if (getRecvCity() != null ? !getRecvCity().equals(order.getRecvCity()) : order.getRecvCity() != null)
return false;
if (getRecvArea() != null ? !getRecvArea().equals(order.getRecvArea()) : order.getRecvArea() != null)
return false;
if (getRecvAddress() != null ? !getRecvAddress().equals(order.getRecvAddress()) : order.getRecvAddress() != null)
return false;
if (getTotalPrice() != null ? !getTotalPrice().equals(order.getTotalPrice()) : order.getTotalPrice() != null)
return false;
if (getStatus() != null ? !getStatus().equals(order.getStatus()) : order.getStatus() != null) return false;
if (getOrderTime() != null ? !getOrderTime().equals(order.getOrderTime()) : order.getOrderTime() != null)
return false;
return getPayTime() != null ? getPayTime().equals(order.getPayTime()) : order.getPayTime() == null;
}
@Override
public int hashCode() {
int result = getOid() != null ? getOid().hashCode() : 0;
result = 31 * result + (getUid() != null ? getUid().hashCode() : 0);
result = 31 * result + (getRecvName() != null ? getRecvName().hashCode() : 0);
result = 31 * result + (getRecvPhone() != null ? getRecvPhone().hashCode() : 0);
result = 31 * result + (getRecvProvince() != null ? getRecvProvince().hashCode() : 0);
result = 31 * result + (getRecvCity() != null ? getRecvCity().hashCode() : 0);
result = 31 * result + (getRecvArea() != null ? getRecvArea().hashCode() : 0);
result = 31 * result + (getRecvAddress() != null ? getRecvAddress().hashCode() : 0);
result = 31 * result + (getTotalPrice() != null ? getTotalPrice().hashCode() : 0);
result = 31 * result + (getStatus() != null ? getStatus().hashCode() : 0);
result = 31 * result + (getOrderTime() != null ? getOrderTime().hashCode() : 0);
result = 31 * result + (getPayTime() != null ? getPayTime().hashCode() : 0);
return result;
}
@Override
public String toString() {
return "Order{" +
"oid=" + oid +
", uid=" + uid +
", recvName='" + recvName + '\'' +
", recvPhone='" + recvPhone + '\'' +
", recvProvince='" + recvProvince + '\'' +
", recvCity='" + recvCity + '\'' +
", recvArea='" + recvArea + '\'' +
", recvAddress='" + recvAddress + '\'' +
", totalPrice=" + totalPrice +
", status=" + status +
", orderTime=" + orderTime +
", payTime=" + payTime +
"} " + super.toString();
}
}
OrderItem.java
package com.lll.store.entity;
import java.io.Serializable;
/** 订单中的商品数据 */
public class OrderItem extends BaseEntity implements Serializable {
private Integer id;
private Integer oid;
private Integer pid;
private String title;
private String image;
private Long price;
private Integer num;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getOid() {
return oid;
}
public void setOid(Integer oid) {
this.oid = oid;
}
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public Long getPrice() {
return price;
}
public void setPrice(Long price) {
this.price = price;
}
public Integer getNum() {
return num;
}
public void setNum(Integer num) {
this.num = num;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof OrderItem)) return false;
OrderItem orderItem = (OrderItem) o;
if (getId() != null ? !getId().equals(orderItem.getId()) : orderItem.getId() != null) return false;
if (getOid() != null ? !getOid().equals(orderItem.getOid()) : orderItem.getOid() != null) return false;
if (getPid() != null ? !getPid().equals(orderItem.getPid()) : orderItem.getPid() != null) return false;
if (getTitle() != null ? !getTitle().equals(orderItem.getTitle()) : orderItem.getTitle() != null) return false;
if (getImage() != null ? !getImage().equals(orderItem.getImage()) : orderItem.getImage() != null) return false;
if (getPrice() != null ? !getPrice().equals(orderItem.getPrice()) : orderItem.getPrice() != null) return false;
return getNum() != null ? getNum().equals(orderItem.getNum()) : orderItem.getNum() == null;
}
@Override
public int hashCode() {
int result = getId() != null ? getId().hashCode() : 0;
result = 31 * result + (getOid() != null ? getOid().hashCode() : 0);
result = 31 * result + (getPid() != null ? getPid().hashCode() : 0);
result = 31 * result + (getTitle() != null ? getTitle().hashCode() : 0);
result = 31 * result + (getImage() != null ? getImage().hashCode() : 0);
result = 31 * result + (getPrice() != null ? getPrice().hashCode() : 0);
result = 31 * result + (getNum() != null ? getNum().hashCode() : 0);
return result;
}
@Override
public String toString() {
return "OrderItem{" +
"id=" + id +
", oid=" + oid +
", pid=" + pid +
", title='" + title + '\'' +
", image='" + image + '\'' +
", price=" + price +
", num=" + num +
"} " + super.toString();
}
}
CartVO.java
package com.lll.store.vo;
import java.io.Serializable;
/** 购物车数据的Value Object类 */
public class CartVO implements Serializable {
private Integer cid;
private Integer uid;
private Integer pid;
private Long price;
private Integer num;
private String title;
private Long realPrice;
private String image;
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public Long getPrice() {
return price;
}
public void setPrice(Long price) {
this.price = price;
}
public Integer getNum() {
return num;
}
public void setNum(Integer num) {
this.num = num;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Long getRealPrice() {
return realPrice;
}
public void setRealPrice(Long realPrice) {
this.realPrice = realPrice;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof CartVO)) return false;
CartVO cartVO = (CartVO) o;
if (getCid() != null ? !getCid().equals(cartVO.getCid()) : cartVO.getCid() != null) return false;
if (getUid() != null ? !getUid().equals(cartVO.getUid()) : cartVO.getUid() != null) return false;
if (getPid() != null ? !getPid().equals(cartVO.getPid()) : cartVO.getPid() != null) return false;
if (getPrice() != null ? !getPrice().equals(cartVO.getPrice()) : cartVO.getPrice() != null) return false;
if (getNum() != null ? !getNum().equals(cartVO.getNum()) : cartVO.getNum() != null) return false;
if (getTitle() != null ? !getTitle().equals(cartVO.getTitle()) : cartVO.getTitle() != null) return false;
if (getRealPrice() != null ? !getRealPrice().equals(cartVO.getRealPrice()) : cartVO.getRealPrice() != null)
return false;
return getImage() != null ? getImage().equals(cartVO.getImage()) : cartVO.getImage() == null;
}
@Override
public int hashCode() {
int result = getCid() != null ? getCid().hashCode() : 0;
result = 31 * result + (getUid() != null ? getUid().hashCode() : 0);
result = 31 * result + (getPid() != null ? getPid().hashCode() : 0);
result = 31 * result + (getPrice() != null ? getPrice().hashCode() : 0);
result = 31 * result + (getNum() != null ? getNum().hashCode() : 0);
result = 31 * result + (getTitle() != null ? getTitle().hashCode() : 0);
result = 31 * result + (getRealPrice() != null ? getRealPrice().hashCode() : 0);
result = 31 * result + (getImage() != null ? getImage().hashCode() : 0);
return result;
}
@Override
public String toString() {
return "CartVO{" +
"cid=" + cid +
", uid=" + uid +
", pid=" + pid +
", price=" + price +
", num=" + num +
", title='" + title + '\'' +
", realPrice=" + realPrice +
", image='" + image + '\'' +
'}';
}
}
五、 mapper(接口文件)
UserMapper.java
package com.lll.store.mapper;
import com.lll.store.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.Date;
/**
* 用户模块的持久层接口
*/
public interface UserMapper {
/**
* 插入用户数据
*
* @param user 用户的数据
* @return 受影响的行数(增、删、改,都有受影响的行数作为返回值,可以根据返回值来判断是否执行成功)
*/
Integer insert(User user);
/**
* 根据用户名来查询用户的数据
*
* @param username 用户名
* @return 如果找到对应的用户则返回这个用户的数据,如果没有找到则返回null值
*/
User findByUsername(String username);
/**
* 根据用户的uid来修改密码
*
* @param uid 用户的id
* @param password 用户输入的新密码
* @param modifiedUser 表示执行的修改者
* @param modifiedTime 表示修改数据的时间
* @return 返回值为受影响的行数
*/
Integer updatePasswordByUid(Integer uid, String password, String modifiedUser, Date modifiedTime);
/**
* 根据用户的id查询用户的数据
*
* @param uid 用户的id
* @return 如果找到则返回对象,反之返回null值
*/
User findByUid(Integer uid);
/**
* 更新用户的数据信息
*
* @param user 用户的数据
* @return 返回值为受影响的行数
*/
Integer updateInfoByUid(User user);
/**
* @Para("SQL映射文件中#{}占位符的变量名"): 解决的问题,当SQL语句的占位符
* 和映射的接口方法参数名不一致,需要将某个参数强行注入到某个占位符变量上时,
* 可以使用@Param这个注解来标注映射的关系
*
* 根据用户的uid值来修改用户的头像
* @param uid
* @param avatar
* @param modifiedUser
* @param modifiedTime
* @return
*/
Integer updateAvatarByUid(
@Param("uid") Integer uid,
@Param("avatar") String avatar,
@Param("modifiedUser") String modifiedUser,
@Param("modifiedTime") Date modifiedTime
);
}
AddressMapper.java
package com.lll.store.mapper;
import com.lll.store.entity.Address;
import org.apache.ibatis.annotations.Param;
import java.util.Date;
import java.util.List;
/** 处理收货地址数据的持久层接口 */
public interface AddressMapper {
/**
* 插入收货地址数据
* @param address 收货地址数据
* @return 受影响的行数
*/
Integer insert(Address address);
/**
* 统计某用户的收货地址数据的数量
* @param uid 用户的id
* @return 该用户的收货地址数据的数量
*/
Integer countByUid(Integer uid);
/**
* 查询某用户的收货地址列表数据
* @param uid 收货地址归属的用户id
* @return 该用户的收货地址列表数据
*/
List<Address> findByUid(Integer uid);
/**
* 根据收货地址aid值,查询收货地址详情
* @param aid 收货地址id
* @return 匹配的收货地址详情,如果没有匹配的数据,则返回null
*/
Address findByAid(Integer aid);
/**
* 将某用户的所有收货地址设置为非默认地址
* @param uid 收货地址归属的用户id
* @return 受影响的行数
*/
Integer updateNonDefaultByUid(Integer uid);
/**
* 将指定的收货地址设置为默认地址
* @param aid 收货地址id
* @param modifiedUser 修改执行人
* @param modifiedTime 修改时间
* @return 受影响的行数
*/
Integer updateDefaultByAid(
@Param("aid") Integer aid,
@Param("modifiedUser") String modifiedUser,
@Param("modifiedTime") Date modifiedTime);
/**
* 根据收货地址id删除数据
* @param aid 收货地址id
* @return 受影响的行数
*/
Integer deleteByAid(Integer aid);
/**
* 查询某用户最后修改的收货地址
* @param uid 归属的用户id
* @return 该用户最后修改的收货地址,如果该用户没有收货地址数据则返回null
*/
Address findLastModified(Integer uid);
}
ProductMapper.java
package com.lll.store.mapper;
import com.lll.store.entity.Product;
import java.util.List;
/** 处理商品数据的持久层接口 */
public interface ProductMapper {
/**
* 查询热销商品的前四名
* @return 热销商品前四名的集合
*/
List<Product> findHotList();
/**
* 根据商品id查询商品详情
* @param id 商品id
* @return 匹配的商品详情,如果没有匹配的数据则返回null
*/
Product findById(Integer id);
}
DistrictMapper.java
package com.lll.store.mapper;
import com.lll.store.entity.District;
import java.util.List;
/** 处理省/市/区数据的持久层接口 */
public interface DistrictMapper {
/**
* 获取全国所有省/某省所有市/某市所有区
* @param parent 父级代号,当获取某市所有区时,使用市的代号;当获取省所有市时,使用省的代号;当获取全国所有省时,使用"86"作为父级代号
* @return 全国所有省/某省所有市/某市所有区的列表
*/
List<District> findByParent(String parent);
/**
* 根据省/市/区的行政代号获取省/市/区的名称
* @param code 省/市/区的行政代号
* @return 匹配的省/市/区的名称,如果没有匹配的数据则返回null
*/
String findNameByCode(String code);
}
CartMapper.java
package com.lll.store.mapper;
import com.lll.store.entity.Cart;
import com.lll.store.vo.CartVO;
import org.apache.ibatis.annotations.Param;
import java.util.Date;
import java.util.List;
/** 处理购物车数据的持久层接口 */
public interface CartMapper {
/**
* 插入购物车数据
* @param cart 购物车数据
* @return 受影响的行数
*/
Integer insert(Cart cart);
/**
* 修改购物车数据中商品的数量
* @param cid 购物车数据的id
* @param num 新的数量
* @param modifiedUser 修改执行人
* @param modifiedTime 修改时间
* @return 受影响的行数
*/
Integer updateNumByCid(
@Param("cid") Integer cid,
@Param("num") Integer num,
@Param("modifiedUser") String modifiedUser,
@Param("modifiedTime") Date modifiedTime);
/**
* 根据用户id和商品id查询购物车中的数据
* @param uid 用户id
* @param pid 商品id
* @return 匹配的购物车数据,如果该用户的购物车中并没有该商品,则返回null
*/
Cart findByUidAndPid(
@Param("uid") Integer uid,
@Param("pid") Integer pid);
/**
* 查询某用户的购物车数据
* @param uid 用户id
* @return 该用户的购物车数据的列表
*/
List<CartVO> findVOByUid(Integer uid);
/**
* 根据购物车数据id查询购物车数据详情
* @param cid 购物车数据id
* @return 匹配的购物车数据详情,如果没有匹配的数据则返回null
*/
Cart findByCid(Integer cid);
/**
* 根据若干个购物车数据id查询详情的列表
* @param cids 若干个购物车数据id
* @return 匹配的购物车数据详情的列表
*/
List<CartVO> findVOByCids(Integer[] cids);
}
OrderMapper.java
package com.lll.store.mapper;
import com.lll.store.entity.Order;
import com.lll.store.entity.OrderItem;
/** 处理订单及订单商品数据的持久层接口 */
public interface OrderMapper {
/**
* 插入订单数据
* @param order 订单数据
* @return 受影响的行数
*/
Integer insertOrder(Order order);
/**
* 插入订单商品数据
* @param orderItem 订单商品数据
* @return 受影响的行数
*/
Integer insertOrderItem(OrderItem orderItem);
}
六、mapper(映射文件)
UserMapper.xml
<?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">
<!--namespace属性:用于指定当前文件夹的映射文件和哪个接口进行映射,需要指定接口的文件路径,需要标注包的完整路径的接口-->
<mapper namespace="com.lll.store.mapper.UserMapper">
<!--自定义映射规则:resultMap标签来完成映射规则-->
<!--
id属性:标签给这个映射负责分配一个唯一的id值,对应的就是resultMap="id属性的值"属性的取值
type属性:取值是一个类,表示的是数据库中的查询结果于Java中的哪个实体类进行结构集的映射
-->
<resultMap id="UserEntityMap" type="com.lll.store.entity.User">
<!--将表的字段和类的属性不一致的字段进行匹配指定,名称一致的字段可以省略不写-->
<!--
配合完成名称不一致的映射:
column属性:表示表中的字段名称
property属性:表示类中属性名称
-->
<!--在定义映射规则时主键是不可以省略的-->
<id column="uid" property="uid"></id>
<result column="is_delete" property="isDelete"></result>
<result column="created_user" property="createdUser"></result>
<result column="created_time" property="createdTime"></result>
<result column="modified_user" property="modifiedUser"></result>
<result column="modified_time" property="modifiedTime"></result>
</resultMap>
<!--id属性:表示映射接口中方法的名称,直接在标签的内容部分来编写sql语句-->
<!--useGeneratedKeys:自增-->
<!--keyProperty:根据uid自增-->
<insert id="insert" useGeneratedKeys="true" keyProperty="uid">
INSERT INTO t_user(
username,password,
salt,phone,
email,gender,
avatar,is_delete,
created_user,created_time,
modified_user,modified_time
) VALUES (
#{username},#{password},
#{salt},#{phone},
#{email},#{gender},
#{avatar},#{isDelete},
#{createdUser},#{createdTime},
#{modifiedUser},#{modifiedTime}
)
<!--最后一个字段后面不能有逗号,方法后面不能有分号-->
</insert>
<!--select语句再执行的时候,查询的结果是一个对象,多个对象-->
<!--
resultType:表示查询的结果集类型,只需要指定对应映射类的类型,并且包含完整接口
resultMap:当表的字段和类的对属性的字段名称不一致时,来自定义查询结果集的映射规则
-->
<select id="findByUsername" resultMap="UserEntityMap">
SELECT * FROM t_user WHERE username = #{username}
</select>
<!-- pathVariable("user) String username -->
<!-- param("user) String username -->
<update id="updatePasswordByUid">
UPDATE t_user SET
password=#{password},
modified_user=#{modifiedUser},
modified_time=#{modifiedTime}
where uid=#{uid}
</update>
<select id="findByUid" resultMap="UserEntityMap">
SELECT * from t_user WHERE uid=#{uid}
</select>
<update id="updateInfoByUid">
UPDATE t_user SET
<!-- if表示条件判断标签。test接受的是一个返回值为boolean类型的条件,
如果test条件的结果为true则执行if标签内部的语句-->
<if test="phone!=null">phone = #{phone},</if>
<if test="email!=null">email = #{email},</if>
<if test="gender!=null">gender = #{gender},</if>
modified_user = #{modifiedUser},
modified_time = #{modifiedTime}
WHERE uid = #{uid}
</update>
<update id="updateAvatarByUid">
UPDATE t_user SET
avatar = #{avatar},
modified_user = #{modifiedUser},
modified_time = #{modifiedTime}
WHERE
uid = #{uid}
</update>
</mapper>
AddressMapper.xml
<?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">
<!--namespace属性:用于指定当前文件夹的映射文件和哪个接口进行映射,需要指定接口的文件路径,需要标注包的完整路径的接口-->
<mapper namespace="com.lll.store.mapper.AddressMapper">
<resultMap id="AddressEntityMap" type="com.lll.store.entity.Address">
<id column="aid" property="aid"/>
<result column="province_code" property="provinceCode"/>
<result column="province_name" property="provinceName"/>
<result column="city_code" property="cityCode"/>
<result column="city_name" property="cityName"/>
<result column="area_code" property="areaCode"/>
<result column="area_name" property="areaName"/>
<result column="is_default" property="isDefault"/>
<result column="created_user" property="createdUser"/>
<result column="created_time" property="createdTime"/>
<result column="modified_user" property="modifiedUser"/>
<result column="modified_time" property="modifiedTime"/>
</resultMap>
<!-- 插入收货地址数据:Integer insert(Address address) -->
<insert id="insert" useGeneratedKeys="true" keyProperty="aid">
INSERT INTO t_address (
uid, name, province_name, province_code, city_name, city_code, area_name, area_code, zip,
address, phone, tel, tag, is_default, created_user, created_time, modified_user, modified_time
) VALUES (
#{uid}, #{name}, #{provinceName}, #{provinceCode}, #{cityName}, #{cityCode}, #{areaName},
#{areaCode}, #{zip}, #{address}, #{phone}, #{tel}, #{tag}, #{isDefault}, #{createdUser},
#{createdTime}, #{modifiedUser}, #{modifiedTime}
)
</insert>
<!-- 统计某用户的收货地址数据的数量:Integer countByUid(Integer uid) -->
<select id="countByUid" resultType="java.lang.Integer">
SELECT
COUNT(*)
FROM
t_address
WHERE
uid=#{uid}
</select>
<!-- 查询某用户的收货地址列表数据:List<Address> findByUid(Integer uid) -->
<select id="findByUid" resultMap="AddressEntityMap">
SELECT
*
FROM
t_address
WHERE
uid=#{uid}
ORDER BY
is_default DESC, created_time DESC
</select>
<!-- 将某用户的所有收货地址设置为非默认地址:Integer updateNonDefaultByUid(Integer uid) -->
<update id="updateNonDefaultByUid">
UPDATE
t_address
SET
is_default=0
WHERE
uid=#{uid}
</update>
<!-- 将指定的收货地址设置为默认地址:
Integer updateDefaultByAid(
@Param("aid") Integer aid,
@Param("modifiedUser") String modifiedUser,
@Param("modifiedTime") Date modifiedTime) -->
<update id="updateDefaultByAid">
UPDATE
t_address
SET
is_default=1,
modified_user=#{modifiedUser},
modified_time=#{modifiedTime}
WHERE
aid=#{aid}
</update>
<!-- 根据收货地址aid值,查询收货地址详情:Address findByAid(Integer aid) -->
<select id="findByAid" resultMap="AddressEntityMap">
SELECT
*
FROM
t_address
WHERE
aid=#{aid}
</select>
<!-- 根据收货地址id删除数据:Integer deleteByAid(Integer aid) -->
<delete id="deleteByAid">
DELETE FROM
t_address
WHERE
aid=#{aid}
</delete>
<!-- 查询某用户最后修改的收货地址:Address findLastModified(Integer uid) -->
<select id="findLastModified" resultMap="AddressEntityMap">
SELECT
*
FROM
t_address
WHERE
uid=#{uid}
ORDER BY
modified_time DESC
LIMIT 0,1
</select>
</mapper>
DistrictMapper.xml
<?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.lll.store.mapper.DistrictMapper">
<!-- 获取全国所有省/某省所有市/某市所有区:List<District> findByParent(String parent) -->
<select id="findByParent" resultType="com.lll.store.entity.District">
SELECT
*
FROM
t_dict_district
WHERE
parent=#{parent}
ORDER BY
code ASC
</select>
<!-- 根据省/市/区的行政代号获取省/市/区的名称:String findNameByCode(String code) -->
<select id="findNameByCode" resultType="java.lang.String">
SELECT
name
FROM
t_dict_district
WHERE
code=#{code}
</select>
</mapper>
ProductMapper.xml
<?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.lll.store.mapper.ProductMapper">
<resultMap id="ProductEntityMap" type="com.lll.store.entity.Product">
<id column="id" property="id"/>
<result column="category_id" property="categoryId"/>
<result column="item_type" property="itemType"/>
<result column="sell_point" property="sellPoint"/>
<result column="created_user" property="createdUser"/>
<result column="created_time" property="createdTime"/>
<result column="modified_user" property="modifiedUser"/>
<result column="modified_time" property="modifiedTime"/>
</resultMap>
<!-- 查询热销商品的前四名:List<Product> findHostList() -->
<select id="findHotList" resultMap="ProductEntityMap">
SELECT
*
FROM
t_product
WHERE
status=1
ORDER BY
priority DESC
LIMIT 0,4
</select>
<!-- 根据商品id查询商品详情:Product findById(Integer id) -->
<select id="findById" resultMap="ProductEntityMap">
SELECT
*
FROM
t_product
WHERE
id=#{id}
</select>
</mapper>
CartMapper.xml
<?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.lll.store.mapper.CartMapper">
<resultMap id="CartEntityMap" type="com.lll.store.entity.Cart">
<id column="cid" property="cid"/>
<result column="created_user" property="createdUser"/>
<result column="created_time" property="createdTime"/>
<result column="modified_user" property="modifiedUser"/>
<result column="modified_time" property="modifiedTime"/>
</resultMap>
<!-- 插入购物车数据:Integer insert(Cart cart) -->
<insert id="insert" useGeneratedKeys="true" keyProperty="cid">
INSERT INTO t_cart (uid, pid, price, num, created_user, created_time, modified_user, modified_time)
VALUES (#{uid}, #{pid}, #{price}, #{num}, #{createdUser}, #{createdTime}, #{modifiedUser}, #{modifiedTime})
</insert>
<!-- 修改购物车数据中商品的数量:
Integer updateNumByCid(
@Param("cid") Integer cid,
@Param("num") Integer num,
@Param("modifiedUser") String modifiedUser,
@Param("modifiedTime") Date modifiedTime) -->
<update id="updateNumByCid">
UPDATE
t_cart
SET
num=#{num},
modified_user=#{modifiedUser},
modified_time=#{modifiedTime}
WHERE
cid=#{cid}
</update>
<!-- 根据用户id和商品id查询购物车中的数据:
Cart findByUidAndPid(
@Param("uid") Integer uid,
@Param("pid") Integer pid) -->
<select id="findByUidAndPid" resultMap="CartEntityMap">
SELECT
*
FROM
t_cart
WHERE
uid=#{uid} AND pid=#{pid}
</select>
<!-- 查询某用户的购物车数据:List<CartVO> findVOByUid(Integer uid) -->
<select id="findVOByUid" resultType="com.lll.store.vo.CartVO">
SELECT
cid,
uid,
pid,
t_cart.price,
t_cart.num,
t_product.title,
t_product.price AS realPrice,
t_product.image
FROM
t_cart
LEFT JOIN t_product ON t_cart.pid = t_product.id
WHERE
uid = #{uid}
ORDER BY
t_cart.created_time DESC
</select>
<!-- 根据购物车数据id查询购物车数据详情:Cart findByCid(Integer cid) -->
<select id="findByCid" resultMap="CartEntityMap">
SELECT
*
FROM
t_cart
WHERE
cid = #{cid}
</select>
<!-- 根据若干个购物车数据id查询详情的列表:List<CartVO> findVOByCids(Integer[] cids) -->
<select id="findVOByCids" resultType="com.lll.store.vo.CartVO">
SELECT
cid,
uid,
pid,
t_cart.price,
t_cart.num,
t_product.title,
t_product.price AS realPrice,
t_product.image
FROM
t_cart
LEFT JOIN t_product ON t_cart.pid = t_product.id
WHERE
cid IN (
<foreach collection="array" item="cid" separator=",">
#{cid}
</foreach>
)
ORDER BY
t_cart.created_time DESC
</select>
</mapper>
OrderMapper.xml
<?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.lll.store.mapper.OrderMapper">
<!-- 插入订单数据:Integer insertOrder(Order order) -->
<insert id="insertOrder" useGeneratedKeys="true" keyProperty="oid">
INSERT INTO t_order (
uid, recv_name, recv_phone, recv_province, recv_city, recv_area, recv_address,
total_price,status, order_time, pay_time, created_user, created_time, modified_user,
modified_time
) VALUES (
#{uid}, #{recvName}, #{recvPhone}, #{recvProvince}, #{recvCity}, #{recvArea},
#{recvAddress}, #{totalPrice}, #{status}, #{orderTime}, #{payTime}, #{createdUser},
#{createdTime}, #{modifiedUser}, #{modifiedTime}
)
</insert>
<!-- 插入订单商品数据:Integer insertOrderItem(OrderItem orderItem) -->
<insert id="insertOrderItem" useGeneratedKeys="true" keyProperty="id">
INSERT INTO t_order_item (
oid, pid, title, image, price, num, created_user,
created_time, modified_user, modified_time
) VALUES (
#{oid}, #{pid}, #{title}, #{image}, #{price}, #{num}, #{createdUser},
#{createdTime}, #{modifiedUser}, #{modifiedTime}
)
</insert>
</mapper>
七、service
1. ex(错误抛出)
UserNotFoundException.java
package com.lll.store.service.ex;
/** 用户数据不存在的异常 */
public class UserNotFoundException extends ServiceException {
public UserNotFoundException() {
super();
}
public UserNotFoundException(String message) {
super(message);
}
public UserNotFoundException(String message, Throwable cause) {
super(message, cause);
}
public UserNotFoundException(Throwable cause) {
super(cause);
}
protected UserNotFoundException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
UsernameDuplicatedException.java
package com.lll.store.service.ex;
/** 用户名比占用的异常*/
public class UsernameDuplicatedException extends ServiceException{
public UsernameDuplicatedException() {
}
public UsernameDuplicatedException(String message) {
super(message);
}
public UsernameDuplicatedException(String message, Throwable cause) {
super(message, cause);
}
public UsernameDuplicatedException(Throwable cause) {
super(cause);
}
public UsernameDuplicatedException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
UpdateException.java
package com.lll.store.service.ex;
import org.springframework.context.annotation.Configuration;
/**
* 用户在更新数据时产生的未知异常
*/
public class UpdateException extends ServiceException{
public UpdateException() {
super();
}
public UpdateException(String message) {
super(message);
}
public UpdateException(String message, Throwable cause) {
super(message, cause);
}
public UpdateException(Throwable cause) {
super(cause);
}
public UpdateException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
ServiceException.java
package com.lll.store.service.ex;
/** 业务层异常的基类: throws new ServiceExcption("业务层产生未知的异常")*/
public class ServiceException extends RuntimeException {
public ServiceException() {
super();
}
public ServiceException(String message) {
super(message);
}
public ServiceException(String message, Throwable cause) {
super(message, cause);
}
public ServiceException(Throwable cause) {
super(cause);
}
public ServiceException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
ProductNotFoundException.java
package com.lll.store.service.ex;
/** 商品数据不存在的异常 */
public class ProductNotFoundException extends ServiceException {
public ProductNotFoundException() {
super();
}
public ProductNotFoundException(String message) {
super(message);
}
public ProductNotFoundException(String message, Throwable cause) {
super(message, cause);
}
public ProductNotFoundException(Throwable cause) {
super(cause);
}
protected ProductNotFoundException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
PasswordNotMatchException.java
package com.lll.store.service.ex;
/** 密码验证失败的异常 */
public class PasswordNotMatchException extends ServiceException {
public PasswordNotMatchException() {
super();
}
public PasswordNotMatchException(String message) {
super(message);
}
public PasswordNotMatchException(String message, Throwable cause) {
super(message, cause);
}
public PasswordNotMatchException(Throwable cause) {
super(cause);
}
protected PasswordNotMatchException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
InsertException.java
package com.lll.store.service.ex;
/** 数据再插入过程中所产生异常*/
public class InsertException extends ServiceException{
public InsertException() {
}
public InsertException(String message) {
super(message);
}
public InsertException(String message, Throwable cause) {
super(message, cause);
}
public InsertException(Throwable cause) {
super(cause);
}
public InsertException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
DeleteException.java
package com.lll.store.service.ex;
/** 删除数据失败的异常 */
public class DeleteException extends ServiceException {
public DeleteException() {
super();
}
public DeleteException(String message) {
super(message);
}
public DeleteException(String message, Throwable cause) {
super(message, cause);
}
public DeleteException(Throwable cause) {
super(cause);
}
protected DeleteException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
CartNotFoundException.java
package com.lll.store.service.ex;
/** 购物车数据不存在的异常 */
public class CartNotFoundException extends ServiceException {
public CartNotFoundException() {
super();
}
public CartNotFoundException(String message) {
super(message);
}
public CartNotFoundException(String message, Throwable cause) {
super(message, cause);
}
public CartNotFoundException(Throwable cause) {
super(cause);
}
protected CartNotFoundException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
AddressNotFoundException.java
package com.lll.store.service.ex;
/** 收货地址数据不存在的异常 */
public class AddressNotFoundException extends ServiceException {
public AddressNotFoundException() {
super();
}
public AddressNotFoundException(String message) {
super(message);
}
public AddressNotFoundException(String message, Throwable cause) {
super(message, cause);
}
public AddressNotFoundException(Throwable cause) {
super(cause);
}
protected AddressNotFoundException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
AddressCountLimitException.java
package com.lll.store.service.ex;
/** 收货地址数量达到上限的异常 */
public class AddressCountLimitException extends ServiceException {
public AddressCountLimitException() {
super();
}
public AddressCountLimitException(String message) {
super(message);
}
public AddressCountLimitException(String message, Throwable cause) {
super(message, cause);
}
public AddressCountLimitException(Throwable cause) {
super(cause);
}
protected AddressCountLimitException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
AccessDeniedException.java
package com.lll.store.service.ex;
/** 非法访问的异常 */
public class AccessDeniedException extends ServiceException {
public AccessDeniedException() {
super();
}
public AccessDeniedException(String message) {
super(message);
}
public AccessDeniedException(String message, Throwable cause) {
super(message, cause);
}
public AccessDeniedException(Throwable cause) {
super(cause);
}
protected AccessDeniedException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
2. 接口文件
IUserService.java
package com.lll.store.service;
import com.lll.store.entity.User;
import org.apache.ibatis.annotations.Param;
import java.util.Date;
/**
* 用户模块业务层接口
*/
public interface IUserService {
/**
* 用户注册方法
*
* @param user 用户的数据对象
*/
void reg(User user);
/**
* 用户登录功能
*
* @param username 用户名
* @param password 用户的密码
* @return 当前匹配的用户数据,如果用户没有则返回null值
*/
User login(String username, String password);
void changePassword(Integer uid, String username, String oldPassword, String newPassword);
/**
* 根据用户的id查询用户的数据
*
* @param uid 用户id
* @return 用户的数据
*/
User getByUid(Integer uid);
/**
* 更新用户的数据操作
*
* @param uid 用户的id
* @param username 用户的名称
* @param user 用户对象的数据
*/
void changeInfo(Integer uid, String username, User user);
/**
* 修改用户头像
* @param uid 用户的id
* @param avatar 用户头像的路径
* @param username 用户的名称
*/
void changeAvatar(Integer uid, String avatar, String username);
}
IAddressService.java
package com.lll.store.service;
import com.lll.store.entity.Address;
import java.util.List;
/** 处理收货地址数据的业务层接口 */
public interface IAddressService {
/**
* 创建新的收货地址
* @param uid 当前登录的用户的id
* @param username 当前登录的用户名
* @param address 用户提交的收货地址数据
*/
void addNewAddress(Integer uid, String username, Address address);
List<Address> getByUid(Integer uid);
/**
* 设置默认收货地址
* @param aid 收货地址id
* @param uid 归属的用户id
* @param username 当前登录的用户名
*/
void setDefault(Integer aid, Integer uid, String username);
/**
* 删除收货地址
* @param aid 收货地址id
* @param uid 归属的用户id
* @param username 当前登录的用户名
*/
void delete(Integer aid, Integer uid, String username);
/** 提交订单获取地址
* 根据收货地址数据的id,查询收货地址详情
* @param aid 收货地址id
* @param uid 归属的用户id
* @return 匹配的收货地址详情
*/
Address getByAid(Integer aid, Integer uid);
}
IDistrictService.java
package com.lll.store.service;
import com.lll.store.entity.District;
import java.util.List;
/** 处理省/市/区数据的业务层接口 */
public interface IDistrictService {
/**
* 获取全国所有省/某省所有市/某市所有区
* @param parent 父级代号,当获取某市所有区时,使用市的代号;当获取某省所有市时,使用省的代号;当获取全国所有省时,使用"86"作为父级代号
* @return 全国所有省/某省所有市/某市所有区的列表
*/
List<District> getByParent(String parent);
/**
* 根据省/市/区的行政代号获取省/市/区的名称
* @param code 省/市/区的行政代号
* @return 匹配的省/市/区的名称,如果没有匹配的数据则返回null
*/
String getNameByCode(String code);
}
IProductService.java
package com.lll.store.service;
import com.lll.store.entity.Product;
import java.util.List;
/** 处理商品数据的业务层接口 */
public interface IProductService {
/**
* 查询热销商品的前四名
* @return 热销商品前四名的集合
*/
List<Product> findHotList();
/**
* 根据商品id查询商品详情
* @param id 商品id
* @return 匹配的商品详情,如果没有匹配的数据则返回null
*/
Product findById(Integer id);
}
ICartService.java
package com.lll.store.service;
import com.lll.store.vo.CartVO;
import java.util.List;
/** 处理商品数据的业务层接口 */
public interface ICartService {
/**
* 将商品添加到购物车
* @param uid 当前登录用户的id
* @param pid 商品的id
* @param amount 增加的数量
* @param username 当前登录的用户名
*/
void addToCart(Integer uid, Integer pid, Integer amount, String username);
/**
* 查询某用户的购物车数据
* @param uid 用户id
* @return 该用户的购物车数据的列表
*/
List<CartVO> getVOByUid(Integer uid);
/**
* 将购物车中某商品的数量加1
* @param cid 购物车数量的id
* @param uid 当前登录的用户的id
* @param username 当前登录的用户名
* @return 增加成功后新的数量
*/
Integer addNum(Integer cid, Integer uid, String username);
/**
* 根据若干个购物车数据id查询详情的列表
* @param uid 当前登录的用户的id
* @param cids 若干个购物车数据id
* @return 匹配的购物车数据详情的列表
*/
List<CartVO> getVOByCids(Integer uid, Integer[] cids);
}
IOrderService.java
package com.lll.store.service;
import com.lll.store.entity.Order;
/** 处理订单和订单数据的业务层接口 */
public interface IOrderService {
/**
* 创建订单
* @param aid 收货地址的id
* @param cids 即将购买的商品数据在购物车表中的id
* @param uid 当前登录的用户的id
* @param username 当前登录的用户名
* @return 成功创建的订单数据
*/
Order create(Integer aid, Integer[] cids, Integer uid, String username);
}
3. impl(接口实现类)
UserServiceImpl.java
package com.lll.store.service.impl;
import com.lll.store.entity.User;
import com.lll.store.mapper.UserMapper;
import com.lll.store.service.IUserService;
import com.lll.store.service.ex.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import java.nio.file.attribute.UserPrincipalNotFoundException;
import java.util.Date;
import java.util.UUID;
/**
* 用户模块业务层的实现类
*/
@Service //@Server注解:将当前的对象交给Spring来管理,自动创建对象以及对象的维护
public class UserServiceImpl implements IUserService {
@Autowired
private UserMapper userMapper;
@Autowired
private RedisTemplate redisTemplate;
@Override
public void reg(User user) {
// 通过user参数来获取传递过来的username
String username = user.getUsername();
// 调用findByUsername(username)判断用户是否被注册过
User result = userMapper.findByUsername(username);
// 判断结果集是否为null则抛出用户名被占用的异常
if (result != null) {
//抛出异常
throw new UsernameDuplicatedException("用户名被占用");
}
//密码加密处理的实现:md5算法的形式
//串+password+串---md5算法进行加密,连续加载三次
//盐值 +password+盐值---盐值就是一个随机的字符串
String oldPassword = user.getPassword();
//获取盐值(随即生成一个盐值)全大写
String salt = UUID.randomUUID().toString().toUpperCase();
//将密码和盐值作为一个整体进行加密处理,忽略原有密码的强度提升了数据的安全性
String md5Password = getMD5Password(oldPassword, salt);
//将加密之后的密码重新补全设置到user对象中
user.setPassword(md5Password);
// System.out.println(user.getPassword());
//将盐值补全到user中
user.setSalt(salt);
//补全数据:is_delete设置成0
user.setIsDelete(0);
//补全数据:4个日志字段信息
user.setCreatedUser(user.getUsername());
user.setModifiedUser(user.getUsername());
Date date = new Date();
user.setCreatedTime(date);
user.setModifiedTime(date);
//执行注册业务功能的实现(rows==1)
Integer rows = userMapper.insert(user);
if (rows != 1) {
throw new InsertException("在用户注册过程中产生了未知的异常");
}
}
@Override
public User login(String username, String password) {
//根据用户名称来查询用户的数据是否存在,如果不在则抛出异常
User result = userMapper.findByUsername(username);
if (result == null) {
throw new UserNotFoundException("用户数据不存在");
}
//检测用户的密码是否匹配
//1.先获取到数据库中的加密之后的密码
String oldPassword = result.getPassword();
//2.和用户的传递过来的密码进行比较
//2.1 先获取盐值:上一次注册时所自动生成的盐值
String salt = result.getSalt();
//2.2 将用户的密码按照相同的md5算法的规则进行加密
String newMd5Password = getMD5Password(password, salt);
//3. 将密码进行比较
if (!newMd5Password.equals(oldPassword)) {
throw new PasswordNotMatchException("用户密码错误");
}
//判断is_delete字段的值是否为1表示标记为删除
if (result.getIsDelete() == 1) {
throw new UserNotFoundException("用户数据不存在");
}
//调用mapper层的findByUsername来查询用户的数据,提升系统的性能
User user = new User();
user.setUid(result.getUid());
user.setUsername(result.getUsername());
// 返回有用户的头像
user.setAvatar(result.getAvatar());
// 在Redis缓存中存储,下面一行注释取消掉redis就可以正常使用
// redisTemplate.opsForValue().set("user_" + user.getUid(), user);
// 将当前的用户数据返回,返回的数据是为了辅助其他页面做数据展示使用(uid,username,avatar)
return user;
}
@Override
public void changePassword(Integer uid, String username, String oldPassword, String newPassword) {
User result = userMapper.findByUid(uid);
if (result == null || result.getIsDelete() == 1) {
throw new UserNotFoundException("用户数据不存在");
}
// 原始密码和数据库中的密码进行比较
String oldMd5Password = getMD5Password(oldPassword, result.getSalt());
if (!result.getPassword().equals(oldMd5Password)) {
throw new PasswordNotMatchException("密码错误");
}
//将新的密码设置到数据库中,将新的密码进行加密再去更新
String newMd5Password = getMD5Password(newPassword, result.getSalt());
Integer rows = userMapper.updatePasswordByUid(uid, newMd5Password, username, new Date());
if (rows != 1) {
throw new UpdateException("跟新数据产生未知的异常");
}
}
@Override
public User getByUid(Integer uid) {
User result = userMapper.findByUid(uid);
if (result == null || result.getIsDelete() == 1) {
throw new UserNotFoundException("用户数据不存在");
}
User user = new User();
user.setUsername(result.getUsername());
user.setPhone(result.getPhone());
user.setEmail(result.getEmail());
user.setGender(result.getGender());
return user;
}
/**
* User对象中的数据phone\email\gender,手动再将uid、username封装
* user对象中
*/
@Override
public void changeInfo(Integer uid, String username, User user) {
User result = userMapper.findByUid(uid);
if (result == null || result.getIsDelete() == 1) {
throw new UserNotFoundException("用户数据不存在");
}
// 向参数user中补全数据:uid
user.setUid(uid);
// 向参数user中补全数据:modifiedUser(username)
user.setModifiedUser(username);
// 向参数user中补全数据:modifiedTime(new Date())
user.setModifiedTime(new Date());
// user.setEmail();
// user.setPhone();
// 调用userMapper的updateInfoByUid(User user)方法执行修改,并获取返回值
Integer rows = userMapper.updateInfoByUid(user);
if (rows != 1) {
throw new UpdateException("更新数据时产生未知的异常");
}
}
@Override
public void changeAvatar(Integer uid, String avatar, String username) {
//查询当前用户数据是否存在
User result = userMapper.findByUid(uid);
if (result == null || result.getIsDelete().equals(1)) {
throw new UserNotFoundException("用户数据不存在");
}
Integer rows = userMapper.updateAvatarByUid(uid, avatar, username, new Date());
if (rows != 1) {
throw new UpdateException("更新用户头像产生未知的异常");
}
}
/**
* 定义一个md5算法的加密处理
*/
private String getMD5Password(String password, String salt) {
for (int i = 0; i < 3; i++) {
//md5加密算法的调用(进行三次加密)
password = DigestUtils.md5DigestAsHex((salt + password + salt).getBytes()).toUpperCase();
}
//返回加密后的密码
return password;
}
}
AddressServiceImpl.java
package com.lll.store.service.impl;
import com.lll.store.entity.Address;
import com.lll.store.mapper.AddressMapper;
import com.lll.store.service.IAddressService;
import com.lll.store.service.IDistrictService;
import com.lll.store.service.ex.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
/**
* 新增收货地址
*/
@Service
public class AddressServiceImpl implements IAddressService {
@Autowired
private AddressMapper addressMapper;
// 在添加用户的收货地址的业务层依赖于IDistrictService的业务层接口
@Autowired
private IDistrictService districtService;
@Autowired
private RedisTemplate redisTemplate;
@Value("${user.address.max-count}")
private int maxCount;
@Override
/** 新增地址
* 综合了AddressMapper中insert、countByUid方法
*/
public void addNewAddress(Integer uid, String username, Address address) {
// 根据参数uid调用addressMapper的countByUid(Integer uid)方法,统计当前用户的收货地址数据的数量
Integer count = addressMapper.countByUid(uid);
// 判断数量是否达到上限值
if (count > maxCount) {
// 是:抛出AddressCountLimitException
throw new AddressCountLimitException("收货地址数量已经达到上限(" + maxCount + ")!");
}
// 补全数据:省、市、区的名称
String provinceName = districtService.getNameByCode(address.getProvinceCode());
String cityName = districtService.getNameByCode(address.getCityCode());
String areaName = districtService.getNameByCode(address.getAreaCode());
address.setProvinceName(provinceName);
address.setCityName(cityName);
address.setAreaName(areaName);
// 补全数据:将参数uid封装到参数address中
address.setUid(uid);
// 补全数据:根据以上统计的数量,得到正确的isDefault值(是否默认:0-不默认,1-默认),并封装
Integer isDefault = count == 0 ? 1 : 0;
address.setIsDefault(isDefault);
// 补全数据:4项日志
Date now = new Date();
address.setCreatedUser(username);
address.setCreatedTime(now);
address.setModifiedUser(username);
address.setModifiedTime(now);
// 调用addressMapper的insert(Address address)方法插入收货地址数据,并获取返回的受影响行数
Integer rows = addressMapper.insert(address);
// Redis存入缓存
redisTemplate.opsForValue().set("address_" + address.getAid(), address);
// 判断受影响行数是否不为1
if (rows != 1) {
// 是:抛出InsertException
throw new InsertException("插入收货地址数据时出现未知错误,请联系系统管理员!");
}
}
@Override
public List<Address> getByUid(Integer uid) {
List<Address> list = addressMapper.findByUid(uid);
// for (Address address : list) {
// address.setAid(null);
// address.setUid(null);
// address.setProvinceCode(null);
// address.setCityCode(null);
// address.setAreaCode(null);
// address.setCreatedUser(null);
// address.setCreatedTime(null);
// address.setModifiedUser(null);
// address.setModifiedTime(null);
// }
return list;
}
@Override
public void setDefault(Integer aid, Integer uid, String username) {
// 根据参数aid,调用addressMapper中的findByAid()查询收货地址数据
Address result = addressMapper.findByAid(aid);
// 判断查询结果是否为null
if (result == null) {
// 是:抛出AddressNotFoundException
throw new AddressNotFoundException("尝试访问的收货地址数据不存在");
}
// 判断查询结果中的uid与参数uid是否不一致(使用equals()判断)
if (!result.getUid().equals(uid)) {
// 是:抛出AccessDeniedException
throw new AccessDeniedException("非法访问的异常");
}
// 调用addressMapper的updateNonDefaultByUid()将该用户的所有收货地址全部设置为非默认,并获取返回受影响的行数
Integer rows = addressMapper.updateNonDefaultByUid(uid);
// 判断受影响的行数是否小于1(不大于0)
if (rows < 1) {
// 是:抛出UpdateException
throw new UpdateException("设置默认收货地址时出现未知错误[1]");
}
// 调用addressMapper的updateDefaultByAid()将指定aid的收货地址设置为默认,并获取返回的受影响的行数
rows = addressMapper.updateDefaultByAid(aid, username, new Date());
// 判断受影响的行数是否不为1
if (rows != 1) {
// 是:抛出UpdateException
throw new UpdateException("设置默认收货地址时出现未知错误[2]");
}
}
@Override
public void delete(Integer aid, Integer uid, String username) {
// 根据参数aid,调用findByAid()查询收货地址数据
Address result = addressMapper.findByAid(aid);
// 判断查询结果是否为null
if (result == null) {
// 是:抛出AddressNotFoundException
throw new AddressNotFoundException("尝试访问的收货地址数据不存在");
}
// 判断查询结果中的uid与参数uid是否不一致(使用equals()判断)
if (!result.getUid().equals(uid)) {
// 是:抛出AccessDeniedException:非法访问
throw new AccessDeniedException("非常访问");
}
// 根据参数aid,调用deleteByAid()执行删除
Integer rows1 = addressMapper.deleteByAid(aid);
if (rows1 != 1) {
throw new DeleteException("删除收货地址数据时出现未知错误,请联系系统管理员");
}
// 判断查询结果中的isDefault是否为0
if (result.getIsDefault() == 0) {
return;
}
// 调用持久层的countByUid()统计目前还有多少收货地址
Integer count = addressMapper.countByUid(uid);
// 判断目前的收货地址的数量是否为0
if (count == 0) {
return;
}
// 调用findLastModified()找出用户最近修改的收货地址数据
Address lastModified = addressMapper.findLastModified(uid);
// 从以上查询结果中找出aid属性值
Integer lastModifiedAid = lastModified.getAid();
// 调用持久层的updateDefaultByAid()方法执行设置默认收货地址,并获取返回的受影响的行数
Integer rows2 = addressMapper.updateDefaultByAid(lastModifiedAid, username, new Date());
// 判断受影响的行数是否不为1
if (rows2 != 1) {
// 是:抛出UpdateException
throw new UpdateException("更新收货地址数据时出现未知错误,请联系系统管理员");
}
}
//提交订单获取地址
@Override
public Address getByAid(Integer aid, Integer uid) {
// 根据收货地址数据id,查询收货地址详情
Address address = addressMapper.findByAid(aid);
if (address == null) {
throw new AddressNotFoundException("尝试访问的收货地址数据不存在");
}
if (!address.getUid().equals(uid)) {
throw new AccessDeniedException("非法访问");
}
address.setProvinceCode(null);
address.setCityCode(null);
address.setAreaCode(null);
address.setCreatedUser(null);
address.setCreatedTime(null);
address.setModifiedUser(null);
address.setModifiedTime(null);
return address;
}
}
DistrictServiceImpl.java
package com.lll.store.service.impl;
import com.lll.store.entity.District;
import com.lll.store.mapper.DistrictMapper;
import com.lll.store.service.IDistrictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/** 处理省/市/区数据的业务层实现类 */
@Service
public class DistrictServiceImpl implements IDistrictService {
@Autowired
private DistrictMapper districtMapper;
@Override
public List<District> getByParent(String parent) {
List<District> list = districtMapper.findByParent(parent);
for (District district : list) {
district.setId(null);
district.setParent(null);
}
return list;
}
@Override
public String getNameByCode(String code) {
return districtMapper.findNameByCode(code);
}
}
ProductServiceImpl.java
package com.lll.store.service.impl;
import com.lll.store.entity.Product;
import com.lll.store.mapper.ProductMapper;
import com.lll.store.service.IProductService;
import com.lll.store.service.ex.ProductNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/** 处理商品数据的业务层实现类 */
@Service
public class ProductServiceImpl implements IProductService {
@Autowired
private ProductMapper productMapper;
@Override
public List<Product> findHotList() {
List<Product> list = productMapper.findHotList();
for (Product product : list) {
product.setPriority(null);
product.setCreatedUser(null);
product.setCreatedTime(null);
product.setModifiedUser(null);
product.setModifiedTime(null);
}
return list;
}
@Override
public Product findById(Integer id) {
// 根据参数id调用私有方法执行查询,获取商品数据
Product product = productMapper.findById(id);
// 判断查询结果是否为null
if (product == null) {
// 是:抛出ProductNotFoundException
throw new ProductNotFoundException("尝试访问的商品数据不存在");
}
// 将查询结果中的部分属性设置为null
product.setPriority(null);
product.setCreatedUser(null);
product.setCreatedTime(null);
product.setModifiedUser(null);
product.setModifiedTime(null);
// 返回查询结果
return product;
}
}
CartServiceImpl.java
package com.lll.store.service.impl;
import com.lll.store.entity.Cart;
import com.lll.store.entity.Product;
import com.lll.store.mapper.CartMapper;
import com.lll.store.mapper.ProductMapper;
import com.lll.store.service.ICartService;
import com.lll.store.service.IProductService;
import com.lll.store.service.ex.AccessDeniedException;
import com.lll.store.service.ex.CartNotFoundException;
import com.lll.store.service.ex.InsertException;
import com.lll.store.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
/** 处理购物车数据的业务层实现类 */
@Service
public class CartServiceImpl implements ICartService {
// 购物车的业务层依赖于购物车的持久层以及商品的持久层
@Autowired
private CartMapper cartMapper;
@Autowired
private ProductMapper productMapper;
@Override
public void addToCart(Integer uid, Integer pid, Integer amount, String username) {
// 根据参数pid和uid查询购物车中的数据
Cart result = cartMapper.findByUidAndPid(uid, pid);
Date now = new Date();
// 判断查询结果是否为null
if (result == null) {
// 是:表示该用户并未将该商品添加到购物车
// 创建Cart对象
Cart cart = new Cart();
// 封装数据:uid,pid,amount
cart.setUid(uid);
cart.setPid(pid);
cart.setNum(amount);
// 调用productService.findById(pid)查询商品数据,得到商品价格,来自于商品中的数据
Product product = productMapper.findById(pid);
// 封装数据:price
cart.setPrice(product.getPrice());
// 封装数据:4个日志
cart.setCreatedUser(username);
cart.setCreatedTime(now);
cart.setModifiedUser(username);
cart.setModifiedTime(now);
// 调用insert(cart)执行将数据插入到数据表中
Integer rows = cartMapper.insert(cart);
if (rows != 1) {
throw new InsertException("插入商品数据时出现未知错误,请联系系统管理员");
}
} else {
// 否:表示该用户的购物车中已有该商品
// 从查询结果中获取购物车数据的id
Integer cid = result.getCid();
// 从查询结果中取出原数量,与参数amount相加,得到新的数量
Integer num = result.getNum() + amount;
// 执行更新数量
Integer rows = cartMapper.updateNumByCid(cid, num, username, now);
if (rows != 1) {
throw new InsertException("修改商品数量时出现未知错误,请联系系统管理员");
}
}
}
@Override
public List<CartVO> getVOByUid(Integer uid) {
return cartMapper.findVOByUid(uid);
}
@Override
public Integer addNum(Integer cid, Integer uid, String username) {
// 调用findByCid(cid)根据参数cid查询购物车数据
Cart result = cartMapper.findByCid(cid);
// 判断查询结果是否为null
if (result == null) {
// 是:抛出CartNotFoundException
throw new CartNotFoundException("尝试访问的购物车数据不存在");
}
// 判断查询结果中的uid与参数uid是否不一致
if (!result.getUid().equals(uid)) {
// 是:抛出AccessDeniedException
throw new AccessDeniedException("非法访问");
}
// 可选:检查商品的数量是否大于多少(适用于增加数量)或小于多少(适用于减少数量)
// 根据查询结果中的原数量增加1得到新的数量num
Integer num = result.getNum() + 1;
// 创建当前时间对象,作为modifiedTime
Date now = new Date();
// 调用updateNumByCid(cid, num, modifiedUser, modifiedTime)执行修改数量
Integer rows = cartMapper.updateNumByCid(cid, num, username, now);
if (rows != 1) {
throw new InsertException("修改商品数量时出现未知错误,请联系系统管理员");
}
// 返回新的数量
return num;
}
@Override
public List<CartVO> getVOByCids(Integer uid, Integer[] cids) {
List<CartVO> list = cartMapper.findVOByCids(cids);
/**
for (CartVO cart : list) {
if (!cart.getUid().equals(uid)) {
list.remove(cart);
}
}
*/
Iterator<CartVO> it = list.iterator();
while (it.hasNext()) {
CartVO cart = it.next();
if (!cart.getUid().equals(uid)) {// 表示当前数据不属于当前用户
// 从集合中一处这个元素
it.remove();
}
}
return list;
}
}
OrderServiceImpl.java
package com.lll.store.service.impl;
import com.lll.store.entity.Address;
import com.lll.store.entity.Order;
import com.lll.store.entity.OrderItem;
import com.lll.store.mapper.OrderMapper;
import com.lll.store.service.IAddressService;
import com.lll.store.service.ICartService;
import com.lll.store.service.IOrderService;
import com.lll.store.service.ex.InsertException;
import com.lll.store.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
/** 处理订单和订单数据的业务层实现类 */
@Service
public class OrderServiceImpl implements IOrderService {
@Autowired
private OrderMapper orderMapper;
@Autowired
private IAddressService addressService;
@Autowired
private ICartService cartService;
@Transactional
@Override
public Order create(Integer aid, Integer[] cids, Integer uid, String username) {
// 创建当前时间对象
Date now = new Date();
// 根据cids查询所勾选的购物车列表中的数据
List<CartVO> carts = cartService.getVOByCids(uid, cids);
// 计算这些商品的总价
long totalPrice = 0;
for (CartVO cart : carts) {
totalPrice += cart.getRealPrice() * cart.getNum();
}
// 创建订单数据对象
Order order = new Order();
// 补全数据:uid
order.setUid(uid);
// 查询收货地址数据
Address address = addressService.getByAid(aid, uid);
// 补全数据:收货地址相关的6项
order.setRecvName(address.getName());
order.setRecvPhone(address.getPhone());
order.setRecvProvince(address.getProvinceName());
order.setRecvCity(address.getCityName());
order.setRecvArea(address.getAreaName());
order.setRecvAddress(address.getAddress());
// 补全数据:totalPrice
order.setTotalPrice(totalPrice);
// 补全数据:status
order.setStatus(0);
// 补全数据:下单时间
order.setOrderTime(now);
// 补全数据:日志
order.setCreatedUser(username);
order.setCreatedTime(now);
order.setModifiedUser(username);
order.setModifiedTime(now);
// 插入订单数据
Integer rows1 = orderMapper.insertOrder(order);
if (rows1 != 1) {
throw new InsertException("插入订单数据时出现未知错误,请联系系统管理员");
}
// 遍历carts,循环插入订单商品数据
for (CartVO cart : carts) {
// 创建订单商品数据
OrderItem item = new OrderItem();
// 补全数据:setOid(order.getOid())
item.setOid(order.getOid());
// 补全数据:pid, title, image, price, num
item.setPid(cart.getPid());
item.setTitle(cart.getTitle());
item.setImage(cart.getImage());
item.setPrice(cart.getRealPrice());
item.setNum(cart.getNum());
// 补全数据:4项日志
item.setCreatedUser(username);
item.setCreatedTime(now);
item.setModifiedUser(username);
item.setModifiedTime(now);
// 插入订单商品数据
Integer rows2 = orderMapper.insertOrderItem(item);
if (rows2 != 1) {
throw new InsertException("插入订单商品数据时出现未知错误,请联系系统管理员");
}
}
// 返回
return order;
}
}
八、controller
ex
FileEmptyException.java
package com.lll.store.controller.ex;
/** 上传的文件为空的异常,例如没有选择上传的文件就提交了表单,或选择的文件是0字节的空文件 */
public class FileEmptyException extends FileUploadException {
public FileEmptyException() {
super();
}
public FileEmptyException(String message) {
super(message);
}
public FileEmptyException(String message, Throwable cause) {
super(message, cause);
}
public FileEmptyException(Throwable cause) {
super(cause);
}
protected FileEmptyException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
FileSizeException.java
package com.lll.store.controller.ex;
/** 上传的文件的大小超出了限制值 */
public class FileSizeException extends FileUploadException {
public FileSizeException() {
super();
}
public FileSizeException(String message) {
super(message);
}
public FileSizeException(String message, Throwable cause) {
super(message, cause);
}
public FileSizeException(Throwable cause) {
super(cause);
}
protected FileSizeException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
FileStateException.java
package com.lll.store.controller.ex;
/** 上传的文件状态异常 */
public class FileStateException extends FileUploadException {
public FileStateException() {
super();
}
public FileStateException(String message) {
super(message);
}
public FileStateException(String message, Throwable cause) {
super(message, cause);
}
public FileStateException(Throwable cause) {
super(cause);
}
protected FileStateException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
FileTypeException.java
package com.lll.store.controller.ex;
/** 上传的文件类型超出了限制 */
public class FileTypeException extends FileUploadException {
public FileTypeException() {
super();
}
public FileTypeException(String message) {
super(message);
}
public FileTypeException(String message, Throwable cause) {
super(message, cause);
}
public FileTypeException(Throwable cause) {
super(cause);
}
protected FileTypeException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
FileUploadException.java
package com.lll.store.controller.ex;
/** 文件上传相关异常的基类 */
public class FileUploadException extends RuntimeException {
public FileUploadException() {
super();
}
public FileUploadException(String message) {
super(message);
}
public FileUploadException(String message, Throwable cause) {
super(message, cause);
}
public FileUploadException(Throwable cause) {
super(cause);
}
protected FileUploadException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
FileUploadIOException.java
package com.lll.store.controller.ex;
/** 上传文件时读写异常 */
public class FileUploadIOException extends FileUploadException {
public FileUploadIOException() {
super();
}
public FileUploadIOException(String message) {
super(message);
}
public FileUploadIOException(String message, Throwable cause) {
super(message, cause);
}
public FileUploadIOException(Throwable cause) {
super(cause);
}
protected FileUploadIOException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
控制器
UserController.java
package com.lll.store.controller;
import com.lll.store.controller.ex.*;
import com.lll.store.entity.User;
import com.lll.store.service.IUserService;
import com.lll.store.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
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 IUserService userService;
/**
* 约定大于配置:开发细想来完成,省略大量的配置甚至注解的编写
* 1.接收数据方式:请求处理方法参数列表设置为pojo类型来接收前端的数据,
* SpringBoot会将前端数据的url地址中的参数名和pojo类的属性名进行比较,如果
* 这两个名称项目,则将值注入到pojo类中对应的属性上
*/
@PostMapping("reg")
public JsonResult<Void> reg(User user) {
userService.reg(user);
return new JsonResult<Void>(OK);
}
/**
@RequestMapping("reg") public JsonResult<Void> reg(User user){
//创建响应结果对象
JsonResult<Void> result = new JsonResult<>();//抛出异常
try {
userService.reg(user);
result.setState(200);
result.setMessage("用户注册成功");
} catch (UsernameDuplicatedException e) {
result.setState(4000);
result.setMessage("用户名被占用");
}catch (InsertException e) {
result.setState(5000);
result.setMessage("注册时产生未知的异常");
}
return result;
}
*/
/**
* 2.接收数据方法:请求处理方法的参数列表设置为非pojo类型
* SpringBoot会直接将请求的参数名和方法的参数名直接进行比较,如果名称相同
* 则自动完成值得依赖注入
*/
@RequestMapping("login")
public JsonResult<User> login(String username, String password, HttpSession session) {
User data = userService.login(username, password);
//向session对象中完成数据的绑定(session全局的)
session.setAttribute("uid", data.getUid());
session.setAttribute("username", data.getUsername());
//获取session中绑定的数据
// System.out.println(getuidFromSession(session));
// System.out.println(getUsernameFromSession(session));
return new JsonResult<User>(OK, data);
}
@RequestMapping("change_password")
public JsonResult<Void> changePassword(String oldPassword, String newPassword, HttpSession session) {
// System.out.println("lllllll");
Integer uid = getUidFromSession(session);
String username = getUsernameFromSession(session);
// System.out.println(uid);
userService.changePassword(uid, username, oldPassword, newPassword);
return new JsonResult<Void>(OK);
}
@RequestMapping("get_by_uid")
public JsonResult<User> getByUid(HttpSession session) {
User data = userService.getByUid(getUidFromSession(session));
return new JsonResult<>(OK, data);
}
@RequestMapping("change_info")
public JsonResult<Void> changeInfo(User user, HttpSession session) {
// user对象有四部分的数据:username、phone、Email、gender、
// uid数据需要再次封装到user对象中
Integer uid = getUidFromSession(session);
String username = getUsernameFromSession(session);
userService.changeInfo(uid, username, user);
return new JsonResult<>(OK);
}
/**
* 设置上传文件的最大值
**/
public static final int AVATAR_MAX_SIZE = 10 * 1024 * 1024;
/**
* 限制上传文件的类型
**/
public static final List<String> AVATAR_TYPE = new ArrayList<>();
static {
AVATAR_TYPE.add("images/jpeg");
AVATAR_TYPE.add("images/png");
AVATAR_TYPE.add("images/bmp");
AVATAR_TYPE.add("images/gif");
}
/**
* MultipartFile接口是SpringMVC提供的一个接口,这个接口为我们包装了
* 获取文件类型的数据(任何类型的file都可以接收),SpringBoot它有整合了
* SpringMVC,只需要在处理请求的方法参数列表上声明了应该参数类型为MultipartFile
* 的参数,然后SpringBoot自动将传递给服务的文件数据赋值给这个参数
*
* @param session
* @param file
* @return
* @RequestParam 表示请求中的参数,将请求中的参数注入请求处理方法的某个参数上,
* 如果名称不一致则可以使用@RequestParam注解进行标记和映射
*/
@RequestMapping("change_avatar")
public JsonResult<String> changeAvatar(HttpSession session, @RequestParam("file") MultipartFile file) {
// 判断文件是否为null
if (file.isEmpty()) {
throw new FileEmptyException("文件为空");
}
if (file.getSize() > AVATAR_MAX_SIZE) {
throw new FileSizeException("文件超出限制");
}
// 判断文件的类型是否包含我们规定的后缀类型
String contentType = file.getContentType();
// 如果集合包含某个元素则返回值true
if (AVATAR_TYPE.contains(contentType)) {
throw new FileTypeException("文件类型不支持");
}
// 上传的文件.../upload/文件.png
String parent = session.getServletContext().getRealPath("upload");
// File对象指向这个路径,File是否存在
File dir = new File(parent);
if (!dir.exists()) {// 检测目录是否存在
dir.mkdirs();//创建当前的目录
}
// 获取到这个文件名称,UUID工具来将生成一个新的字符串作为文件名
// 例如:avatar01.png
String originalFilename = file.getOriginalFilename();
System.out.println("originalFilename=" + originalFilename);
int index = originalFilename.lastIndexOf(".");
String suffix = originalFilename.substring(index);
String filename = UUID.randomUUID().toString().toUpperCase() + suffix;
File dest = new File(dir, filename);// 是一个空文件
// 参数file中数据写入到这个空文件中
try {
file.transferTo(dest);// 将file文件中的数据写入到dest文件
} catch (FileStateException e) {
throw new FileStateException("文件状态异常");
} catch (IOException e) {
throw new FileUploadIOException("文件读写异常");
}
Integer uid = getUidFromSession(session);
String username = getUsernameFromSession(session);
// 返回头像的路径/upload/test.png
String avatar = "/upload/" + filename;
userService.changeAvatar(uid, avatar, username);
// 返回用户头像的路径给前端页面,将来用于头像展示使用
return new JsonResult<>(OK,avatar);
}
}
BaseController.java
package com.lll.store.controller;
import com.lll.store.controller.ex.*;
import com.lll.store.entity.User;
import com.lll.store.service.ex.*;
import com.lll.store.util.JsonResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import javax.servlet.http.HttpSession;
/**
* 控制层的基类
*/
public class BaseController {
//操作成功的状态码
public static final int OK = 200;
//请求处理方法,这个方法的返回值就是需要传递给前端的数据
//自动将异常对象传递给此方法的参数列表上
//当前项目中产生了异常,被统一拦截到此方法中,这个方法此时就充当的是请求处理方法,方法的返回值会直接给到前端
@ExceptionHandler({ServiceException.class, FileUploadException.class})//用于统一处理抛出的异常
public JsonResult<Void> handleException(Throwable e) {
JsonResult<Void> result = new JsonResult<>(e);
if (e instanceof UsernameDuplicatedException) {
result.setState(4000);
result.setMessage("用户名已经被占用");
} else if (e instanceof UserNotFoundException) {
result.setState(5001);
result.setMessage("用户数据不存在的异常");
} else if (e instanceof PasswordNotMatchException) {
result.setState(5002);
result.setMessage("用户名的密码错误的异常");
} else if (e instanceof AddressCountLimitException) {
result.setState(4003);
result.setMessage("用户的收货地址超出上限异常");
} else if (e instanceof AddressNotFoundException) {
result.setState(4004);
result.setMessage("用户的收货地址数据不存在的异常");
} else if (e instanceof AccessDeniedException) {
result.setState(4005);
result.setMessage("收货地址数据非法访问的异常");
} else if (e instanceof ProductNotFoundException) {
result.setState(4006);
result.setMessage("商品不存在的异常");
} else if (e instanceof InsertException) {
result.setState(5000);
result.setMessage("注册时产生未知的异常");
} else if (e instanceof InsertException) {
result.setState(5001);
result.setMessage("更新数据时产生未知的异常");
} else if (e instanceof DeleteException) {
result.setState(5002);
result.setMessage("删除数据时产生未知的异常");
} else if (e instanceof FileEmptyException) {
result.setState(6000);
} else if (e instanceof FileSizeException) {
result.setState(6001);
} else if (e instanceof FileTypeException) {
result.setState(6002);
} else if (e instanceof FileStateException) {
result.setState(6003);
} else if (e instanceof FileUploadIOException) {
result.setState(6004);
}
return result;
}
/**
* 获取session对象中的uid
*
* @param session session对象
* @return 当前登录的用户的uid的值
*/
protected final Integer getUidFromSession(HttpSession session) {
return Integer.valueOf(session.getAttribute("uid").toString());
}
/**
* 获取当前登录用户的username
*
* @param session session对象
* @return 当前登录用户的用户名
* 在实现类中重写父类的toString(),不是句柄信息的输出
*/
protected final String getUsernameFromSession(HttpSession session) {
return session.getAttribute("username").toString();
}
}
AddressController.java
package com.lll.store.controller;
import com.lll.store.entity.Address;
import com.lll.store.service.IAddressService;
import com.lll.store.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
import java.util.List;
@RequestMapping("addresses")
@RestController
public class AddressController extends BaseController {
@Autowired
private IAddressService addressService;
@RequestMapping("add_new_address")
public JsonResult<Void> addNewAddress(Address address, HttpSession session) {
// 从Session中获取uid和username
Integer uid = getUidFromSession(session);
String username = getUsernameFromSession(session);
// 调用业务对象的方法执行业务
addressService.addNewAddress(uid, username, address);
// 响应成功
return new JsonResult<Void>(OK);
}
// @RequestMapping({"", "/"}) 字符串和斜杠都可以
@RequestMapping({"", "/"})
public JsonResult<List<Address>> getByUid(HttpSession session) {
Integer uid = getUidFromSession(session);
List<Address> data = addressService.getByUid(uid);
return new JsonResult<List<Address>>(OK, data);
}
@RequestMapping("{aid}/set_default")
public JsonResult<Void> setDefault(@PathVariable("aid") Integer aid, HttpSession session) {
Integer uid = getUidFromSession(session);
String username = getUsernameFromSession(session);
addressService.setDefault(aid, uid, username);
return new JsonResult<Void>(OK);
}
@RequestMapping("{aid}/delete")
public JsonResult<Void> delete(@PathVariable("aid") Integer aid, HttpSession session) {
Integer uid = getUidFromSession(session);
String username = getUsernameFromSession(session);
addressService.delete(aid, uid, username);
return new JsonResult<Void>(OK);
}
}
DistrictController.java
package com.lll.store.controller;
import com.lll.store.entity.District;
import com.lll.store.service.IDistrictService;
import com.lll.store.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("districts")
public class DistrictController extends BaseController {
@Autowired
private IDistrictService districtService;
//districts开头的请求都被拦截到getByParent方法
@RequestMapping({"/", ""})
//@RequestMapping(method={RequestMethod.GET})
public JsonResult<List<District>> getByParent(String parent) {
List<District> data = districtService.getByParent(parent);
return new JsonResult<>(OK, data);
}
}
ProductController.java
package com.lll.store.controller;
import com.lll.store.entity.Product;
import com.lll.store.service.IProductService;
import com.lll.store.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("products")
public class ProductController extends BaseController {
@Autowired
private IProductService productService;
@RequestMapping("hot_list")
public JsonResult<List<Product>> getHotList() {
List<Product> data = productService.findHotList();
return new JsonResult<List<Product>>(OK, data);
}
@GetMapping("{id}/details")
public JsonResult<Product> getById(@PathVariable("id") Integer id) {
// 调用业务对象执行获取数据
Product data = productService.findById(id);
// 返回成功和数据
return new JsonResult<Product>(OK, data);
}
}
CartController.java
package com.lll.store.controller;
import com.lll.store.service.ICartService;
import com.lll.store.util.JsonResult;
import com.lll.store.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
import java.util.List;
@RestController
@RequestMapping("carts")
public class CartController extends BaseController {
@Autowired
private ICartService cartService;
@RequestMapping("add_to_cart")
public JsonResult<Void> addToCart(Integer pid, Integer amount, HttpSession session) {
// System.out.println("pid=" + pid);
// System.out.println("amount=" + amount);
// 从Session中获取uid和username
Integer uid = getUidFromSession(session);
String username = getUsernameFromSession(session);
// 调用业务对象执行添加到购物车
cartService.addToCart(uid, pid, amount, username);
// 返回成功
return new JsonResult<Void>(OK);
}
@GetMapping({"", "/"})
public JsonResult<List<CartVO>> getVOByUid(HttpSession session) {
// 从Session中获取uid
Integer uid = getUidFromSession(session);
// 调用业务对象执行查询数据
List<CartVO> data = cartService.getVOByUid(uid);
// 返回成功与数据
return new JsonResult<List<CartVO>>(OK, data);
}
@RequestMapping("{cid}/num/add")
public JsonResult<Integer> addNum(@PathVariable("cid") Integer cid, HttpSession session) {
// 从Session中获取uid和username
Integer uid = getUidFromSession(session);
String username = getUsernameFromSession(session);
// 调用业务对象执行增加数量
Integer data = cartService.addNum(cid, uid, username);
// 返回成功
return new JsonResult<Integer>(OK, data);
}
@GetMapping("list")
public JsonResult<List<CartVO>> getVOByCids(Integer[] cids, HttpSession session) {
// 从Session中获取uid
Integer uid = getUidFromSession(session);
// 调用业务对象执行查询数据
List<CartVO> data = cartService.getVOByCids(uid, cids);
// 返回成功与数据
return new JsonResult<>(OK, data);
}
}
OrderController.java
package com.lll.store.controller;
import com.lll.store.entity.Order;
import com.lll.store.service.IOrderService;
import com.lll.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 javax.servlet.http.HttpSession;
@RestController
@RequestMapping("orders")
public class OrderController extends BaseController {
@Autowired
private IOrderService orderService;
@RequestMapping("create")
public JsonResult<Order> create(Integer aid, Integer[] cids, HttpSession session) {
// 从Session中取出uid和username
Integer uid = getUidFromSession(session);
String username = getUsernameFromSession(session);
// 调用业务对象执行业务
Order data = orderService.create(aid, cids, uid, username);
// 返回成功与数据
return new JsonResult<Order>(OK, data);
}
}
总结
数据库文件和前端页面我都放gitee了
gieee地址