SpringBoot系列第3篇整合技术

整合Junit

只需要使用SpringBootTest一个注解就可以完成

1
2
3
4
5
6
7
8
9
@SpringBootTest
class SpringApplicationTest {
@Autowired
private BookService bookService;
@Test
public void testSave() {
bookService.save();
}
}

如果测试类不在SpringBoot启动类的包或子包中,需要设置classes

1
@SpringBootTest(classes = SpringBootApplicaiton.class)

整合Mybatis

首先需要导入相关的依赖,依赖包括mybaits,mysql,druid连接池

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!--数据库框架-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.22</version>
</dependency>

然后设置数据源相关的参数

1
2
3
4
5
6
7
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm_db?serverTimezone=GMT
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource

dao层需要添加@Mapper注解

1
2
3
4
5
@Mapper
public interface BookDao {
@Select("select * from tbl_book")
public List<Book> getAll();
}

SpringBoot整合SSM

整合案例使用了之前的SSM整合,所有资源文件可以从SSM项目中获取,SpringBoot整合项目只进行了少部分的修改

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>ssm</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ssm</name>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--排除tomcat-->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>

<!--添加jetty-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<!--自定义封装警告-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

<!--数据库框架-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.22</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

2.然后不需要之前SSM整合中的config目录中,需要在dao中添加@Mapper接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Mapper
public interface BookDao {
@Insert("insert into tbl_book (type, name, description) values(#{type}, #{name}, #{description})")
public int save(Book book);

@Update("update tbl_book set type=#{type}, name=#{name}, description=#{description} where id=#{id}")
public int update(Book book);

@Delete("delete from tbl_book where id =#{id}")
public int delete(Integer id);

@Select("select * from tbl_book where id= #{id}")
public Book getById(Integer id);

@Select("select * from tbl_book")
public List<Book> getAll();
}

3.需要在application.yml文件中配置相关的设置

1
2
3
4
5
6
7
8
9
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm_db?serverTimezone=GMT
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
server:
port: 8081

4.最后一步将前端文件复制到resources中的static目录中,如果想要直接使用端口访问,则需要将page文件夹中的index.html文件复制到static目录中


SpringBoot系列第3篇整合技术
https://www.eldpepar.com/coding/9964/
作者
EldPepar
发布于
2023年2月28日
许可协议