开始之前
源码已上传github:桂香驿站
版本
- springboot版本:2.1.3.RELEASE
- redis版本:3.2.1
- elasticsearch版本:6.4.3
SDK
pom依赖
<!--springboot依赖-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!--JPA-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--elasticsearch-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<!--缓存-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
<!--定时任务 Quartz-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
<!--lombok工具-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--微信小程序SDK-->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-miniapp</artifactId>
<version>3.3.0</version>
</dependency>
<!--七牛云SDK-->
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId>
<version>[7.2.0, 7.2.99]</version>
</dependency>
<!--MD5加密-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<!--mysql连接-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--beanutils-->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>
<!--测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
lombok是方便的工具,可以帮你生成get、set方法
<!--lombok工具-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
目录结构
东西有点多,会依次说明
一些工具类、异常类和枚举类
结果枚举类
用于返回一些错误信息,与异常类配合使用
@Getter
public enum ResultEnum {
SUCCESS(0, "成功"),
CODE_ERROR(1, "未获取到code"),
SESSION_ERROR(2, "获取sessionKey和openid失败"),
SESSION_ID_NULL(3, "sessionId不存在"),
USER_INFO_ERROR(4, "用户信息校验失败"),
PARAM_ERROR(5, "表单参数不正确"),
UPLOAD_ERROR(6, "文件上传出错"),
ARTICLE_ID_ERROR(7, "帖子id不能为空"),
COMMENT_ID_ERROR(8, "评论id不能为空"),
USER_NOT_EXIT(9, "用户不存在"),
ARTICLE_NOT_EXIT(10, "帖子不存在"),
COMMENT_NOT_EXIT(11, "评论不存在"),
REPLY_NOT_EXIT(12, "回复不存在")
;
private Integer code;
private String message;
ResultEnum(Integer code, String message) {
this.code = code;
this.message = message;
}
}
异常类
用于抛出项目自定义的一些错误信息
@Data
public class BBSException extends RuntimeException {
private Integer code;
public BBSException(ResultEnum resultEnum) {
super(resultEnum.getMessage());
this.code = resultEnum.getCode();
}
public BBSException(Integer code, String message){
super(message);
this.code = code;
}
}
返回结果类
返回http请求的结果,code可让前端知道返回是否正常,操作是否成功
/**
* Http请求返回最外层对象
*/
@Data
public class ResultVO<T> {
/** 错误码. */
private Integer code;
/** 提示信息. */
private String msg;
/** 具体内容. */
private T data;
}
返回结果工具类
用这个工具类可以方便的返回统一的结果
public class ResultVOUtil {
public static ResultVO success(Object object){
ResultVO resultVO = new ResultVO();
resultVO.setData(object);
resultVO.setCode(0);
resultVO.setMsg("成功");
return resultVO;
}
public static ResultVO success(){
return success(null);
}
public static ResultVO error(Integer code, String msg){
ResultVO resultVO = new ResultVO();
resultVO.setCode(code);
resultVO.setMsg(msg);
return resultVO;
}
}
生成键的工具类
一个方法用于生成主键id,另一个用与加密前后端通信的sessionId,因为微信不建议使用sessionKey作为前后端通信的token
public class KeyUtil {
/**
* 生成唯一的主键UUID
* @return
*/
public static synchronized String genUniqueKey(){
return UUID.randomUUID().toString().replace("-", "").toLowerCase();
}
/**
* MD5加密的sessionId
* @param key
* @return
*/
public static synchronized String getSessionId(String key) {
String sessionId = DigestUtils.md5Hex(key);
System.out.println("MD5加密后的sessionId为:" + sessionId);
return sessionId;
}
}
下一章会介绍springboot-jpa进行数据库增删改查~
下一篇:springboot+jpa+redis+quzartz+elasticsearch实现微信论坛小程序(二)