maven之packaging标签
(1)项目创建父模块
首先设置 下Maven
Maven:仓库地址:这里是腾讯云仓库
作为父模块,src没用,干掉src
这里我们是Maven创建的项目,想要项目变成SpringBoot的项目,需要引入SpringBoot父工程:也可以通过SpringBoot的方式创建项目
引入依赖管理:这里是对依赖进行管理,并不是引入实际的依赖
<!--定义jar包的版本号--><properties><java.version>1.8</java.version><cloud.version>Hoxton.SR8</cloud.version><alibaba.version>2.2.5.RELEASE</alibaba.version><gmall.version>1.0-SNAPSHOT</gmall.version><mybatis-plus.version>3.4.1</mybatis-plus.version><mysql.version>5.1.46</mysql.version><swagger.version>2.7.0</swagger.version><lombok.version>1.18.10</lombok.version><fastjson.version>1.2.29</fastjson.version><redisson.version>3.15.3</redisson.version><pool2.version>2.6.0</pool2.version><httpclient.version>4.5.13</httpclient.version></properties><!--配置dependencyManagement锁定依赖的版本 并不是实际的依赖。--><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${cloud.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>${alibaba.version}</version><type>pom</type><scope>import</scope></dependency><!--mybatis-plus 持久层--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>${mybatis-plus.version}</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql.version}</version></dependency><!--swagger--><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>${swagger.version}</version></dependency><!--swagger ui--><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>${swagger.version}</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>${lombok.version}</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>${fastjson.version}</version></dependency><!-- redisson 分布式锁--><dependency><groupId>org.redisson</groupId><artifactId>redisson</artifactId><version>${redisson.version}</version></dependency><!-- spring2.X集成redis所需common-pool2--><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId><version>${pool2.version}</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>${httpclient.version}</version></dependency></dependencies></dependencyManagement>
作为父工程的话,加个<packaging> pom </packaging>
(2) 搭建common父模块
common:公共模块父节点
common-util:工具类模块,所有模块都可以依赖于它
service-util:service服务的工具包,包含service服务的公共配置类,所有 service模块依赖于它
点击gmall-parent,选择New–>Module,操作如下
commmon也是个父工程加:
引入依赖:
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><scope>provided </scope></dependency><!--lombok用来简化实体类:需要安装lombok插件--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><!--swagger--><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId></dependency><!--用来转换json使用 {JavaObject - json | json - JavaObject}--><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId></dependency><!-- 服务调用feign --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId><scope>provided </scope></dependency></dependencies>
2.1搭建common-util模块
点击common,选择New–>Module,操作如下
修改pom:不声明就是jar,可以声明一下,说明他是jar包
添加依赖:
<dependencies><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId></dependency></dependencies>
在下面添加工具类:
GmallException :自定义全局异常类:
package com.atguigu.gmall.common.execption;import com.atguigu.gmall.common.result.ResultCodeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;/*** 自定义全局异常类*/
@Data
@ApiModel(value = "自定义全局异常类")
public class GmallException extends RuntimeException {@ApiModelProperty(value = "异常状态码")private Integer code;/*** 通过状态码和错误消息创建异常对象* @param message* @param code*/public GmallException(String message, Integer code) {super(message);this.code = code;}/*** 接收枚举类型对象* @param resultCodeEnum*/public GmallException(ResultCodeEnum resultCodeEnum) {super(resultCodeEnum.getMessage());this.code = resultCodeEnum.getCode();}@Overridepublic String toString() {return "GuliException{" +"code=" + code +", message=" + this.getMessage() +'}';}
}
Result:全局统一返回结果类
package com.atguigu.gmall.common.result;import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;/*** 全局统一返回结果类**/
@Data
@ApiModel(value = "全局统一返回结果")
public class Result<T> {@ApiModelProperty(value = "返回码")private Integer code;@ApiModelProperty(value = "返回消息")private String message;@ApiModelProperty(value = "返回数据")private T data;public Result(){}// 返回数据protected static <T> Result<T> build(T data) {Result<T> result = new Result<T>();if (data != null)result.setData(data);return result;}public static <T> Result<T> build(T body, ResultCodeEnum resultCodeEnum) {Result<T> result = build(body);result.setCode(resultCodeEnum.getCode());result.setMessage(resultCodeEnum.getMessage());return result;}public static<T> Result<T> ok(){return Result.ok(null);}/*** 操作成功* @param data* @param <T>* @return*/public static<T> Result<T> ok(T data){Result<T> result = build(data);return build(data, ResultCodeEnum.SUCCESS);}public static<T> Result<T> fail(){return Result.fail(null);}/*** 操作失败* @param data* @param <T>* @return*/public static<T> Result<T> fail(T data){Result<T> result = build(data);return build(data, ResultCodeEnum.FAIL);}public Result<T> message(String msg){this.setMessage(msg);return this;}public Result<T> code(Integer code){this.setCode(code);return this;}public boolean isOk() {if(this.getCode().intValue() == ResultCodeEnum.SUCCESS.getCode().intValue()) {return true;}return false;}
}
ResultCodeEnum:统一返回结果状态信息类
package com.atguigu.gmall.common.result;import lombok.Getter;/*** 统一返回结果状态信息类**/
@Getter
public enum ResultCodeEnum {SUCCESS(200,"成功"),FAIL(201, "失败"),SERVICE_ERROR(2012, "服务异常"),ILLEGAL_REQUEST( 204, "非法请求"),PAY_RUN(205, "支付中"),LOGIN_AUTH(208, "未登陆"),PERMISSION(209, "没有权限"),SECKILL_NO_START(210, "秒杀还没开始"),SECKILL_RUN(211, "正在排队中"),SECKILL_NO_PAY_ORDER(212, "您有未支付的订单"),SECKILL_FINISH(213, "已售罄"),SECKILL_END(214, "秒杀已结束"),SECKILL_SUCCESS(215, "抢单成功"),SECKILL_FAIL(216, "抢单失败"),SECKILL_ILLEGAL(217, "请求不合法"),SECKILL_ORDER_SUCCESS(218, "下单成功"),COUPON_GET(220, "优惠券已经领取"),COUPON_LIMIT_GET(221, "优惠券已发放完毕"),;private Integer code;private String message;private ResultCodeEnum(Integer code, String message) {this.code = code;this.message = message;}
}
util下:
AuthContextHolder :获取登录用户信息类
package com.atguigu.gmall.common.util;//import com.baomidou.mybatisplus.core.toolkit.StringUtils;import org.springframework.util.StringUtils;import javax.servlet.http.HttpServletRequest;/*** 获取登录用户信息类*/
public class AuthContextHolder {/*** 获取当前登录用户id* @param request* @return*/public static String getUserId(HttpServletRequest request) {String userId = request.getHeader("userId");return StringUtils.isEmpty(userId) ? "" : userId;}/*** 获取当前未登录临时用户id* @param request* @return*/public static String getUserTempId(HttpServletRequest request) {String userTempId = request.getHeader("userTempId");return StringUtils.isEmpty(userTempId) ? "" : userTempId;}
}
DateUtil:日期操作工具类
package com.atguigu.gmall.common.util;import org.apache.commons.lang.time.DateUtils;import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;/*** 日期操作工具类*/
public class DateUtil {private static final String dateFormat = "yyyy-MM-dd";/*** 获取两个时间差 单位:秒* @param date1* @param date2* @return*/public Long getTimeSubtract(Date date1, Date date2) {return (date1.getTime() - date2.getTime()) / 1000;}/*** 格式化日期** @param date* @return*/public static String formatDate(Date date) {SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);return sdf.format(date);}/*** 截取比较断两个日期对象的field处的值 。* 如果第一个日期小于、等于、大于第二个,则对应返回负整数、0、正整数** @param date1 第一个日期对象,非null* @param date2 第二个日期对象,非null* @param field Calendar中的阈值* <p>* date1 > date2 返回:1* date1 = date2 返回:0* date1 < date2 返回:-1*/public static int truncatedCompareTo(final Date date1, final Date date2, final int field) {return DateUtils.truncatedCompareTo(date1, date2, field);}/*** 比对时间大小** @param beginDate* @param endDate* @return*/public static boolean dateCompare(Date beginDate, Date endDate) {// endDate > beginDateif (DateUtil.truncatedCompareTo(beginDate, endDate, Calendar.SECOND) == 1) {return false;}// beginDate <= endDatereturn true;}
}
HttpClientUtil:HttpClient类 http客户端类 微信:
package com.atguigu.gmall.common.util;import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;/*** HttpClient类 微信:**/
public class HttpClientUtil {public static String doGet(String url) {// 创建Httpclient对象CloseableHttpClient httpclient = HttpClients.createDefault();// 创建http GET请求HttpGet httpGet = new HttpGet(url);CloseableHttpResponse response = null;try {// 执行请求response = httpclient.execute(httpGet);// 判断返回状态是否为200if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {HttpEntity entity = response.getEntity();String result = EntityUtils.toString(entity, "UTF-8");EntityUtils.consume(entity);httpclient.close();return result;}httpclient.close();}catch (IOException e){e.printStackTrace();return null;}return null;}public static void download(String url,String fileName) {// 创建Httpclient对象CloseableHttpClient httpclient = HttpClients.createDefault();// 创建http GET请求HttpGet httpGet = new HttpGet(url);CloseableHttpResponse response = null;try {// 执行请求response = httpclient.execute(httpGet);// 判断返回状态是否为200if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {HttpEntity entity = response.getEntity();// String result = EntityUtils.toString(entity, "UTF-8");byte[] bytes = EntityUtils.toByteArray(entity);File file =new File(fileName);// InputStream in = entity.getContent();FileOutputStream fout = new FileOutputStream(file);fout.write(bytes);EntityUtils.consume(entity);httpclient.close();fout.flush();fout.close();return ;}httpclient.close();}catch (IOException e){e.printStackTrace();return ;}return ;}
}
IpUtil :获取ip地址
package com.atguigu.gmall.common.util;import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;/*** 获取ip地址*/
public class IpUtil {public static String getIpAddress(HttpServletRequest request) {String ipAddress = null;try {ipAddress = request.getHeader("x-forwarded-for");if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {ipAddress = request.getHeader("Proxy-Client-IP");}if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {ipAddress = request.getHeader("WL-Proxy-Client-IP");}if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {ipAddress = request.getRemoteAddr();if (ipAddress.equals("127.0.0.1")) {// 根据网卡取本机配置的IPInetAddress inet = null;try {inet = InetAddress.getLocalHost();} catch (UnknownHostException e) {e.printStackTrace();}ipAddress = inet.getHostAddress();}}// 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()// = 15if (ipAddress.indexOf(",") > 0) {ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));}}} catch (Exception e) {ipAddress="";}// ipAddress = this.getRequest().getRemoteAddr();return ipAddress;}// 网关中获取Ip地址public static String getGatwayIpAddress(ServerHttpRequest request) {HttpHeaders headers = request.getHeaders();String ip = headers.getFirst("x-forwarded-for");if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {// 多次反向代理后会有多个ip值,第一个ip才是真实ipif (ip.indexOf(",") != -1) {ip = ip.split(",")[0];}}if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {ip = headers.getFirst("Proxy-Client-IP");}if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {ip = headers.getFirst("WL-Proxy-Client-IP");}if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {ip = headers.getFirst("HTTP_CLIENT_IP");}if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {ip = headers.getFirst("HTTP_X_FORWARDED_FOR");}if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {ip = headers.getFirst("X-Real-IP");}if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {ip = request.getRemoteAddress().getAddress().getHostAddress();}return ip;}
}
MD5 :通过MD5给字符串加密的工具类
package com.atguigu.gmall.common.util;import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;public final class MD5 {public static String encrypt(String strSrc) {try {char hexChars[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8','9', 'a', 'b', 'c', 'd', 'e', 'f' };byte[] bytes = strSrc.getBytes();MessageDigest md = MessageDigest.getInstance("MD5");md.update(bytes);bytes = md.digest();int j = bytes.length;char[] chars = new char[j * 2];int k = 0;for (int i = 0; i < bytes.length; i++) {byte b = bytes[i];chars[k++] = hexChars[b >>> 4 & 0xf];chars[k++] = hexChars[b & 0xf];}return new String(chars);} catch (NoSuchAlgorithmException e) {e.printStackTrace();throw new RuntimeException("MD5加密出错!!+" + e);}}}
2.2搭建service-util模块
点击common,选择New–>Module,搭建过程同common-util
修改pom:
引入依赖:
<dependencies><dependency><groupId>com.atguigu.gmall</groupId><artifactId>common-util</artifactId><version>1.0-SNAPSHOT</version></dependency><!-- redis --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><!-- spring2.X集成redis所需common-pool2--><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId></dependency><!-- redisson 分布式锁--><dependency><groupId>org.redisson</groupId><artifactId>redisson</artifactId></dependency><!--mybatis-plus--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><scope>provided</scope></dependency></dependencies>
添加service-util公共类
GmallCache:注解
package com.atguigu.gmall.common.cache;import java.lang.annotation.*;/*** @author atguigu-mqx*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface GmallCache {// 定义一个数据 sku:skuId// 目的用这个前缀要想组成 缓存的key!String prefix() default "cache:";}
GmallCacheAspect:
package com.atguigu.gmall.common.cache;import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.common.constant.RedisConst;
import lombok.SneakyThrows;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;import java.util.Arrays;
import java.util.concurrent.TimeUnit;/*** @author atguigu-mqx*/
@Component
@Aspect
public class GmallCacheAspect {@Autowiredprivate RedissonClient redissonClient;@Autowiredprivate RedisTemplate redisTemplate;// 定义一个环绕通知!@SneakyThrows@Around("@annotation(com.atguigu.gmall.common.cache.GmallCache)")public Object gmallCacheAspectMethod(ProceedingJoinPoint point){// 定义一个对象Object obj = new Object();/*业务逻辑!1. 必须先知道这个注解在哪些方法 || 必须要获取到方法上的注解2. 获取到注解上的前缀3. 必须要组成一个缓存的key!4. 可以通过这个key 获取缓存的数据true:直接返回!false:分布式锁业务逻辑!*/MethodSignature methodSignature = (MethodSignature) point.getSignature();GmallCache gmallCache = methodSignature.getMethod().getAnnotation(GmallCache.class);// 获取到注解上的前缀String prefix = gmallCache.prefix();// 组成缓存的key! 获取方法传递的参数String key = prefix+ Arrays.asList(point.getArgs()).toString();try {// 可以通过这个key 获取缓存的数据obj = this.getRedisData(key,methodSignature);if (obj==null){// 分布式业务逻辑// 设置分布式锁,进入数据库进行查询数据!RLock lock = redissonClient.getLock(key + ":lock");// 调用trylock方法boolean result = lock.tryLock(RedisConst.SKULOCK_EXPIRE_PX1, RedisConst.SKULOCK_EXPIRE_PX2, TimeUnit.SECONDS);// 判断if(result){try {// 执行业务逻辑:直接从数据库获取数据// 这个注解 @GmallCache 有可能在 BaseCategoryView getCategoryName , List<SpuSaleAttr> getSpuSaleAttrListById ....obj = point.proceed(point.getArgs());// 防止缓存穿透if (obj==null){Object object = new Object();// 将缓存的数据变为 Json 的 字符串this.redisTemplate.opsForValue().set(key, JSON.toJSONString(object),RedisConst.SKUKEY_TEMPORARY_TIMEOUT,TimeUnit.SECONDS);return object;}// 将缓存的数据变为 Json 的 字符串this.redisTemplate.opsForValue().set(key, JSON.toJSONString(obj),RedisConst.SKUKEY_TIMEOUT,TimeUnit.SECONDS);return obj;}finally {// 解锁lock.unlock();}}else {// 没有获取到try {Thread.sleep(100);return gmallCacheAspectMethod(point);} catch (InterruptedException e) {e.printStackTrace();}}}else {// 直接从缓存获取的数据!return obj;}} catch (Throwable throwable) {throwable.printStackTrace();}// 数据库兜底!return point.proceed(point.getArgs());}/*** 从缓存中获取数据!* @param key* @return*/private Object getRedisData(String key,MethodSignature methodSignature) {// 在向缓存存储数据的时候,将数据变为Json 字符串了!// 通过这个key 获取到缓存的valueString strJson = (String) this.redisTemplate.opsForValue().get(key);// 判断if(!StringUtils.isEmpty(strJson)){// 将字符串转换为对应的数据类型!return JSON.parseObject(strJson,methodSignature.getReturnType());}return null;}}
MybatisPlusConfig :MybatisPlus配置类
package com.atguigu.gmall.common.config;import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;/*** MybatisPlus配置类**/
@EnableTransactionManagement
@Configuration
@MapperScan("com.atguigu.gmall.*.mapper")
public class MybatisPlusConfig {/*** 分页插件*/@Beanpublic PaginationInterceptor paginationInterceptor() {PaginationInterceptor paginationInterceptor = new PaginationInterceptor();// paginationInterceptor.setLimit(你的最大单页限制数量,默认 500 条,小于 0 如 -1 不受限制);return paginationInterceptor;}}
RedisConfig :Redis配置类,序列化
package com.atguigu.gmall.common.config;import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
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.lang.reflect.Method;
import java.time.Duration;/*** Redis配置类*/
@Configuration
@EnableCaching
public class RedisConfig {// 声明模板/*ref = 表示引用value = 具体的值<bean class="org.springframework.data.redis.core.RedisTemplate" ><property name="defaultSerializer" ref = ""></bean>*/@Beanpublic RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();// 设置redis的连接池工厂。redisTemplate.setConnectionFactory(redisConnectionFactory);// 设置序列化的。Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);ObjectMapper objectMapper = new ObjectMapper();objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(objectMapper);// 将Redis 中 string ,hash 数据类型,自动序列化!redisTemplate.setKeySerializer(new StringRedisSerializer());redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);// 设置数据类型是Hash 的 序列化!redisTemplate.setHashKeySerializer(new StringRedisSerializer());redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);redisTemplate.afterPropertiesSet();return redisTemplate;}}
RedissonConfig:redisson配置信息
package com.atguigu.gmall.common.config;import lombok.Data;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;/*** redisson配置信息*/
@Data
@Configuration
@ConfigurationProperties("spring.redis")
public class RedissonConfig {private String host;private String addresses;private String password;private String port;private int timeout = 3000;private int connectionPoolSize = 64;private int connectionMinimumIdleSize=10;private int pingConnectionInterval = 60000;private static String ADDRESS_PREFIX = "redis://";/*** 自动装配**/@BeanRedissonClient redissonSingle() {Config config = new Config();if(StringUtils.isEmpty(host)){throw new RuntimeException("host is empty");}SingleServerConfig serverConfig = config.useSingleServer()//redis://127.0.0.1:7181.setAddress(ADDRESS_PREFIX + this.host + ":" + port).setTimeout(this.timeout).setPingConnectionInterval(pingConnectionInterval).setConnectionPoolSize(this.connectionPoolSize).setConnectionMinimumIdleSize(this.connectionMinimumIdleSize);if(!StringUtils.isEmpty(this.password)) {serverConfig.setPassword(this.password);}// RedissonClient redisson = Redisson.create(config);return Redisson.create(config);}
}
Swagger2Config :
package com.atguigu.gmall.common.config;import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;import java.util.ArrayList;
import java.util.List;/*** Swagger2配置信息*/
@Configuration
@EnableSwagger2
public class Swagger2Config {@Beanpublic Docket webApiConfig(){//添加head参数startList<Parameter> pars = new ArrayList<>();ParameterBuilder tokenPar = new ParameterBuilder();tokenPar.name("userId").description("用户ID").defaultValue("1").modelRef(new ModelRef("string")).parameterType("header").required(false).build();pars.add(tokenPar.build());ParameterBuilder tmpPar = new ParameterBuilder();tmpPar.name("userTempId").description("临时用户ID").defaultValue("1").modelRef(new ModelRef("string")).parameterType("header").required(false).build();pars.add(tmpPar.build());//添加head参数endreturn new Docket(DocumentationType.SWAGGER_2).groupName("webApi").apiInfo(webApiInfo()).select()//过滤掉admin路径下的所有页面.paths(Predicates.and(PathSelectors.regex("/api/.*")))//过滤掉所有error或error.*页面//.paths(Predicates.not(PathSelectors.regex("/error.*"))).build().globalOperationParameters(pars);}@Beanpublic Docket adminApiConfig(){return new Docket(DocumentationType.SWAGGER_2).groupName("adminApi").apiInfo(adminApiInfo()).select()//只显示admin路径下的页面.paths(Predicates.and(PathSelectors.regex("/admin/.*"))).build();}private ApiInfo webApiInfo(){return new ApiInfoBuilder().title("网站-API文档").description("本文档描述了网站微服务接口定义").version("1.0").contact(new Contact("Helen", "http://atguigu.com", "55317332@qq.com")).build();}private ApiInfo adminApiInfo(){return new ApiInfoBuilder().title("后台管理系统-API文档").description("本文档描述了后台管理系统微服务接口定义").version("1.0").contact(new Contact("Helen", "http://atguigu.com", "55317332@qq.com")).build();}}
RedisConst:Redis常量配置类
package com.atguigu.gmall.common.constant;/*** Redis常量配置类* set name admin*/
public class RedisConst {public static final String SKUKEY_PREFIX = "sku:";public static final String SKUKEY_SUFFIX = ":info";//单位:秒public static final long SKUKEY_TIMEOUT = 24 * 60 * 60;// 定义变量,记录空对象的缓存过期时间public static final long SKUKEY_TEMPORARY_TIMEOUT = 10 * 60;//单位:秒 尝试获取锁的最大等待时间public static final long SKULOCK_EXPIRE_PX1 = 1;//单位:秒 锁的持有时间public static final long SKULOCK_EXPIRE_PX2 = 1;public static final String SKULOCK_SUFFIX = ":lock";public static final String USER_KEY_PREFIX = "user:";public static final String USER_CART_KEY_SUFFIX = ":cart";public static final long USER_CART_EXPIRE = 60 * 60 * 24 * 30;//用户登录public static final String USER_LOGIN_KEY_PREFIX = "user:login:";// public static final String userinfoKey_suffix = ":info";public static final int USERKEY_TIMEOUT = 60 * 60 * 24 * 7;//秒杀商品前缀public static final String SECKILL_GOODS = "seckill:goods";public static final String SECKILL_ORDERS = "seckill:orders";public static final String SECKILL_ORDERS_USERS = "seckill:orders:users";public static final String SECKILL_STOCK_PREFIX = "seckill:stock:";public static final String SECKILL_USER = "seckill:user:";//用户锁定时间 单位:秒public static final int SECKILL__TIMEOUT = 60 * 60 * 1;}
GlobalExceptionHandler :全局异常处理类
package com.atguigu.gmall.common.handler;import com.atguigu.gmall.common.execption.GmallException;
import com.atguigu.gmall.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;/*** 全局异常处理类**/
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {@ExceptionHandler(Exception.class)@ResponseBodypublic Result error(Exception e){e.printStackTrace();return Result.fail();}/*** 自定义异常处理方法* @param e* @return*/@ExceptionHandler(GmallException.class)@ResponseBodypublic Result error(GmallException e){return Result.fail(e.getMessage());}
}
(3)搭建model模块
点击gmall-parent 父工程 选择module
导入实体类:给的资料中,搭建过程同common父模块
不写就是jar,写了形象一点
引入依赖:
<dependencies><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><!--mybatis-plus--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><scope>provided </scope></dependency><!--swagger--><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><scope>provided </scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId><scope>provided </scope></dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-mongodb</artifactId><scope>provided </scope></dependency></dependencies>
引入实体类:
BaseEntity:是一个基础类
package com.atguigu.gmall.model.base;import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;import java.io.Serializable;
import java.util.Date;@Data
public class BaseEntity implements Serializable {@ApiModelProperty(value = "id")@TableId(type = IdType.AUTO)private Long id;@ApiModelProperty(value = "创建时间")@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")@TableField("create_time")private Date createTime;@ApiModelProperty(value = "更新时间")@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")@TableField("update_time")private Date updateTime;@ApiModelProperty(value = "逻辑删除(1:已删除,0:未删除)")@JsonIgnore@TableLogic@TableField("is_deleted")private Integer isDeleted;
}
所有的实体类都继承了这个类:
(4)搭建service父模块
service:service模块父节点
service-product:商品服务模块
...
搭建过程同common父模块
他是父工程,删除src