springboot入门
java.boot
主程序
1 2 3 4 5 6 7 8 9 10 11 12 13
| package cn.sdadgz.boot;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication public class MainApplication { public static void main(String[] args) { SpringApplication.run(MainApplication.class, args); } }
|
controller
注解:
1 2 3 4
| @RestController @Controller @RequestMapping("[路径]") @ResponseBody
|
config
手动添加bean
1 2 3 4 5 6 7 8 9
| @Configuration public class MyConfig(){ @Bean public User user01(){ return new User("张三",18); } }
|
自定义
拦截器config
1 2 3 4 5 6 7 8 9 10 11
| @Configuration public class adminWebConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new LoginInterceptor()) .addPathPatterns("/**")
.excludePathPatterns("/","/login","/css/**","/fonts/**","/images/**","/js/**");
} }
|
矩阵变量|converter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| @Bean public WebMvcConfigurer webMvcConfigurer(){ return new WebMvcConfigurer() { @Override public void configurePathMatch(PathMatchConfigurer configurer) { UrlPathHelper urlPathHelper = new UrlPathHelper(); urlPathHelper.setRemoveSemicolonContent(false); configurer.setUrlPathHelper(urlPathHelper); } @Override public void extendMessageConverters(List<HttpMessageConverter<?>> converters) { converters.add(new GuiguconventerMessage()); } }; }
|
converter 提交数据时
messageConverter 返回数据时
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| public class GuiguconventerMessage implements HttpMessageConverter<Person> { @Override public boolean canRead(Class<?> aClass, MediaType mediaType) { return false; }
@Override public boolean canWrite(Class<?> aClass, MediaType mediaType) { return true; }
@Override public List<MediaType> getSupportedMediaTypes() { return MediaType.parseMediaTypes("application/x-guigu"); }
@Override public Person read(Class<? extends Person> aClass, HttpInputMessage httpInputMessage) throws IOException, HttpMessageNotReadableException { return null; }
@Override public void write(Person person, MediaType mediaType, HttpOutputMessage httpOutputMessage) throws IOException, HttpMessageNotWritableException { String data = person.getName()+";"+person.getPassword()+";"+person.getAge(); OutputStream body = httpOutputMessage.getBody(); body.write(data.getBytes()); } }
|
@Configuration
- proxyBeanMethods = false // 默认true,其他组件不依赖它时用false更快
@Import({})
- {User.class,Pet.class} // 自动创建组件
@ConditionalOnBean(name = “[tom]”) // tom存在时创建组件,省略其他条件装配
@ImportResource(“classpath:beans.xml”) // 导入xml装配
@EnableConfigurationProperties(User.class) // 自动装配配置文件
pojo
注解:
@Component // 组件
@ConfigurationPeroperties(Prefix = “[mycar]”) // 配置文件自动装配
拦截器旧
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| package com.atguigu.springboot.demo2.interceptor;
import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession;
public class LoginInterceptor implements HandlerInterceptor {
@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { HttpSession session = request.getSession(); Object loginuser = session.getAttribute("loginuser"); if(loginuser != null){ return true; }else { session.setAttribute("msg","请先登录");
request.getRequestDispatcher("/").forward(request,response); return false;
} }
@Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { HandlerInterceptor.super.postHandle(request, response, handler, modelAndView); }
@Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { HandlerInterceptor.super.afterCompletion(request, response, handler, ex); } }
|
文件上传
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| @Controller public class FormTestController { @GetMapping("/form_layouts") public String formLayout(){ return "form/form_layouts"; }
@PostMapping("/upload") public String uploadForm( @RequestParam("email") String email, @RequestParam("username") String username, @RequestParam("headerImg")MultipartFile headerImg, @RequestParam("photos") MultipartFile [] photos // 自动封装 ){
System.out.println("email:"+email+"username:"+username+"headering:"+headerImg.getSize()+"photos:"+photos.length);
if(!headerImg.isEmpty()){ try {
String originalFilename = headerImg.getOriginalFilename(); headerImg.transferTo(new File("D:/cache/"+originalFilename)); } catch (IOException e) { throw new RuntimeException(e); } }
if (photos.length>0){ for (MultipartFile photo : photos) { if(!photo.isEmpty()){ String originalFilename = photo.getOriginalFilename(); try { photo.transferTo(new File("D:/cache/"+originalFilename)); } catch (IOException e) { throw new RuntimeException(e); } } } }
return "main"; } }
|
resources
application.properties|application.yaml
springboot配置文件 官网
开启页面表单的Rest功能
1 2 3 4 5
| spring: mvc: hiddenmethod: filter: enabled: true
|
开启请求参数内容协商模式
1 2 3 4 5
| spring: mvc: contentnegotiation: favor-parameter: true
|
pom
导入JDBC
导入boot
1 2 3 4 5 6 7 8 9 10 11 12
| <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.2</version> </parent>
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
|
yaml和properties自动提示
1 2 3 4 5
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
|
maven打包jar
1 2 3 4 5 6 7 8 9 10 11 12
|
<packaging>jar</packaging>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
|
maven重写boot配置
1 2 3 4 5 6 7 8 9 10
| <properties> <mysql.version>5.1.43</mysql.version> </properties>
<dependecies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependecies>
|
springboot+mybatis
导入pom
1 2 3 4 5
| <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency>
|
配置文件properties|yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| mybatis: config-location: classpath:mybatis/mybatis-config.xml mapper-locations: classpath:mybatis/mapper/*.xml
mybatis:
mapper-locations: classpath:mybatis/mapper/*.xml configuration: map-underscore-to-camel-case: true
|
mapper
注解
我不会,长大后再学习
原生组件注入(Servlet,Filters,Listeners)
尚硅谷65p mybatisPlus下次从这p开始看,草,这会不看了
SpringBoot2核心技术与响应式编程 · 语雀 (yuque.com)