提交 3d06f36f 编写于 作者: Q qinyingjie

fix:添加springboot功能

上级 d0df4171
......@@ -69,6 +69,12 @@
<version>1.3.0.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
......
......@@ -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());
......
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");
}
}
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;
}
}
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";
}
}
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
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 {
......
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;
}
}
......@@ -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<User> {
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册