Spring boot集成redis教程

6 篇文章 0 订阅
5 篇文章 0 订阅

Spring boot集成redis教程

项目结构

在这里插入图片描述

POM.XML

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.linyuan</groupId>
	<artifactId>redis_0101</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<!-- 父依赖 -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.3.RELEASE</version>
	</parent>
	<dependencies>
		<!-- spring-boot-starter-data-redis -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
		</dependency>
		<!-- spring-boot-starter-data-web -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- springboot test -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
</project>

application.properties

########################################################
###Redis (RedisConfiguration)
########################################################
spring.redis.database=0
spring.redis.host=127.0.0.1
spring.redis.port=6379
#spring.redis.password=123456
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.timeout=5000

RedisTest测试类

package com.linyuan.redis_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.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import com.linyuan.redis.App;
import com.linyuan.redis.service.RedisService;

/**
 * @author lin
 *
 *springboot项目测试类,用以下两个注解
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes =App.class)
public class RedisTest {
	/**
	 * 注入redisService,此处不用
	 */
	@Autowired
	private RedisService redisService;
	
	
	/**
	 * stringRedisTemplate,用于操作redis
	 */
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	
	/**
	 * 增加一个str类型的数据
	 */
	@org.junit.Test
	public void addString() {
		
		redisService.setStr("name","linyuan");
		
	}
	/**
	 * 操作list,redis中的List好像栈,增加删除也就是压栈和出栈的操作,但是可以从左右两边进行进出栈。
	 * List中的元素可重复。
	 */
	@org.junit.Test
	public void addList() {
		stringRedisTemplate.opsForList().leftPush("stuList", "林");
		stringRedisTemplate.opsForList().rightPush("stuList", "wangwu");
		
	}
	/**
	 * List出栈
	 */
	@org.junit.Test
	public void popList() {
		stringRedisTemplate.opsForList().leftPop("stuList");
	}
	
	
	/**
	 * 操作set,redis中的set好像栈,set底层是hashmap,通过每个值的hashcode确定这个值存在数组中的哪个位置。
	 * set中的元素不可重复。
	 */
	@org.junit.Test
	public void addSet() {
		stringRedisTemplate.opsForSet().add("goodsSet", "苹果");
		stringRedisTemplate.opsForSet().add("goodsSet", "苹果2");
		stringRedisTemplate.opsForSet().add("goodsSet", "苹果1");
		stringRedisTemplate.opsForSet().add("goodsSet", "苹果10");
		// 是否是set中的成员
		Boolean isMember = stringRedisTemplate.opsForSet().isMember("goodsSet", "苹果");
		System.out.println(isMember);
	}
	
	/**
	 * set删除元素
	 */
	@org.junit.Test
	public void removeSet() {
		stringRedisTemplate.opsForSet().remove("goodsSet", "苹果");
	}
	/**
	 * 操作SortedSet,排序的set,根据设置的分数大小,从小到达进行排序
	 * SortedSet中的元素不可重复。
	 */
	@org.junit.Test
	public void addSortedSet() {
		stringRedisTemplate.opsForZSet().add("goodsSortedSet", "苹果",10);
		stringRedisTemplate.opsForZSet().add("goodsSortedSet", "梨",100);
		stringRedisTemplate.opsForZSet().add("goodsSortedSet", "脐橙",8);
	}
	
	/**
	 * SortedSet删除元素
	 */
	@org.junit.Test
	public void removeSortedSet() {
		stringRedisTemplate.opsForZSet().remove("goodsSortedSet", "苹果");
	}
	/**
	 * 操作map,map中的key不能重复
	 */
	@org.junit.Test
	public void addHMSet() {
		stringRedisTemplate.opsForHash().put("person", "name", "linyuan");
		stringRedisTemplate.opsForHash().put("person", "pass", "123456");
	}
	
	/**
	 * map删除元素
	 */
	@org.junit.Test
	public void removeHMSet() {
		stringRedisTemplate.opsForHash().delete("person", "pass");
	}
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值