lrf 8 maanden geleden
commit
c8860c5e4e

+ 15 - 0
src/main/java/com/free/Main.java

@@ -0,0 +1,15 @@
+package com.free;
+
+import org.mybatis.spring.annotation.MapperScan;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.annotation.ComponentScan;
+
+@MapperScan({"com.free.mapper","com.gitee.sunchenbin.mybatis.actable.dao.*"})
+@ComponentScan(basePackages = {"com.free","com.gitee.sunchenbin.mybatis.actable.manager.*"})
+@SpringBootApplication
+public class Main {
+    public static void main(String[] args) {
+        SpringApplication.run(Main.class, args);
+    }
+}

+ 44 - 0
src/main/java/com/free/controller/TestController.java

@@ -0,0 +1,44 @@
+package com.free.controller;
+
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.free.entity.Test;
+import com.free.frame.CustomizationException;
+import com.free.frame.ExceptionEnum;
+import com.free.service.TestService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.List;
+
+@RestController
+@RequestMapping("/test")
+public class TestController {
+    @Autowired
+    private TestService testService;
+
+    @GetMapping()
+    public Object list() {
+        List list = testService.list();
+        return list;
+    }
+
+    @PostMapping()
+    public Object save(@RequestBody Test test) {
+        boolean result = testService.save(test);
+        return result;
+    }
+
+    @PostMapping("/{id}")
+    public Object update(@PathVariable long id, @RequestBody Test test) {
+        QueryWrapper<Test> qw = new QueryWrapper<Test>();
+        qw.eq("id", id);
+        Long num = testService.count(qw);
+        System.out.println(num);
+        if (num <= 0) throw new CustomizationException(ExceptionEnum.NOT_FOUND);
+        testService.updateById(test);
+        Test newData = testService.getById(id);
+        return newData;
+    }
+
+
+}

+ 43 - 0
src/main/java/com/free/entity/Test.java

@@ -0,0 +1,43 @@
+package com.free.entity;
+
+import com.baomidou.mybatisplus.annotation.*;
+import com.fasterxml.jackson.annotation.JsonFormat;
+import com.gitee.sunchenbin.mybatis.actable.annotation.Column;
+import com.gitee.sunchenbin.mybatis.actable.annotation.IsAutoIncrement;
+import com.gitee.sunchenbin.mybatis.actable.annotation.IsKey;
+import com.gitee.sunchenbin.mybatis.actable.annotation.Table;
+import lombok.Data;
+import org.springframework.format.annotation.DateTimeFormat;
+
+import java.time.LocalDateTime;
+
+@Data
+@Table(name = "test")
+@TableName(value = "test")
+public class Test {
+    @TableId(value = "id",type = IdType.AUTO)
+    @IsKey
+    @IsAutoIncrement
+    @Column(name = "id",comment = "主键")
+    private Long id;
+
+    @TableField(value = "name")
+    private String name;
+
+    @TableField
+    private Integer age;
+
+    @TableField()
+    private String status;
+
+    @TableField(fill = FieldFill.INSERT)
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "Asia/Shanghai")
+    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
+    private LocalDateTime  create_time;
+
+    @TableField(fill = FieldFill.UPDATE)
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "Asia/Shanghai")
+    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
+    private LocalDateTime  update_time;
+
+}

+ 30 - 0
src/main/java/com/free/frame/CustomizationException.java

@@ -0,0 +1,30 @@
+package com.free.frame;
+
+/**
+ * 自定义异常
+ */
+public class CustomizationException extends RuntimeException {
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * 错误码
+     */
+    protected String errcode;
+    /**
+     * 错误信息
+     */
+    protected String errmsg;
+
+    public String getErrCode() {
+        return this.errcode;
+    }
+    public String getErrMsg() {
+        return this.errmsg;
+    }
+
+    public CustomizationException(ExceptionEnum e) {
+        super(e.getResultCode());
+        this.errcode = e.getResultCode();
+        this.errmsg = e.getResultMsg();
+    }
+}

+ 36 - 0
src/main/java/com/free/frame/ExceptionEnum.java

@@ -0,0 +1,36 @@
+package com.free.frame;
+
+/**
+ * 自定义异常枚举
+ */
+public enum ExceptionEnum {
+    SERVICE_FAULT("500", "服务发生错误"),
+    // 数据操作异常定义
+    NOT_FOUND("4004","数据不存在或已删除!");
+
+
+    /**
+     * 错误码
+     */
+    private final String resultCode;
+
+    /**
+     * 错误描述
+     */
+    private final String resultMsg;
+
+    ExceptionEnum(String resultCode, String resultMsg) {
+        this.resultCode = resultCode;
+        this.resultMsg = resultMsg;
+    }
+
+
+    public String getResultCode() {
+        return resultCode;
+    }
+
+
+    public String getResultMsg() {
+        return resultMsg;
+    }
+    }

+ 49 - 0
src/main/java/com/free/frame/GlobalExceptionController.java

@@ -0,0 +1,49 @@
+package com.free.frame;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.web.bind.annotation.ControllerAdvice;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.ResponseBody;
+
+import javax.servlet.http.HttpServletRequest;
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+
+/**
+ * 自定义异常拦截
+ */
+@ControllerAdvice
+public class GlobalExceptionController {
+    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionController.class);
+    @ExceptionHandler(value = CustomizationException.class)
+    @ResponseBody
+    public ResponseFormat exception(HttpServletRequest req, CustomizationException ce) {
+        log.error("==========自定义异常捕获==========");
+        log.error(getExceptionInfo(ce));
+        return ResponseFormat.customizationError(ce);
+    }
+    @ExceptionHandler(value = Exception.class)
+    @ResponseBody
+    public ResponseFormat exceptionHandler(HttpServletRequest req, Exception e) {
+        log.error("==========全局异常捕获==========");
+        log.error(getExceptionInfo(e));
+        return ResponseFormat.error();
+    }
+
+
+    private static String getExceptionInfo(Exception ex) {
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        PrintStream printStream = new PrintStream(out);
+        ex.printStackTrace(printStream);
+        String rs = new String(out.toByteArray());
+        try {
+            printStream.close();
+            out.close();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return rs;
+    }
+
+}

+ 50 - 0
src/main/java/com/free/frame/ResponseFormat.java

@@ -0,0 +1,50 @@
+package com.free.frame;
+
+import lombok.Data;
+
+/**
+ * 格式化返回结果
+ */
+@Data
+public class ResponseFormat {
+    private String errcode;
+    private String errmsg;
+    private Object data;
+
+    /**
+     * 接口返回成功
+     * @param data
+     * @return
+     */
+    public static ResponseFormat success(Object data) {
+        ResponseFormat rf = new ResponseFormat();
+        rf.setData(data);
+        rf.setErrcode("0");
+        rf.setErrmsg("ok");
+        return rf;
+    }
+
+    /**
+     * 接口返回自定义异常
+     * @param e
+     * @return
+     */
+    public static ResponseFormat customizationError(CustomizationException e) {
+        ResponseFormat rf = new ResponseFormat();
+        rf.setErrmsg(e.getErrMsg());
+        rf.setErrcode(e.getErrCode());
+        return rf;
+    }
+
+    /**
+     * 接口返回其他异常
+     * @return
+     */
+    public static ResponseFormat error() {
+        ExceptionEnum serviceFault = ExceptionEnum.SERVICE_FAULT;
+        ResponseFormat rf = new ResponseFormat();
+        rf.setErrcode(serviceFault.getResultCode());
+        rf.setErrmsg(serviceFault.getResultMsg());
+        return rf;
+    }
+}

+ 9 - 0
src/main/java/com/free/mapper/TestMapper.java

@@ -0,0 +1,9 @@
+package com.free.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.free.entity.Test;
+import org.apache.ibatis.annotations.Mapper;
+
+@Mapper
+public interface TestMapper extends BaseMapper<Test> {
+}

+ 10 - 0
src/main/java/com/free/service/TestService.java

@@ -0,0 +1,10 @@
+package com.free.service;
+
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.free.entity.Test;
+import com.free.mapper.TestMapper;
+import org.springframework.stereotype.Service;
+
+@Service
+public class TestService extends ServiceImpl<TestMapper, Test> {
+}

+ 12 - 0
src/main/java/com/free/test/HelloWorldController.java

@@ -0,0 +1,12 @@
+package com.free.test;
+
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+public class HelloWorldController {
+    @GetMapping("/hello")
+    private String hello() {
+        return "Hello World1234";
+    }
+}

+ 16 - 0
src/main/java/com/free/utils/BaseEntity.java

@@ -0,0 +1,16 @@
+package com.free.utils;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import lombok.Getter;
+import lombok.Setter;
+
+@Getter
+@Setter
+public class BaseEntity {
+    @TableId(type = IdType.AUTO) //mybatis-plus主键注解
+//    @IsKey 						 //actable主键注解
+//    @IsAutoIncrement			 //自增
+//    @Column 					 //对应数据库字段,不配置name会直接采用属性名作为字段名
+    private Long id;
+}

+ 25 - 0
src/main/java/com/free/utils/MyMetaObjectHandler.java

@@ -0,0 +1,25 @@
+package com.free.utils;
+
+import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.ibatis.reflection.MetaObject;
+import org.springframework.stereotype.Component;
+
+import java.time.LocalDateTime;
+
+@Slf4j
+@Component
+public class MyMetaObjectHandler implements MetaObjectHandler {
+
+    @Override
+    public void insertFill(MetaObject metaObject) {
+        log.info("开始插入填充...");
+        this.strictInsertFill(metaObject, "create_time", LocalDateTime.class, LocalDateTime.now());
+    }
+
+    @Override
+    public void updateFill(MetaObject metaObject) {
+        log.info("开始更新填充...");
+        this.strictUpdateFill(metaObject, "update_time", LocalDateTime.class, LocalDateTime.now());
+    }
+}

+ 0 - 0
src/main/resources/application-dev.yml


+ 0 - 0
src/main/resources/application-prod.yml


+ 30 - 0
src/main/resources/application.yml

@@ -0,0 +1,30 @@
+server:
+  port: 4001
+
+spring:
+  datasource:
+    driver-class-name: com.mysql.cj.jdbc.Driver
+    url: jdbc:mysql://localhost:3306/customer?serverTimezone=GMT%2B8
+    username: root
+    password: root
+  devtools:
+    restart:
+      enabled: true
+
+mybatis:
+  table:
+    auto: update
+  model:
+    pack: com.free.entity
+  database:
+    type: mysql
+
+mybatis-plus:
+  mapper-locations: classpath*:com/gitee/sunchenbin/mybatis/actable/mapping/*/*.xml
+#  type-aliases-package: com.free.entity
+  configuration:
+    map-underscore-to-camel-case: true
+    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
+logging:
+  level:
+    Mapper: debug