SpringBoot已经整合了Spring和SpringMVC

SpringBoot整合MyBatis

image.png

提供实体类

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
public class Book {
private Integer id;
private String name;
private String type;
private String description;

public Book() {
}

public Book(Integer id, String name, String type, String description) {
this.id = id;
this.name = name;
this.type = type;
this.description = description;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", type='" + type + '\'' +
", description='" + description + '\'' +
'}';
}
}

数据层接口

注意需要添加@Mapper注解

1
2
3
4
5
@Mapper
public interface BookDao {
@Select("select * from tb_book where id = #{id}")
public Book getById(Integer id);
}

配置yml文件

1
2
3
4
5
6
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql:///db1
username: root
password: root

整合SSM案例

参见之前的Spring整合SSM,添加依赖坐标,数据层接口添加@Mapper注解,配置yml文件,即可完成。

静态页面需要放在resources/static目录下。