1.1.5、SpringBoot整合其他框架

一、整合Junit

  1. 搭建SpringBoot工程

  2. 引入starter-test起步依赖

     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
     </dependency>
  3. 编写测试类

  4. 添加测试相关注解

    1. @RunWith(SpringRunner.class)

    2. @SpringBootTest(classes = 启动类.class)

      备注:添加“启动类.class”是因为测试类没有在对应的启动类包下,如果在对应的包下,无需添加

  5. 编写测试方法

二、整合Redis

  1. 搭建SpringBoot工程

  2. 引入Redis起步依赖

     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-data-redis</artifactId>    
     </dependency>
  3. 配置redis相关属性

     # 不配置的话,默认链接到本机redis
     spring:
         redis:
         host: 127.0.0.1
         port: 6379
         ......
  4. 注入RedisTemplate模板

     @Autowirde
     private RedisTemplate redisTemplate;
  5. 编写测试方法,测试

三、整合MyBatis

  1. 搭建SpringBoot工程

  2. 引入mybatis起步依赖,添加MySQL驱动

  3. 编写DataSource和MyBatis相关配置

  4. 定义表和实体类

  5. 编写dao和mapper文件/纯注解开发

  6. 测试

Last updated

Was this helpful?