上一篇https://www.cnblogs.com/taiguyiba/p/9828984.html 整合了數據庫表和Dao層代碼html
這一篇繼續設計數據封裝類:前端
涉及到獲取秒殺地址,查詢,返回秒殺結果等內容java
建立Exposer類,用於封裝秒殺地址:若是開啓秒殺,則返回狀態、秒殺地址md5加密值和ID,若是秒殺未開啓,則返回開始時間、結束時間等。ajax
Exposer.java數據庫
package com.seckill.dto; public class Exposer { //是否開啓秒殺
private boolean exposed; //md5加密
private String md5; //id
private long seckillId; //now time
private long now; //start time
private long start; //end time
private long end; public Exposer(boolean exposed, String md5,long seckillId) { super(); this.exposed = exposed; this.md5 = md5; this.seckillId = seckillId; } public Exposer(boolean exposed, long now, long start, long end,long seckillId) { super(); this.exposed = exposed; this.now = now; this.start = start; this.end = end; this.seckillId = seckillId; } public Exposer(boolean exposed, long seckillId) { super(); this.exposed = exposed; this.seckillId = seckillId; } public boolean isExposed() { return exposed; } public void setExposed(boolean exposed) { this.exposed = exposed; } public String getMd5() { return md5; } public void setMd5(String md5) { this.md5 = md5; } public long getSeckillId() { return seckillId; } public void setSeckillId(long seckillId) { this.seckillId = seckillId; } public long getNow() { return now; } public void setNow(long now) { this.now = now; } public long getStart() { return start; } public void setStart(long start) { this.start = start; } public long getEnd() { return end; } public void setEnd(long end) { this.end = end; } @Override public String toString() { return "Exposer [exposed=" + exposed + ", md5=" + md5 + ", seckillId=" + seckillId + ", now=" + now + ", start="
+ start + ", end=" + end + "]"; } }
SeckillExecution主要用於封裝秒殺結果json
package com.seckill.dto; import com.seckill.entity.SuccessKilled; import com.seckill.enums.SeckillStateEnum; /** * 數據傳遞 * 其中並無添加枚舉類型,轉換成json有問題 * @author lll * */
public class SeckillExecution { private long seckillId; private int state; private String stateInfo; private SuccessKilled successKilled; public SeckillExecution(long seckillId,SeckillStateEnum seckillStateEnum, SuccessKilled successKilled) { super(); this.seckillId = seckillId; this.state = seckillStateEnum.getState(); this.stateInfo = seckillStateEnum.getStateInfo(); this.successKilled = successKilled; } public SeckillExecution(long seckillId, SeckillStateEnum seckillStateEnum) { super(); this.seckillId = seckillId; this.state = seckillStateEnum.getState(); this.stateInfo = seckillStateEnum.getStateInfo(); } public SeckillExecution(SeckillStateEnum seckillStateEnum) { super(); this.state = seckillStateEnum.getState(); this.stateInfo = seckillStateEnum.getStateInfo(); } public long getSeckillId() { return seckillId; } public void setSeckillId(long seckillId) { this.seckillId = seckillId; } public int getState() { return state; } public void setState(int state) { this.state = state; } public String getStateInfo() { return stateInfo; } public void setStateInfo(String stateInfo) { this.stateInfo = stateInfo; } public SuccessKilled getSuccessKilled() { return successKilled; } public void setSuccessKilled(SuccessKilled successKilled) { this.successKilled = successKilled; } @Override public String toString() { return "SeckillExecution [seckillId=" + seckillId + ", state=" + state + ", stateInfo=" + stateInfo + ", successKilled=" + successKilled + "]"; } }
SeckillResult<T> 用於封裝前端請求的結果:ide
package com.seckill.dto; /** * 全部ajax請求封裝 * @author mayn * */
public class SeckillResult<T> { private boolean success; private T data; private String error; public SeckillResult(boolean success, T data) { super(); this.success = success; this.data = data; } public SeckillResult(boolean success, String error) { super(); this.success = success; this.error = error; } public boolean isSuccess() { return success; } public void setSuccess(boolean success) { this.success = success; } public T getData() { return data; } public void setData(T data) { this.data = data; } public String getError() { return error; } public void setError(String error) { this.error = error; } }
SeckillStateEnum用於封裝數據字典:this
package com.seckill.enums; /** * 使用枚舉標示常量數據字典 * @author mayn * */
public enum SeckillStateEnum { SUCCESSD(1,"秒殺成功"), END(0,"秒殺結束"), REPEAT_KILL(-1,"重複秒殺"), INNER_ERROR(-2,"內部錯誤"), DATA_REWRITE(-3,"數據篡改") ; private int state; private String stateInfo; private SeckillStateEnum(int state, String stateInfo) { this.state = state; this.stateInfo = stateInfo; } public int getState() { return state; } public String getStateInfo() { return stateInfo; } public static SeckillStateEnum stateOf(int index) { for(SeckillStateEnum state : values()) { if(state.getState() == index) { return state; } } return null; } }
RepeatKillException 重複秒殺異常類加密
package com.seckill.exception; public class RepeatKillException extends RuntimeException { public RepeatKillException(String message, Throwable cause) { super(message, cause); // TODO Auto-generated constructor stub
} public RepeatKillException(String message) { super(message); // TODO Auto-generated constructor stub
} }
SeckillCloseException 秒殺關閉異常類spa
package com.seckill.exception; public class SeckillCloseException extends RuntimeException { public SeckillCloseException(String message, Throwable cause) { super(message, cause); // TODO Auto-generated constructor stub
} public SeckillCloseException(String message) { super(message); // TODO Auto-generated constructor stub
} }
SeckillException 秒殺異常類
package com.seckill.exception; public class SeckillException extends RuntimeException { public SeckillException(String message, Throwable cause) { super(message, cause); // TODO Auto-generated constructor stub
} public SeckillException(String message) { super(message); // TODO Auto-generated constructor stub
} }