SpringBoot 整合 redis

本文将介绍如何在SpringBoot项目中集成并配置Redis,通过properties.properties文件进行关键设置,实现数据缓存功能。
第一步 引入 jar 包
		<!-- 引入父类项目 -->
			<parent>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-starter-parent</artifactId>
				<version>1.4.3.RELEASE</version>
			</parent>
			
			<dependencies>
			
				<dependency>
					<groupId>org.springframework.boot</groupId>
					<artifactId>spring-boot-starter-web</artifactId>
				</dependency>
				
				<!-- springboot 整合 Redis -->
				<dependency>
					<groupId>org.springframework.boot</groupId>
					<artifactId>spring-boot-starter-data-redis</artifactId>
				</dependency>
				
				<dependency>
					<groupId>org.springframework.boot</groupId>
					<artifactId>spring-boot-starter-thymeleaf</artifactId>
				</dependency>
				
			</dependencies>

第二步 properties.properties 配置文件的信息

package com.service;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class RedisTest {
	
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	
	//*************** string 类型  *************************************************************
	
	//*************** string 类型 set 存放数据
	public void setString(String key,String value){
		//redis: string类型的 set
		stringRedisTemplate.opsForValue().set(key, value);
	}
	
	//*************** string 类型 get 获取数据
	public String getString(String key){
		//redis: string类型的 get
		return stringRedisTemplate.opsForValue().get(key);
	}
	
	//*************** hash 类型  *************************************************************
	//redis: hash类型的 set
	public void setHash(String key,String filedkey,String value){
		stringRedisTemplate.opsForHash().put(key, filedkey, value);
	}
	
	//redis: hash类型的 set 整个 map
	public void setHashAll(String key,Map<Object, Object> value){
		System.err.println(key+"    "+value);
		
		stringRedisTemplate.opsForHash().putAll(key, value);
	}
	
    //redis:hash类型的set,如果key存在则不覆盖
	public void setHashAbsent(String key,String filedKey,String value) {
    	stringRedisTemplate.opsForHash().putIfAbsent(key, filedKey, value);
    }
	
	//redis:hash 类型的  get 获取整个 map
	public Map<Object, Object> getHashAll(String key){
		Map<Object, Object> map = stringRedisTemplate.opsForHash().entries(key);
		return map;
	}
	
	
	//*************** List 类型  ****************************************************************
	// reids:list类型的set,leftPush中,pivot参数是list中的value值,如果pivot=bb,即往bb元素前面插入aa
	// rightPush是往后插入元素,     注意:插入成功则返回list的long类型的元素个数,如果不成功则返回-1
	public Long setList(String key,String pivot,String value){
		 Long rightPush = stringRedisTemplate.opsForList().rightPush(key, pivot ,value);
		 return rightPush;
	}
	
	//redis:list类型的set,set一个list    注意:插入成功则返回list的long类型的元素个数,如果不成功则返回-1
	public long setListAll(String key,List<String> values){
		Long rightPushAll = stringRedisTemplate.opsForList().rightPushAll(key, values);
		return rightPushAll;
	}
	
	//redis:list类型get,start = 0,end = -1,获取list的全部值
	public List<String> getList(String key,long start,long end){
		List<String> range = stringRedisTemplate.opsForList().range(key, start, end);
		return range;
	}
	
    /**
     * redis:list类型delete
     * rightPop:从右边逐个删除,返回删除的value,当全部删除完的时候返回null,同时这个list也将不存在
     */
	
	public String deleteList(String key){
		String rightPop = stringRedisTemplate.opsForList().rightPop(key);
		return rightPop;
	}
	
	
	//*************** Set 类型  ************************************************************************************************************
	//redis:Set类型set,连续添加多个值,返回添加的个数
	public long setSet(String key,String... values){
			Long add = stringRedisTemplate.opsForSet().add(key, values);
		return add;
	}
	
    /**
     * redis:Set类set,key集合中不在otherKeys的数据中,新建一个destKey的Set,并存放这些key中的非交集数据
     * 注意:Collection otherKeys为多个Set的keyName,也可以是一个,即String otherKeys方法
     * 注意:返回的long数据是key Set的不同数据条数
     */
	public Set<String> getSet_difference(String key,String otherkey){
		Set<String> difference = stringRedisTemplate.opsForSet().difference(key, otherkey);
		return difference;
	}
	
    /**
     * redis:Set类的get,比较两个Set的相同元素,并返回相同元素的结果集
     * 提示:intersectAndStore和differenceAndStore用法类似
     */
	public Set<String> getSet_intersect(String key,String otherkey){
		Set<String> intersect = stringRedisTemplate.opsForSet().intersect(otherkey, otherkey);
		return intersect;
	}
	
    //redis:Set类的get,获取key Set的全部结果集
	public Set<String> getSet_all(String key){
		Set<String> members = stringRedisTemplate.opsForSet().members(key);
		return members;
	}
	
    /**
     * redis:Set类的get,两个Set的union,返回union之后的结果集
     * 注意:得到的Set已去重!!!!
     * 提示:unionAndStore和intersectAndStore这些的用法类似
     */
	public Set<String> getSet_union(String key,String otherkey){
		Set<String> union = stringRedisTemplate.opsForSet().union(key, otherkey);
		return union;
	}
	
    // redis:Set的pop,无序集合,随机删除key Set的元素。
	public String popSet(String key){
		String pop = stringRedisTemplate.opsForSet().pop(key);
		return pop;
	}
	
	//redis:Set的remove,删除key Set中指定的value,删除成功返回删除了的条数,删除失败则返回0
	public Long removeSet(String key,Object... values){
		Long remove = stringRedisTemplate.opsForSet().remove(key, values);
		return remove;
	}
	
	
	//设置 key 的过期时间    MINUTES 设置几分钟过期
	public boolean setTime(String key,Long outtime){
		boolean expire = stringRedisTemplate.expire(key, outtime, TimeUnit.MINUTES);
		return expire;
	}
	
}
第四步 Controller 控制层
package com.controller;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.service.RedisTest;

@Controller
public class DeptController {
	
	@Resource
	private RedisTest redistest;
	
	//向 String 类型中添加数据
	@RequestMapping("/SetString")
	@ResponseBody
	public String setString(Model model){
		System.err.println("请求成功");
		redistest.setString("name", "张三");
		redistest.setString("sex", "男");
		redistest.setString("age", "21");
		return "success";
	}
	
	//获取 String 类型的数据
	@RequestMapping("/GetString")
	@ResponseBody
	public String getString(String key){
		return redistest.getString(key);
	}
	
}
第 五步启动 springboot 整合  Redis 项目
package com.AppMain;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan(basePackages={
		"com.controller",
		"com.service"})
@EnableAutoConfiguration
public class AppMainRun {
	
	public static void main(String[] args) {
		//启动 springboot 项目
		SpringApplication.run(AppMainRun.class, args);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值