springboot学习代码

参考:https://gitee.com/CandyWall/spring-boot-study

restful 风格

  1. @ResponseBody
  2. @RequestParam
    详情:https://blog.csdn.net/sswqzx/article/details/84195043
  3. @PathVariable
  4. //@Controller
    //@ResponseBody
    @RestController
  5. 可以将公有路由提取到类外面
  6. 方法可以简写为 getmapping…
1
2
3
4
5
6
7
@Component
@Data
public class User {
private Integer id;
private String username;
private String password;
}
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
@Controller
public class BookController {
@RequestMapping(value = "/users1", method = RequestMethod.POST)
@ResponseBody
public String save() {
System.out.println("user1 save...");
return "{'module':'user1 save'}";
}
@RequestMapping(value = "/users1/{id}", method = RequestMethod.DELETE)
@ResponseBody
public String delete(@PathVariable Integer id) {
System.out.println("user1 delete..." + id);
return "{'module':'user1 delete'}";
}

@RequestMapping(value = "/users1", method = RequestMethod.PUT)
@ResponseBody
public String update(@RequestBody User user) {
System.out.println("user1 update..." + user);
return "{'module':'user1 update'}";
}

@RequestMapping(value = "/users1/{id}", method = RequestMethod.GET)
@ResponseBody
public String getById(@PathVariable Integer id) {
System.out.println("user1 getById..." + id);
return "{'module':'user1 getById'}";
}

@RequestMapping(value = "/users1", method = RequestMethod.GET)
@ResponseBody
public String getAll() {
System.out.println("user1 getAll...");
return "{'module':'user1 getAll'}";
}
}

简化

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
//@Controller
//@ResponseBody
@RestController
@RequestMapping("/users2")
public class BookController {
//@RequestMapping(method = RequestMethod.POST)
@PostMapping
public String save(@RequestBody User user) {
System.out.println("user2 save..." + user);
return "{'module':'user2 save'}";
}

//@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
@DeleteMapping("/{id}")
public String delete(@PathVariable Integer id) {
System.out.println("user2 delete..." + id);
return "{'module':'user2 delete'}";
}

//@RequestMapping(method = RequestMethod.PUT)
@PutMapping
public String update(@RequestBody User user) {
System.out.println("user2 update..." + user);
return "{'module':'user2 update'}";
}

//@RequestMapping(value = "/{id}", method = RequestMethod.GET)
@GetMapping("/{id}")
public String getById(@PathVariable Integer id) {
System.out.println("user2 getById..." + id);
return "{'module':'user2 getById'}";
}

//@RequestMapping(method = RequestMethod.GET)
@GetMapping
public String getAll() {
System.out.println("user2 getAll...");
return "{'module':'user2 getAll'}";
}
}

yaml

  1. application.yml
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
country: China
province: Beijing
city: Beijing
area: Haidian

party: true

birthday: 1949-10-01

user:
name: zhangsan
age: 17

user2:
name: lisi
age: 18

a:
b:
c:
d:
e: 123

habits:
- game
- music
- sleep

habits2: [game, music, sleep]

users:
- name: zhangsan
age: 40
- name: lisi
age: 38
- name: wangwu
age: 15

users2:
-
name: zhangsan
age: 40
-
name: lisi
age: 38
-
name: wangwu
age: 15

users3: [{name: zhangsan, age: 40}, {name: lisi, age: 38}, {name: wangwu, age: 15}]

baseDir: C:\windows

tempDir: ${baseDir}\temp

tempDir1: "${baseDir}\temp"

datasource:
driver: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/springboot_db
username: root
password: 123
  1. MyDataSource.java
1
2
3
4
5
6
7
8
9
@ConfigurationProperties(prefix = "datasource")
@Component
@Data
public class MyDataSource {
private String driver;
private String url;
private String username;
private String password;
}
  1. 数据显示
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
@RestController
@RequestMapping("/books")
public class BookController {
// 读取yaml数据中的单一数据
@Value("${country}")
private String country1;

@Value("${user.name}")
private String name1;

@Value("${user2.name}")
private String name2;

@Value("${habits[1]}")
private String habit;

@Value("${users[1].age}")
private Integer age;

@Value("${tempDir}")
private String tempDir;

@Value("${tempDir1}")
private String tempDir1;

// 使用自动装配将所有的数据封装到Environment对象中
@Autowired
private Environment env;

@Autowired
private MyDataSource dataSource;

@GetMapping
public String getById() {
System.out.println("springboot is running...");
System.out.println("country1 = " + country1);
System.out.println("name1 = " + name1);
System.out.println("name2 = " + name2);
System.out.println("habit = " + habit);
System.out.println("age = " + age);
System.out.println("tempDir = " + tempDir);
System.out.println("tempDir1 = " + tempDir1);
System.out.println("====================================");
System.out.println("country1 = " + env.getProperty("country"));
System.out.println("name1 = " + env.getProperty("user.name"));
System.out.println("name2 = " + env.getProperty("user2.name"));
System.out.println("habit = " + env.getProperty("habits[1]"));
System.out.println("age = " + env.getProperty("users[1].age"));
System.out.println("tempDir = " + env.getProperty("tempDir"));
System.out.println("tempDir1 = " + env.getProperty("tempDir1"));
System.out.println("=====================================");
System.out.println("dataSource = " + dataSource);
return "springboot is running...";
}
}

整合 junit

  1. 创建模块,不勾选任何选项
  2. BookDao 接口
1
2
3
public interface BookDao {
public void save();
}
  1. BookDaoImpl 类
1
2
3
4
5
6
7
@Repository
public class BookDaoImpl implements BookDao {
@Override
public void save(){
System.out.println("bookDao is running...");
}
}
  1. 测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 如果测试类跟springboot运行引导类不在同一个包下,那么需要给@SpringBootTest注解加classes属性
@SpringBootTest(classes = Demo1Application.class)
class Demo1ApplicationTests {

// 1、注入要测试的对象
@Autowired
private BookDao bookDao;

@Test
void contextLoads() {
// 2、执行要测试的对象对应的方法
bookDao.save();
}


}

整合 mybatis

  1. 导入模块选择对应的技术:
    SQL 中的 MyBatis Framwork 和 MySQL Driver
  2. application.yml 配置数据库信息
1
2
3
4
5
6
7
# 配置数据信息
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test1
username: root
password: xxx
  1. domain.Book.java
1
2
3
4
5
6
7
@Component
@Data
public class Book {
private String username;
private String password;
private Integer age;
}
  1. 接口:dao.BookDao
1
2
3
4
5
@Mapper
public interface BookDao {
@Select("select * from users where age = #{age}")
Book getByAge(Integer age);
}
  1. 测试:
1
2
3
4
5
6
7
8
9
10
11
@SpringBootTest
class Demo2ApplicationTests {
@Autowired
private BookDao bookDao;

@Test
void contextLoads() {
System.out.println(bookDao.getByAge(11));
}

}

整合 druid

访问量 访客