SpringBoot-整合
整合Junit
1. 添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
2. 测试
准备好需要测试的类,我这里以UserService类中的print_test方法为例
package com.sjj.application.service;
// UserService的测试类
import com.sjj.application.Application; // 这是可运行引导类
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class UserServiceTests {
@Autowired
private UserService userService;
@Test
public void userService_test1(){
userService.print_test();
}
}
整合Redis
1. 添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2. 配置redis
本机的redis才能不配置直接运行
application.yaml
spring:
redis:
host: 127.0.0.1 # 本机
port: 6379 # 默认端口
可以根据自己的需要进行修改
3. 测试
package com.sjj.application;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class RedisTests {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Test
public void redis_testSet(){
// 存数据
redisTemplate.boundValueOps("name").set("test");
}
@Test
public void redis_testGet(){
// 读数据
System.out.println(
redisTemplate.boundValueOps("name").get()
);
}
}
整合Mybatis
1. 添加依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
2. 配置数据源
spring:
# datasource
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql:///数据库名称
username: 用户名
password: 密码
3. 测试
你需要实现准备一个User实体类以及相应的Mapper接口
import com.sjj.application.Application;
import com.sjj.application.domain.User;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class UserMapperTests {
@Autowired
private UserMapper userMapper;
@Test
public void userMapper_test1(){
List<User> users = userMapper.findAll();
System.out.println(users);
}
}
分类:
Spring
标签:
SpringBoot
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!