From 57a7ec6424a287d8d857d99b1488b3b2ce74ff6c Mon Sep 17 00:00:00 2001
From: 安瑾然 <107107765@qq.com>
Date: 星期一, 11 七月 2022 17:44:12 +0800
Subject: [PATCH] 统一异常拦截,mybatis配置

---
 src/main/java/com/example/jz/dao/CauseDao.java                         |   15 ++
 src/main/java/com/example/jz/enums/BusinessHttpStatus.java             |   58 +++++++++
 src/main/java/com/example/jz/modle/entity/Cause.java                   |   33 ++---
 src/main/java/com/example/jz/exception/BusinessException.java          |   46 +++++++
 src/main/java/com/example/jz/modle/entity/Announcement.java            |    5 
 src/main/java/com/example/jz/controller/CauseController.java           |   38 +++---
 src/main/java/com/example/jz/service/impl/CauseServiceImpl.java        |   19 +++
 pom.xml                                                                |    9 +
 src/main/java/com/example/jz/config/DefaultExceptionHandlerConfig.java |   39 ++++++
 /dev/null                                                              |   19 ---
 src/main/resources/application.properties                              |    2 
 src/main/java/com/example/jz/config/MybatisPlusConfig.java             |   24 ++++
 src/main/java/com/example/jz/JzApplication.java                        |    2 
 src/main/java/com/example/jz/controller/GroupUserController.java       |    2 
 src/main/java/com/example/jz/service/CauseService.java                 |   15 ++
 src/main/java/com/example/jz/controller/AnnouncementController.java    |    9 
 16 files changed, 264 insertions(+), 71 deletions(-)

diff --git a/pom.xml b/pom.xml
index 23a3361..5bb23bd 100644
--- a/pom.xml
+++ b/pom.xml
@@ -48,12 +48,17 @@
         <dependency>
             <groupId>io.springfox</groupId>
             <artifactId>springfox-swagger2</artifactId>
-            <version>2.9.2</version>
+            <version>3.0.0</version>
         </dependency>
         <dependency>
             <groupId>com.github.xiaoymin</groupId>
             <artifactId>swagger-bootstrap-ui</artifactId>
-            <version>1.9.3</version>
+            <version>1.9.6</version>
+        </dependency>
+        <dependency>
+            <groupId>cn.hutool</groupId>
+            <artifactId>hutool-all</artifactId>
+            <version>5.8.3</version>
         </dependency>
     </dependencies>
 
diff --git a/src/main/java/com/example/jz/JzApplication.java b/src/main/java/com/example/jz/JzApplication.java
index 0cb7ae9..838dcc3 100644
--- a/src/main/java/com/example/jz/JzApplication.java
+++ b/src/main/java/com/example/jz/JzApplication.java
@@ -1,9 +1,11 @@
 package com.example.jz;
 
+import org.mybatis.spring.annotation.MapperScan;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 
 @SpringBootApplication
+@MapperScan("com.example.jz.dao")
 public class JzApplication {
 
     public static void main(String[] args) {
diff --git a/src/main/java/com/example/jz/config/DefaultExceptionHandlerConfig.java b/src/main/java/com/example/jz/config/DefaultExceptionHandlerConfig.java
new file mode 100644
index 0000000..c153ac9
--- /dev/null
+++ b/src/main/java/com/example/jz/config/DefaultExceptionHandlerConfig.java
@@ -0,0 +1,39 @@
+package com.example.jz.config;
+
+import com.example.jz.enums.BusinessHttpStatus;
+import com.example.jz.exception.BusinessException;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.stereotype.Controller;
+import org.springframework.validation.BindException;
+import org.springframework.web.bind.MethodArgumentNotValidException;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.RestControllerAdvice;
+
+/**
+ * 鑷畾涔夐敊璇鐞嗗櫒
+ */
+@Controller
+@RestControllerAdvice
+public class DefaultExceptionHandlerConfig {
+
+
+    @ExceptionHandler(BindException.class)
+    public ResponseEntity<String> bindExceptionHandler(BindException e) {
+        e.printStackTrace();
+        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getBindingResult().getFieldErrors().get(0).getDefaultMessage());
+
+    }
+
+    @ExceptionHandler(MethodArgumentNotValidException.class)
+    public ResponseEntity<String> methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
+        e.printStackTrace();
+        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getBindingResult().getFieldErrors().get(0).getDefaultMessage());
+    }
+
+    @ExceptionHandler(BusinessException.class)
+    public ResponseEntity<String> unauthorizedExceptionHandler(BusinessException e) {
+        e.printStackTrace();
+        return ResponseEntity.status(e.getHttpStatusCode()).body(e.getMessage());
+    }
+}
diff --git a/src/main/java/com/example/jz/config/MybatisPlusConfig.java b/src/main/java/com/example/jz/config/MybatisPlusConfig.java
new file mode 100644
index 0000000..24e21b7
--- /dev/null
+++ b/src/main/java/com/example/jz/config/MybatisPlusConfig.java
@@ -0,0 +1,24 @@
+package com.example.jz.config;
+
+import com.baomidou.mybatisplus.core.injector.ISqlInjector;
+import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
+import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
+import org.mybatis.spring.annotation.MapperScan;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+@MapperScan({"com.example.jz.dao"})
+public class MybatisPlusConfig {
+    /**
+     * 鍒嗛〉鎻掍欢
+     * @return PaginationInterceptor
+     */
+    @Bean
+    @ConditionalOnMissingBean
+    public PaginationInterceptor paginationInterceptor() {
+        return new PaginationInterceptor();
+    }
+
+}
diff --git a/src/main/java/com/example/jz/controller/AnnouncementController.java b/src/main/java/com/example/jz/controller/AnnouncementController.java
index 833c767..8de6c43 100644
--- a/src/main/java/com/example/jz/controller/AnnouncementController.java
+++ b/src/main/java/com/example/jz/controller/AnnouncementController.java
@@ -1,13 +1,12 @@
 package com.example.jz.controller;
 
-
-
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.api.ApiController;
 import com.baomidou.mybatisplus.extension.api.R;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.example.jz.modle.entity.Announcement;
 import com.example.jz.service.AnnouncementService;
+import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;
 
 import javax.annotation.Resource;
@@ -32,13 +31,13 @@
     /**
      * 鍒嗛〉鏌ヨ鎵�鏈夋暟鎹�
      *
-     * @param page 鍒嗛〉瀵硅薄
+     * @param page         鍒嗛〉瀵硅薄
      * @param announcement 鏌ヨ瀹炰綋
      * @return 鎵�鏈夋暟鎹�
      */
     @GetMapping
-    public R selectAll(Page<Announcement> page, Announcement announcement) {
-        return success(this.announcementService.page(page, new QueryWrapper<>(announcement)));
+    public ResponseEntity<Page<Announcement>> selectAll(Page<Announcement> page, Announcement announcement) {
+        return ResponseEntity.ok(this.announcementService.page(page, new QueryWrapper<>(announcement)));
     }
 
     /**
diff --git a/src/main/java/com/example/jz/controller/CaseController.java b/src/main/java/com/example/jz/controller/CauseController.java
similarity index 60%
rename from src/main/java/com/example/jz/controller/CaseController.java
rename to src/main/java/com/example/jz/controller/CauseController.java
index a4eaaac..ec40390 100644
--- a/src/main/java/com/example/jz/controller/CaseController.java
+++ b/src/main/java/com/example/jz/controller/CauseController.java
@@ -1,13 +1,11 @@
 package com.example.jz.controller;
 
-
-
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.api.ApiController;
 import com.baomidou.mybatisplus.extension.api.R;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
-import com.example.jz.modle.entity.Case;
-import com.example.jz.service.CaseService;
+import com.example.jz.modle.entity.Cause;
+import com.example.jz.service.CauseService;
 import org.springframework.web.bind.annotation.*;
 
 import javax.annotation.Resource;
@@ -15,30 +13,30 @@
 import java.util.List;
 
 /**
- * (Case)琛ㄦ帶鍒跺眰
+ * (Cause)琛ㄦ帶鍒跺眰
  *
  * @author makejava
- * @since 2022-07-11 16:35:56
+ * @since 2022-07-11 16:55:40
  */
 @RestController
-@RequestMapping("case")
-public class CaseController extends ApiController {
+@RequestMapping("cause")
+public class CauseController extends ApiController {
     /**
      * 鏈嶅姟瀵硅薄
      */
     @Resource
-    private CaseService caseService;
+    private CauseService causeService;
 
     /**
      * 鍒嗛〉鏌ヨ鎵�鏈夋暟鎹�
      *
      * @param page 鍒嗛〉瀵硅薄
-     * @param case 鏌ヨ瀹炰綋
+     * @param cause 鏌ヨ瀹炰綋
      * @return 鎵�鏈夋暟鎹�
      */
     @GetMapping
-    public R selectAll(Page<Case> page, Case case) {
-        return success(this.caseService.page(page, new QueryWrapper<>(case)));
+    public R selectAll(Page<Cause> page, Cause cause) {
+        return success(this.causeService.page(page, new QueryWrapper<>(cause)));
     }
 
     /**
@@ -49,29 +47,29 @@
      */
     @GetMapping("{id}")
     public R selectOne(@PathVariable Serializable id) {
-        return success(this.caseService.getById(id));
+        return success(this.causeService.getById(id));
     }
 
     /**
      * 鏂板鏁版嵁
      *
-     * @param case 瀹炰綋瀵硅薄
+     * @param cause 瀹炰綋瀵硅薄
      * @return 鏂板缁撴灉
      */
     @PostMapping
-    public R insert(@RequestBody Case case) {
-        return success(this.caseService.save(case));
+    public R insert(@RequestBody Cause cause) {
+        return success(this.causeService.save(cause));
     }
 
     /**
      * 淇敼鏁版嵁
      *
-     * @param case 瀹炰綋瀵硅薄
+     * @param cause 瀹炰綋瀵硅薄
      * @return 淇敼缁撴灉
      */
     @PutMapping
-    public R update(@RequestBody Case case) {
-        return success(this.caseService.updateById(case));
+    public R update(@RequestBody Cause cause) {
+        return success(this.causeService.updateById(cause));
     }
 
     /**
@@ -82,7 +80,7 @@
      */
     @DeleteMapping
     public R delete(@RequestParam("idList") List<Long> idList) {
-        return success(this.caseService.removeByIds(idList));
+        return success(this.causeService.removeByIds(idList));
     }
 }
 
diff --git a/src/main/java/com/example/jz/controller/GroupUserController.java b/src/main/java/com/example/jz/controller/GroupUserController.java
index 0a42557..53a7ea7 100644
--- a/src/main/java/com/example/jz/controller/GroupUserController.java
+++ b/src/main/java/com/example/jz/controller/GroupUserController.java
@@ -1,7 +1,5 @@
 package com.example.jz.controller;
 
-
-
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.api.ApiController;
 import com.baomidou.mybatisplus.extension.api.R;
diff --git a/src/main/java/com/example/jz/dao/CaseDao.java b/src/main/java/com/example/jz/dao/CaseDao.java
deleted file mode 100644
index 329b3f1..0000000
--- a/src/main/java/com/example/jz/dao/CaseDao.java
+++ /dev/null
@@ -1,15 +0,0 @@
-package com.example.jz.dao;
-
-import com.baomidou.mybatisplus.core.mapper.BaseMapper;
-import com.example.jz.modle.entity.Case;
-
-/**
- * (Case)琛ㄦ暟鎹簱璁块棶灞�
- *
- * @author makejava
- * @since 2022-07-11 16:35:56
- */
-public interface CaseDao extends BaseMapper<Case> {
-
-}
-
diff --git a/src/main/java/com/example/jz/dao/CauseDao.java b/src/main/java/com/example/jz/dao/CauseDao.java
new file mode 100644
index 0000000..5161083
--- /dev/null
+++ b/src/main/java/com/example/jz/dao/CauseDao.java
@@ -0,0 +1,15 @@
+package com.example.jz.dao;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.example.jz.modle.entity.Cause;
+
+/**
+ * (Cause)琛ㄦ暟鎹簱璁块棶灞�
+ *
+ * @author makejava
+ * @since 2022-07-11 16:55:40
+ */
+public interface CauseDao extends BaseMapper<Cause> {
+
+}
+
diff --git a/src/main/java/com/example/jz/enums/BusinessHttpStatus.java b/src/main/java/com/example/jz/enums/BusinessHttpStatus.java
new file mode 100644
index 0000000..acb2208
--- /dev/null
+++ b/src/main/java/com/example/jz/enums/BusinessHttpStatus.java
@@ -0,0 +1,58 @@
+package com.example.jz.enums;
+
+public enum BusinessHttpStatus {
+
+    // 鏈櫥褰�
+    UNAUTHORIZED(401, "鏈巿鏉�");
+
+    private final int value;
+
+    private final String msg;
+
+
+    BusinessHttpStatus(int value, String msg) {
+        this.value = value;
+        this.msg = msg;
+    }
+
+
+    /**
+     * Return the integer value of this status code.
+     */
+    public int value() {
+        return this.value;
+    }
+
+    /**
+     * Return the msg of this status code.
+     */
+    public String getMsg() {
+        return msg;
+    }
+
+    /**
+     * Return a string representation of this status code.
+     */
+    @Override
+    public String toString() {
+        return this.value + " " + name();
+    }
+
+
+    public static BusinessHttpStatus valueOf(int statusCode) {
+        BusinessHttpStatus status = resolve(statusCode);
+        if (status == null) {
+            throw new IllegalArgumentException("娌℃湁鎵惧埌璇ttp鐘舵�佺爜鍖呭惈鐘舵�� [" + statusCode + "]");
+        }
+        return status;
+    }
+
+    public static BusinessHttpStatus resolve(int statusCode) {
+        for (BusinessHttpStatus status : values()) {
+            if (status.value == statusCode) {
+                return status;
+            }
+        }
+        return null;
+    }
+}
\ No newline at end of file
diff --git a/src/main/java/com/example/jz/exception/BusinessException.java b/src/main/java/com/example/jz/exception/BusinessException.java
new file mode 100644
index 0000000..f4780b1
--- /dev/null
+++ b/src/main/java/com/example/jz/exception/BusinessException.java
@@ -0,0 +1,46 @@
+package com.example.jz.exception;
+
+import com.example.jz.enums.BusinessHttpStatus;
+import org.springframework.http.HttpStatus;
+
+public class BusinessException extends RuntimeException{
+
+    /**
+     *
+     */
+    private static final long serialVersionUID = -4137688758944857209L;
+
+    /**
+     * http鐘舵�佺爜
+     */
+    private Integer httpStatusCode;
+
+
+    /**
+     * @param httpStatus http鐘舵�佺爜
+     */
+    public BusinessException(BusinessHttpStatus httpStatus) {
+        super(httpStatus.getMsg());
+        this.httpStatusCode = httpStatus.value();
+    }
+
+    /**
+     * @param httpStatus http鐘舵�佺爜
+     */
+    public BusinessException(BusinessHttpStatus httpStatus, String msg) {
+        super(msg);
+        this.httpStatusCode = httpStatus.value();
+    }
+
+
+    public BusinessException(String msg) {
+        super(msg);
+        this.httpStatusCode = HttpStatus.BAD_REQUEST.value();
+    }
+
+
+    public Integer getHttpStatusCode() {
+        return httpStatusCode;
+    }
+
+}
\ No newline at end of file
diff --git a/src/main/java/com/example/jz/modle/entity/Announcement.java b/src/main/java/com/example/jz/modle/entity/Announcement.java
index 1c98c27..349b27a 100644
--- a/src/main/java/com/example/jz/modle/entity/Announcement.java
+++ b/src/main/java/com/example/jz/modle/entity/Announcement.java
@@ -1,7 +1,9 @@
 package com.example.jz.modle.entity;
 
 import java.util.Date;
+
 import com.baomidou.mybatisplus.extension.activerecord.Model;
+
 import java.io.Serializable;
 
 /**
@@ -20,7 +22,6 @@
     private Integer groupId;
     //鍒涘缓鏃堕棿
     private Date createTime;
-
 
     public Integer getAnnouncementId() {
         return announcementId;
@@ -63,5 +64,5 @@
     protected Serializable pkVal() {
         return this.announcementId;
     }
-    }
+}
 
diff --git a/src/main/java/com/example/jz/modle/entity/Case.java b/src/main/java/com/example/jz/modle/entity/Cause.java
similarity index 85%
rename from src/main/java/com/example/jz/modle/entity/Case.java
rename to src/main/java/com/example/jz/modle/entity/Cause.java
index 0103815..94a7c0c 100644
--- a/src/main/java/com/example/jz/modle/entity/Case.java
+++ b/src/main/java/com/example/jz/modle/entity/Cause.java
@@ -5,13 +5,13 @@
 import java.io.Serializable;
 
 /**
- * (Case)琛ㄥ疄浣撶被
+ * (Cause)琛ㄥ疄浣撶被
  *
  * @author makejava
- * @since 2022-07-11 16:35:56
+ * @since 2022-07-11 16:55:40
  */
 @SuppressWarnings("serial")
-public class Case extends Model<Case> {
+public class Cause extends Model<Cause> {
     //妗堜欢ID
     private Integer id;
     //澶村儚鍦板潃
@@ -28,11 +28,10 @@
     private String amountInvolved;
     //鎶ユ鏉愭枡鍥剧墖鍦板潃 澶氫釜鐢�,鍒嗛殧
     private String reportMaterials;
-    //鎶ユ鏂瑰紡
-    private String reportMethod
-reportMethod;
     //鎶ユ鏃堕棿
     private Date reportTime;
+    //鎶ユ鏂瑰紡
+    private String reportMethod;
 
 
     public Integer getId() {
@@ -99,20 +98,6 @@
         this.reportMaterials = reportMaterials;
     }
 
-    public String getReportMethod
-reportMethod() {
-        return reportMethod
-reportMethod;
-    }
-
-    public void setReportMethod
-reportMethod(String reportMethod
-reportMethod) {
-        this.reportMethod
-reportMethod = reportMethod
-reportMethod;
-    }
-
     public Date getReportTime() {
         return reportTime;
     }
@@ -121,6 +106,14 @@
         this.reportTime = reportTime;
     }
 
+    public String getReportMethod() {
+        return reportMethod;
+    }
+
+    public void setReportMethod(String reportMethod) {
+        this.reportMethod = reportMethod;
+    }
+
     /**
      * 鑾峰彇涓婚敭鍊�
      *
diff --git a/src/main/java/com/example/jz/service/CaseService.java b/src/main/java/com/example/jz/service/CaseService.java
deleted file mode 100644
index 909bc28..0000000
--- a/src/main/java/com/example/jz/service/CaseService.java
+++ /dev/null
@@ -1,15 +0,0 @@
-package com.example.jz.service;
-
-import com.baomidou.mybatisplus.extension.service.IService;
-import com.example.jz.modle.entity.Case;
-
-/**
- * (Case)琛ㄦ湇鍔℃帴鍙�
- *
- * @author makejava
- * @since 2022-07-11 16:35:56
- */
-public interface CaseService extends IService<Case> {
-
-}
-
diff --git a/src/main/java/com/example/jz/service/CauseService.java b/src/main/java/com/example/jz/service/CauseService.java
new file mode 100644
index 0000000..af8cbbb
--- /dev/null
+++ b/src/main/java/com/example/jz/service/CauseService.java
@@ -0,0 +1,15 @@
+package com.example.jz.service;
+
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.example.jz.modle.entity.Cause;
+
+/**
+ * (Cause)琛ㄦ湇鍔℃帴鍙�
+ *
+ * @author makejava
+ * @since 2022-07-11 16:55:41
+ */
+public interface CauseService extends IService<Cause> {
+
+}
+
diff --git a/src/main/java/com/example/jz/service/impl/CaseServiceImpl.java b/src/main/java/com/example/jz/service/impl/CaseServiceImpl.java
deleted file mode 100644
index 76dd229..0000000
--- a/src/main/java/com/example/jz/service/impl/CaseServiceImpl.java
+++ /dev/null
@@ -1,19 +0,0 @@
-package com.example.jz.service.impl;
-
-import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import com.example.jz.dao.CaseDao;
-import com.example.jz.modle.entity.Case;
-import com.example.jz.service.CaseService;
-import org.springframework.stereotype.Service;
-
-/**
- * (Case)琛ㄦ湇鍔″疄鐜扮被
- *
- * @author makejava
- * @since 2022-07-11 16:35:56
- */
-@Service("caseService")
-public class CaseServiceImpl extends ServiceImpl<CaseDao, Case> implements CaseService {
-
-}
-
diff --git a/src/main/java/com/example/jz/service/impl/CauseServiceImpl.java b/src/main/java/com/example/jz/service/impl/CauseServiceImpl.java
new file mode 100644
index 0000000..a69c7a7
--- /dev/null
+++ b/src/main/java/com/example/jz/service/impl/CauseServiceImpl.java
@@ -0,0 +1,19 @@
+package com.example.jz.service.impl;
+
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.example.jz.dao.CauseDao;
+import com.example.jz.modle.entity.Cause;
+import com.example.jz.service.CauseService;
+import org.springframework.stereotype.Service;
+
+/**
+ * (Cause)琛ㄦ湇鍔″疄鐜扮被
+ *
+ * @author makejava
+ * @since 2022-07-11 16:55:41
+ */
+@Service("causeService")
+public class CauseServiceImpl extends ServiceImpl<CauseDao, Cause> implements CauseService {
+
+}
+
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
index 1c87d9b..45ec867 100644
--- a/src/main/resources/application.properties
+++ b/src/main/resources/application.properties
@@ -3,7 +3,7 @@
 
 ###  datasource
 #test
-spring.datasource.url=jdbc:mysql://42.193.1.25/:3306/job?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
+spring.datasource.url=jdbc:mysql://42.193.1.25:3306/qyjz?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
 spring.datasource.username=root
 spring.datasource.password=321$YcYl@1970!
 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

--
Gitblit v1.8.0