在我的上一篇文章,已經介紹了spring整合redis方法介紹,這裏不在贅述,
spring整合redis參考網址http://blog.csdn.net/qq_28345313/article/details/76713827
下面給的大家介紹spring操作redis實現存儲和刪除數據
這裏有官方中文文檔http://redisdoc.com/ 建議多看一下
首先添加配置redis具體操作的實現方法在spring-redis.xml配置文件<beans>中
插入
<bean id="redisToolService" class="com.mx.service.Impl.RedisToolServiceImpl"/>
這個是操作redis數據的具體實現類RedisToolServiceImpl
這裏不貼代碼,多敲敲代碼吧,不要向我這麼懶
注意配置文件bean的id
package com.mx.service.Impl;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import com.mx.service.RedisToolService;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCommands;
/**
* redis操作具體實現方法
* */
@Service("redisToolService")
public class RedisToolServiceImpl implements RedisToolService {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Resource(name="redisTemplate")
private ListOperations<String, String> listOps;
@Resource(name="redisTemplate")
ValueOperations<String, String> value;
@Resource(name="redisTemplate")
ValueOperations<String, String> vop;
public void addLink(String name,String sex){
listOps.leftPush(name, sex);
redisTemplate.boundListOps(name).leftPush(sex);
}
//根據鍵值獲取存入的key-value數據
public String get(String name){
//String key=jedis.get(name);
//return key;
value= redisTemplate.opsForValue();
return value.get(name);
}
//添加key-value數據
public void set(String name,String sex){
//添加一個 key
value.set(name, sex);
}
//刪除key-value數據
public void delete(String key){
redisTemplate.delete(key);
}
}
創建實現接口RedisToolService
/**
* redis操作接口
* */
public interface RedisToolService {
public void addLink(String name,String sex);
public String get(String key);
public void set(String name,String sex);
public void delete(String key);
}
下面使用junit測試:
存入數據,前,redis數據庫如下:
(這裏不讓貼圖了。。)
現在執行存入操作sendRedis()方法:
@Test
public void sendRedis(){
//redisToolService.addLink("www", "555");
redisToolService.set("test2", "rrrrr");
}
執行後:(注意數據顯示的格式)
(這裏不讓貼圖了。。)
這樣就存入值了;
下面測試查詢getRedis:
(這裏不讓貼圖了。。)
接着測試刪除key的delete方法:
@Test
public void delete(){
redisToolService.delete("test2");
}
下面刷新查看數據庫結果:
(這裏不讓貼圖了。。)
已經沒有數據了
到此,spring整合操作redis數據已經完畢。
這裏貼出junit代碼,csdn上傳圖片有問題
package junit.test.v;
import java.util.List;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.data.redis.connection.RedisServer;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.mx.service.RedisToolService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:spring.xml","classpath:spring-mybatis.xml","classpath:spring-redis.xml"})
public class JunitTest {
@Resource
RedisToolService redisToolService;
@Test
public void sendRedis(){
//redisToolService.addLink("www", "555");
redisToolService.set("test2", "rrrrr");
}
@Test
public void getRedis(){
String key=redisToolService.get("test2");
System.out.println(key);
}
@Test
public void delete(){
redisToolService.delete("test2");
}
}