SpringBoot整合Junit-Redis-打包

SpringBoot整合Junit-Redis-打包

1.整合Junit

  1. 在springboot项目中使用Junit进行单元测试,添加依赖
xml 复制代码
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-test</artifactId>
</dependency>
  1. 在测试包下面编写测试类
    在测试类上面必须加上@Springboot注解
    编写测试类:service\UserServiceTest.java
java 复制代码
import com.itheima.pojo.User;
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;
import java.util.Date;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserServiceTest {
	@Autowired
	private UserService userService;
	@Test
	public void queryById() {
		User user = userService.queryById(1L);
		System.out.println("user = " + user);
	}
	@Test
	public void saveUser() {
		User user = new User();
		user.setUserName("test");
		user.setName("test");
		user.setPassword("123456");
		user.setSex(1);
		user.setAge(20);
		user.setCreated(new Date());
		userService.saveUser(user);
	}

2.整和Redis

  1. 在pom.xml文件中添加依赖
xml 复制代码
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
  1. 配置application.yml
yml 复制代码
spring:
	redis: localhost
	port: 6379
  1. 编写\redis\RedisTest.java 测试代码
java 复制代码
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.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;
import java.util.Set;
@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisTest {
	@Autowired
	private RedisTemplate redisTemplate;
	@Test
	public void test(){
		//string字符串
		//redisTemplate.opsForValue().set("str", "heima");
		redisTemplate.boundValueOps("str").set("heima");
		System.out.println("str = " + redisTemplate.opsForValue().get("str"));
		//hash散列
		redisTemplate.boundHashOps("h_key").put("name", "黑马");
		redisTemplate.boundHashOps("h_key").put("age", 13);

		//获取所有域对应的值
		Set set = redisTemplate.boundHashOps("h_key").keys();
		System.out.println("hash散列所有的域:" + set);
		List list = redisTemplate.boundHashOps("h_key").values();
		System.out.println("hash散列所有的域值:" + list);
		//list列表
		redisTemplate.boundListOps("l_key").leftPush("c");
		redisTemplate.boundListOps("l_key").leftPush("b");
		redisTemplate.boundListOps("l_key").leftPush("a");
		list = redisTemplate.boundListOps("l_key").range(0, -1);
		System.out.println("列表的值:" + list);
		//set集合
		redisTemplate.boundSetOps("set_key").add("a", "b", "c");
		set = redisTemplate.boundSetOps("set_key").members();
		System.out.println("集合的元素:" + set);
		//sorted set有序集合
		redisTemplate.boundZSetOps("z_key").add("a", 30);
		redisTemplate.boundZSetOps("z_key").add("b", 20);
		redisTemplate.boundZSetOps("z_key").add("c", 10);
		set = redisTemplate.boundZSetOps("z_key").range(0, -1);
		System.out.println("有序集合的元素:" + set);
		}
}
  1. 解决Reids乱码配置类:RedisConfig.java
java 复制代码
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);

        // 序列化配置
        StringRedisSerializer stringSerializer = new StringRedisSerializer();
        Jackson2JsonRedisSerializer<Object> jsonSerializer = new Jackson2JsonRedisSerializer<>(Object.class);

        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.activateDefaultTyping(om.getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.NON_FINAL);
        jsonSerializer.setObjectMapper(om);

        // KEY 用 String 序列化
        template.setKeySerializer(stringSerializer);
        template.setHashKeySerializer(stringSerializer);

        // VALUE 用 JSON 序列化(解决乱码)
        template.setValueSerializer(jsonSerializer);
        template.setHashValueSerializer(jsonSerializer);

        template.afterPropertiesSet();
        return template;
    }
}

3. SpringBoot项目部署

  1. 项目打包
    添加项目的pom.xml插件;在pom.xml要显式的加入插件spring-boot-maven-plugin,否则无法产生 jar 清单
    文件,导致打出来的 jar 无法使用命令运行;
xml 复制代码
<build>
	<plugins>
	<!-- 打jar包时如果不配置该插件,打出来的jar包没有清单文件 -->
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
		</plugin>
	</plugins>
</build>
  1. 使用maven命令package打包

    之后在项目下的 target 目录中将有如下jar包:
  2. 运行jar包
    java -jar xxx-1.0-SNAPSHOT.jar
相关推荐
llz_1123 小时前
web-第四次课后作业
前端·spring boot·web
一杯奶茶¥4 小时前
基于springboot的失物招领管理系统带万字文档 校园失物招领管理系统 失物认领管理系统java springboot vue
java·vue.js·spring boot·java项目
不能只会打代码5 小时前
边缘视频分析平台的架构设计与性能优化——从750ms到190ms的调优之路
java·spring boot·redis·性能优化·边缘计算·物联网竞赛
xufengzhu8 小时前
第三方 Python 库 redis-py + hiredis 的使用
开发语言·redis·python
雨辰AI9 小时前
生产级实测:SpringBoot3 + 达梦数据库接口从 200ms 优化至 20ms 完整调优指南
java·数据库·spring boot·后端·政务
轻刀快马10 小时前
跨越软硬件的共鸣(二):从 Cache 写策略看 Redis 与 DB 的一致性博弈
java·开发语言·redis·计算机组成原理
砍材农夫12 小时前
物联网实战|Spring Boot + Netty 搭建 MQTT 消息路由与流转层
java·spring boot·后端·物联网·spring
lazy H12 小时前
Spring Boot 项目如何连接 Redis?新手入门配置和常见错误总结
ide·spring boot·redis·后端·学习·intellij-idea
SXJR13 小时前
spring boot + langchain4j +milvus实现向量存储
java·spring boot·后端·大模型·milvus·rag·langchain4j
王木风13 小时前
Spring Boot + LLM 工程化:把短视频流水线拆成 16 个独立角色的踩坑记录
人工智能·spring boot·后端·开源·新媒体运营·音视频·agent