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
相关推荐
014-code1 小时前
布隆过滤器:判断“可能存在“和“一定不存在“
java·redis
gQ85v10Db1 小时前
Redis分布式锁进阶第十八篇:本地缓存+分布式锁双锁架构 + 高并发削峰兜底 + 极致性能无损优化实战
redis·分布式·缓存
gQ85v10Db2 小时前
Redis分布式锁进阶第十四篇:全系列终局架构复盘 + 锁体系统一规范 + 线上全年零事故收官方案
redis·分布式·架构
KmSH8umpK2 小时前
Redis分布式锁进阶第十二篇
数据库·redis·分布式
gQ85v10Db3 小时前
Redis分布式锁进阶第十六篇:番外高阶避坑篇 + 隐性埋点锁故障深挖 + 疑难杂症终极兜底方案
数据库·redis·分布式
Arya_aa3 小时前
数据字典模块–MapStruct对象转换与加密处理
spring boot
KmSH8umpK4 小时前
Redis分布式锁从原生手写到Redisson高阶落地,附线上死锁复盘优化方案进阶第九篇
数据库·redis·分布式
gQ85v10Db4 小时前
Redis分布式锁进阶第十五篇:全系列终极收官复盘 + 全站锁规范归档 + 生产零故障长期运维兜底总方案
运维·redis·分布式
RuoyiOffice4 小时前
2026 年开源 BPM/工作流引擎大盘点:Flowable vs Camunda vs Activiti vs Turbo——谁才是企业级首选?
java·spring boot·后端·开源·流程图·ruoyi·anti-design-vue