From 3d06f36f03b4449a02a4a151a390c578a6065b46 Mon Sep 17 00:00:00 2001 From: qinyingjie Date: Mon, 19 Dec 2022 16:13:19 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E6=B7=BB=E5=8A=A0springboot=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 6 ++ .../config/MyBatisPlusConfig.java | 9 ++- .../springbootkwan/config/MyWebMvcConfig.java | 31 ++++++++ .../controller/BookController.java | 27 +++++++ .../controller/FileUploadController.java | 71 +++++++++++++++++++ .../controller/PersonController.java | 37 ++++++++++ .../controller/UserController.java | 10 ++- .../kwan/springbootkwan/entity/Person.java | 37 ++++++++++ .../springbootkwan/mapper/UserMapper.java | 12 +++- 9 files changed, 235 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/kwan/springbootkwan/config/MyWebMvcConfig.java create mode 100644 src/main/java/com/kwan/springbootkwan/controller/BookController.java create mode 100644 src/main/java/com/kwan/springbootkwan/controller/FileUploadController.java create mode 100644 src/main/java/com/kwan/springbootkwan/controller/PersonController.java create mode 100644 src/main/java/com/kwan/springbootkwan/entity/Person.java diff --git a/pom.xml b/pom.xml index 46fa85a..10de49d 100644 --- a/pom.xml +++ b/pom.xml @@ -69,6 +69,12 @@ 1.3.0.Final compile + + org.springframework.boot + spring-boot-devtools + true + + diff --git a/src/main/java/com/kwan/springbootkwan/config/MyBatisPlusConfig.java b/src/main/java/com/kwan/springbootkwan/config/MyBatisPlusConfig.java index 70227d8..b4d2cc5 100644 --- a/src/main/java/com/kwan/springbootkwan/config/MyBatisPlusConfig.java +++ b/src/main/java/com/kwan/springbootkwan/config/MyBatisPlusConfig.java @@ -9,13 +9,20 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.transaction.annotation.EnableTransactionManagement; +/** + * mybatisplus配置 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2022/12/19 16:11 + */ @MapperScan("com.kwan.springbootkwan.mapper")//扫描我们的mapper文件夹 这里添加了@MapperScan注解就不需要Springboot启动类上加入@MapperScan注解了 @EnableTransactionManagement //事务控制 @Configuration //配置类 public class MyBatisPlusConfig { @Bean - public MybatisPlusInterceptor mybatisPlusInterceptor(){ + public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); //注册乐观锁插件 interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor()); diff --git a/src/main/java/com/kwan/springbootkwan/config/MyWebMvcConfig.java b/src/main/java/com/kwan/springbootkwan/config/MyWebMvcConfig.java new file mode 100644 index 0000000..e3a9ed1 --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/config/MyWebMvcConfig.java @@ -0,0 +1,31 @@ +package com.kwan.springbootkwan.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +/** + * 全局拦截器-跨域配置 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2022/12/19 16:11 + */ +@Configuration +public class MyWebMvcConfig implements WebMvcConfigurer { + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/static/**") + .addResourceLocations("classpath:/static/"); + } + + @Override + public void addCorsMappings(CorsRegistry registry) { + registry.addMapping("/book/**") + .allowedHeaders("*") + .allowedMethods("*") + .maxAge(1800) + .allowedOrigins("http://localhost:8081"); + } +} diff --git a/src/main/java/com/kwan/springbootkwan/controller/BookController.java b/src/main/java/com/kwan/springbootkwan/controller/BookController.java new file mode 100644 index 0000000..ab087d2 --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/controller/BookController.java @@ -0,0 +1,27 @@ +package com.kwan.springbootkwan.controller; + +import org.springframework.web.bind.annotation.*; + +/** + * 方法上跨域配置 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2022/12/19 16:10 + */ +@RestController +public class BookController { + + @CrossOrigin(value = "http://localhost:8081", maxAge = 1800, allowedHeaders = "*") + @PostMapping("addBook") + public String addBook(String name) { + return "receive:" + name; + } + + + @CrossOrigin(value = "http://localhost:8081", maxAge = 1800, allowedHeaders = "*") + @DeleteMapping("deleteBook/{id}") + public String deleteBook(@PathVariable(value = "id") String id) { + return "receive:" + id; + } +} diff --git a/src/main/java/com/kwan/springbootkwan/controller/FileUploadController.java b/src/main/java/com/kwan/springbootkwan/controller/FileUploadController.java new file mode 100644 index 0000000..5f1a1c2 --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/controller/FileUploadController.java @@ -0,0 +1,71 @@ +package com.kwan.springbootkwan.controller; + +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestPart; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; + +import javax.servlet.http.HttpServletRequest; +import java.io.File; +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.UUID; + +/** + * 文件上传 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2022/12/19 16:07 + */ +@RestController +public class FileUploadController { + + /** + * 单文件上传 + * + * @param uploadFile + * @param req + * @return + */ + @PostMapping("/upload") + public String upload(MultipartFile uploadFile, HttpServletRequest req) { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + String realPath = req.getSession().getServletContext().getRealPath("/uploadFile/"); + String format = sdf.format(new Date()); + File folder = new File(realPath + format); + if (!folder.isDirectory()) { + folder.mkdirs(); + } + String oldName = uploadFile.getOriginalFilename(); + String newName = UUID.randomUUID().toString() + + oldName.substring(oldName.lastIndexOf("."), oldName.length()); + try { + //文件保存操作 + uploadFile.transferTo(new File(folder, newName)); + //生成上传文件的访问路径, 并将访问路径返回 + String filePath = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + + "/uploadFile/" + format + newName; + return filePath; + } catch (IOException e) { + e.printStackTrace(); + } + return "文件上传失败"; + } + + + /** + * 多文件上传 + * + * @param uploadFiles + * @param req + * @return + */ + @PostMapping("/uploadBatch") + public String uploadBatch(@RequestPart("uploadFile") MultipartFile[] uploadFiles, HttpServletRequest req) { + //遍历 uploadFiles 数组分别存储 + return "success"; + } + +} diff --git a/src/main/java/com/kwan/springbootkwan/controller/PersonController.java b/src/main/java/com/kwan/springbootkwan/controller/PersonController.java new file mode 100644 index 0000000..9b60162 --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/controller/PersonController.java @@ -0,0 +1,37 @@ +package com.kwan.springbootkwan.controller; + +import com.kwan.springbootkwan.entity.Person; +import com.kwan.springbootkwan.entity.User; +import com.kwan.springbootkwan.service.IUserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.Date; +import java.util.List; + +/** + * Person相关 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2022/12/19 16:08 + */ +@RestController +@RequestMapping("/person") +public class PersonController { + /** + * { + * "name": "zhang san", + * "age": 24, + * "birthday": "2022-12-19" + * } + */ + @GetMapping("/person") + public Person person() { + Person person = new Person(); + person.setName("zhang san"); + person.setAge(24); + person.setBirthday(new Date()); + return person; + } +} \ No newline at end of file diff --git a/src/main/java/com/kwan/springbootkwan/controller/UserController.java b/src/main/java/com/kwan/springbootkwan/controller/UserController.java index 7c3ab9e..4c5c08e 100644 --- a/src/main/java/com/kwan/springbootkwan/controller/UserController.java +++ b/src/main/java/com/kwan/springbootkwan/controller/UserController.java @@ -1,13 +1,21 @@ package com.kwan.springbootkwan.controller; +import com.kwan.springbootkwan.entity.Person; import com.kwan.springbootkwan.entity.User; import com.kwan.springbootkwan.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import java.util.Date; import java.util.List; - +/** + * 用户相关 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2022/12/19 16:08 + */ @RestController @RequestMapping("/user") public class UserController { diff --git a/src/main/java/com/kwan/springbootkwan/entity/Person.java b/src/main/java/com/kwan/springbootkwan/entity/Person.java new file mode 100644 index 0000000..6e1b399 --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/entity/Person.java @@ -0,0 +1,37 @@ +package com.kwan.springbootkwan.entity; + +import com.fasterxml.jackson.annotation.JsonFormat; + +import java.util.Date; + +public class Person { + + private String name; + private Integer age; + @JsonFormat(pattern = "yyyy-MM-dd") + private Date birthday; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } + + public Date getBirthday() { + return birthday; + } + + public void setBirthday(Date birthday) { + this.birthday = birthday; + } +} diff --git a/src/main/java/com/kwan/springbootkwan/mapper/UserMapper.java b/src/main/java/com/kwan/springbootkwan/mapper/UserMapper.java index 27e1807..19688a5 100644 --- a/src/main/java/com/kwan/springbootkwan/mapper/UserMapper.java +++ b/src/main/java/com/kwan/springbootkwan/mapper/UserMapper.java @@ -2,10 +2,16 @@ package com.kwan.springbootkwan.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.kwan.springbootkwan.entity.User; -import org.springframework.stereotype.Repository; +import org.apache.ibatis.annotations.Mapper; -//@Mapper -@Repository // 代表持久层 +/** + * mapper文件 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2022/12/19 16:12 + */ +@Mapper public interface UserMapper extends BaseMapper { } -- GitLab