Spring整合JUnit

SpringBoot整合JUnit

测试类注解:@SpringBootTest
作用:设置JUnit加载的SpringBoot启动类
①使用spring initializr初始化项目的时候,添加依赖。

②设置数据源application.yml
- spring:
- datasource:
- driver-class-name: com.mysql.cj.jdbc.Driver
- url: jdbc:mysql://localhost:3306/ssm_db
- username: root
- password: root
注意:SpringBoot版本低于2.4.3,Mysql驱动版本大于8.0时,需要在url连接串中配置时区。
jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC
③定义数据层接口与映射配置
- @Mapper
- public interface UserDao {
- @select("select * from user")
- public List
getAll(); - }
在上文的基于Spring的SSM案例上进行修改。
① 创建项目


② 在pom.xml中添加druid依赖
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid</artifactId>
- <version>1.2.18</version>
- </dependency>
③ 配置application.yml
- server:
- port: 80
-
- spring:
- datasource:
- type: com.alibaba.druid.pool.DruidDataSource
- driver-class-name: com.mysql.cj.jdbc.Driver
- url: jdbc:mysql://localhost:3306/ssm_db
- username: root
- password: 123456
④ 删除配置类,设置@Mapper。使用@SpringBootTest简化配置类。
⑤ 静态资源放在resources目录下的static目录下