SpringBoot

SpringBoot配置

建议使用yaml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# yaml强制要求k-v之间有空格
name: zhangsan

# 创建实体类
student:
name: zhangsan
age: 20

student: {name: zhangsan, age: 20}

# 创建数组
pets:
- cat
- dog
- pig

pets: [cat, dog, pig]

属性赋值

@Value

实体类

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
package com.hug.pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Dog {
@Value("章帅")
private String name;
@Value("3")
private Integer age;

public Dog() {
}

public Dog(String name, Integer age) {
this.name = name;
this.age = age;
}

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;
}

@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}

测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.hug;

import com.hug.pojo.Dog;
import com.hug.pojo.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class SpringBootApplicationTest {
@Autowired
private Dog dog;

@Autowired
private Person person;

@Test
public void test01(){
System.out.println(dog);
// Dog{name='章帅', age=3}
}
}

@ConfigurationProperties

@ConfigurationProperties(prefix = “person”) 使用方式,将yaml文件中的定义的类,填充到实体类中

yaml文件内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
spring:
jackson:
date-format: yyyy-MM-dd
time-zone: GMT+8

person:
name: zhangsan
age: 3
happy: false
brith: 2023/04/10
maps: {k1: v1, k2: v2}
lists:
- code
- music
- girl
dog:
name: "章帅"
age: 3

实体类内容

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package com.hug.pojo;


import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String name;
private Integer age;
private Boolean happy;
private Date brith;
private Map<Object, Object> maps;
private List<Object> lists;

private Dog dog;

public Person() {
}

public Person(String name, Integer age, Boolean happy, Date brith, Map<Object, Object> maps, List<Object> lists, Dog dog) {
this.name = name;
this.age = age;
this.happy = happy;
this.brith = brith;
this.maps = maps;
this.lists = lists;
this.dog = dog;
}

public Dog getDog() {
return dog;
}

public void setDog(Dog dog) {
this.dog = dog;
}

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 Boolean getHappy() {
return happy;
}

public void setHappy(Boolean happy) {
this.happy = happy;
}

public Date getBrith() {
return brith;
}

public void setBrith(Date brith) {
this.brith = brith;
}

public Map<Object, Object> getMaps() {
return maps;
}

public void setMaps(Map<Object, Object> maps) {
this.maps = maps;
}

public List<Object> getLists() {
return lists;
}

public void setLists(List<Object> lists) {
this.lists = lists;
}

@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", happy=" + happy +
", brith=" + brith +
", maps=" + maps +
", lists=" + lists +
", dog=" + dog +
'}';
}
}

测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.hug;

import com.hug.pojo.Dog;
import com.hug.pojo.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class SpringBootApplicationTest {
@Autowired
private Dog dog;

@Autowired
private Person person;

@Test
public void test01(){
// Person{name='zhangsan', age=3, happy=false, brith=Mon Apr 10 00:00:00 CST 2023, maps={k1=v1, k2=v2}, lists=[code, music, girl], dog=Dog{name='章帅', age=3}}
System.out.println(person);
}
}

JSP303校验

@Validated

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
@Component
@ConfigurationProperties(prefix = "person")
@Validated // 通过添加这个注解,来进行校验标识
public class Person {
@Email(message = "这不是一个邮箱格式")
private String name;
private Integer age;
private Boolean happy;
private Date brith;
private Map<Object, Object> maps;
private List<Object> lists;

private Dog dog;

public Person() {
}

public Person(String name, Integer age, Boolean happy, Date brith, Map<Object, Object> maps, List<Object> lists, Dog dog) {
this.name = name;
this.age = age;
this.happy = happy;
this.brith = brith;
this.maps = maps;
this.lists = lists;
this.dog = dog;
}

public Dog getDog() {
return dog;
}

public void setDog(Dog dog) {
this.dog = dog;
}

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 Boolean getHappy() {
return happy;
}

public void setHappy(Boolean happy) {
this.happy = happy;
}

public Date getBrith() {
return brith;
}

public void setBrith(Date brith) {
this.brith = brith;
}

public Map<Object, Object> getMaps() {
return maps;
}

public void setMaps(Map<Object, Object> maps) {
this.maps = maps;
}

public List<Object> getLists() {
return lists;
}

public void setLists(List<Object> lists) {
this.lists = lists;
}

@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", happy=" + happy +
", brith=" + brith +
", maps=" + maps +
", lists=" + lists +
", dog=" + dog +
'}';
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.hug;

import com.hug.pojo.Dog;
import com.hug.pojo.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class SpringBootApplicationTest {
@Autowired
private Dog dog;

@Autowired
private Person person;

@Test
public void test01(){
System.out.println(person);
}
}

WEB开发

  • 导入静态资源
  • 首页
  • Thymeleaf
  • 装配SpringMVC
  • 增删改查
  • 拦截器
  • 国际化