diff --git a/pom.xml b/pom.xml index 46fa85a3a5e88b8a9d757b5c2f026b9dd9ace111..10de49d1d7cc8699d4c9001a5f85da0d6859a4bc 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 70227d8b8327567f49e25d167ea7493d8a96beee..b4d2cc57660f582b73a33dd58f00e514c543534b 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 0000000000000000000000000000000000000000..e3a9ed16c35a6b0de8fb30cee97b0709a9ddabf2 --- /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 0000000000000000000000000000000000000000..ab087d22f45a6187a6012c5771f0c191d29d3406 --- /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 0000000000000000000000000000000000000000..5f1a1c2353c65419ec59f55e082cbdbad30dc230 --- /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 0000000000000000000000000000000000000000..9b601622812d8cbde8abfac74701eff1dbaf3b96 --- /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 7c3ab9e9ab9a78884c6f9d4ee5964aecb0ba1f0a..4c5c08e727decda343b409c435a0f27fa0a0a25a 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 0000000000000000000000000000000000000000..6e1b3993c2f6d19feb46778cca54b1b840a9eccf --- /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 27e180782142a1ebde068d10b63fc0fcb049c843..19688a541c4d62198f6eab2f116a7ebc3b3bf613 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 { }