SpringBoot+JPA實現DDD(六)
阿新 • • 發佈:2020-08-18
潤色一下
記錄sql語句及sql的執行時間
<properties>
<p6spy.version>3.9.0</p6spy.version>
</properties>
<dependency>
<groupId>p6spy</groupId>
<artifactId>p6spy</artifactId>
<version>${p6spy.version}</version>
</dependency>
src/main/resources下新建spy.properties配置檔案:
driverlist=com.mysql.cj.jdbc.Driver logfile=spy.log dateformat=yyyy-MM-dd HH:mm:ss.SS logMessageFormat=com.p6spy.engine.spy.appender.CustomLineFormat customLogMessageFormat=- %(currentTime) | took %(executionTime)ms | connection %(connectionId) \nEXPLAIN %(sql);\n filter=true exclude=select 1 from dual
application.properties修改成:
#spring.datasource.url=jdbc:mysql://localhost:3306/product_center?\ useSSL=false&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8 spring.datasource.url=jdbc:p6spy:mysql://localhost:3306/product_center?\ useSSL=false&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8 spring.datasource.username=<your username> spring.datasource.password=<your password> #spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.driver-class-name=com.p6spy.engine.spy.P6SpyDriver
增加全域性異常處理
@ControllerAdvice
@ResponseBody
public class GlobalDefaultExceptionHandler {
private static final String UNKNOWN_ERROR_CODE = "unknown-error";
private static final String SYSTEM_ERROR_INFO = "系統異常,請聯絡管理員";
private static final String ILLEGAL_PARAM_CODE = "illegal-param";
@ExceptionHandler(value = IllegalArgumentException.class)
public ApiResult illegalParamExceptionHandler(IllegalArgumentException e) throws Exception {
// log todo
return ApiResult.error(ILLEGAL_PARAM_CODE, e.getMessage());
}
@ExceptionHandler(value = BusinessException.class)
public ApiResult businessExceptionHandler(BusinessException e) throws Exception {
// log todo
return ApiResult.error(e.getCode(), e.getMessage());
}
@ExceptionHandler(value = Exception.class)
public ApiResult defaultErrorHandler(Exception e) throws Exception {
// log todo
e.printStackTrace();
return ApiResult.error(UNKNOWN_ERROR_CODE, SYSTEM_ERROR_INFO);
}
}
資料庫新增自定義的審計欄位
domain.common.model:
@MappedSuperclass
@Data
public abstract class AuditEntity implements Serializable {
@Column(name = "is_delete", columnDefinition = "TINYINT(1) DEFAULT 0")
protected Boolean isDelete;
@Column(name = "created_by", length = 11, nullable = false)
protected Integer createdBy;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created_at", columnDefinition = "DATETIME NULL DEFAULT CURRENT_TIMESTAMP")
protected Date createdAt;
@Column(name = "updated_by", length = 11, nullable = false)
protected Integer updatedBy;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "updated_at", columnDefinition = "DATETIME NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")
protected Date updatedAt;
@PrePersist
protected void onCreate() {
updatedAt = createdAt = new Date();
isDelete = false;
}
@PreUpdate
protected void onUpdate() {
updatedAt = new Date();
}
}
public class Product extends AuditEntity implements Serializable {
...
}
demo下載地址: productcenter6.zip