開始之前
源碼已上傳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>
<!--定時(shí)任務(wù) 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>
目錄結(jié)構(gòu)
東西有點(diǎn)多蔚出,會依次說明
一些工具類塞淹、異常類和枚舉類
結(jié)果枚舉類
用于返回一些錯誤信息邑蒋,與異常類配合使用
@Getter
public enum ResultEnum {
SUCCESS(0, "成功"),
CODE_ERROR(1, "未獲取到code"),
SESSION_ERROR(2, "獲取sessionKey和openid失敗"),
SESSION_ID_NULL(3, "sessionId不存在"),
USER_INFO_ERROR(4, "用戶信息校驗(yàn)失敗"),
PARAM_ERROR(5, "表單參數(shù)不正確"),
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, "回復(fù)不存在")
;
private Integer code;
private String message;
ResultEnum(Integer code, String message) {
this.code = code;
this.message = message;
}
}
異常類
用于拋出項(xiàng)目自定義的一些錯誤信息
@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;
}
}
返回結(jié)果類
返回http請求的結(jié)果,code可讓前端知道返回是否正常疹味,操作是否成功
/**
* Http請求返回最外層對象
*/
@Data
public class ResultVO<T> {
/** 錯誤碼. */
private Integer code;
/** 提示信息. */
private String msg;
/** 具體內(nèi)容. */
private T data;
}
返回結(jié)果工具類
用這個工具類可以方便的返回統(tǒng)一的結(jié)果
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,因?yàn)槲⑿挪唤ㄗh使用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進(jìn)行數(shù)據(jù)庫增刪改查~
下一篇:springboot+jpa+redis+quzartz+elasticsearch實(shí)現(xiàn)微信論壇小程序(二)