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 // 方法名做组件id,对象里面放值
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);
}
//重写这个extend方法,在数据类型中额外添加自己定义的数据
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(new GuiguconventerMessage());
}
};
}

converter 提交数据时

1660233269866

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","请先登录");
// response.sendRedirect("/login");
request.getRequestDispatcher("/").forward(request,response);
//弹幕:因为前端页面是获取request域中数据,而重定向只能放session数据,为了不改变前端代码,所以这里改为请求转发
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 #开启页面表单的Rest功能

开启请求参数内容协商模式

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
<!--打包jar-->

<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规则
mybatis:
config-location: classpath:mybatis/mybatis-config.xml #全局配置文件位置
mapper-locations: classpath:mybatis/mapper/*.xml #sql映射文件位置

# 或

# 配置mybatis规则
mybatis:
# config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper/*.xml
configuration:
map-underscore-to-camel-case: true # 开启驼峰命名法

# 可以不写全局;配置文件,所有全局配置文件的配置都放在configuration配置项中即可

mapper

注解

  • @Mapper

我不会,长大后再学习

原生组件注入(Servlet,Filters,Listeners)

尚硅谷65p mybatisPlus下次从这p开始看,草,这会不看了

SpringBoot2核心技术与响应式编程 · 语雀 (yuque.com)