设为首页 加入收藏

TOP

redis增删改查之Spring+redis代码实例
2017-11-15 09:15:26 】 浏览:84
Tags:redis 删改 查之 Spring 代码 实例

redis增删改查之Spring+redis代码实例

java 文件

package ******.redis;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * Created by asdas 2017/11/9.
 * for test
 */
public class TestRedis {


    private static RedisTemplate redisTemplate;
    private static ListOperations hellolistops;

    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"applicationContext-redis.xml"});
        //从Spring容器中根据bean的id取出我们要使用的userService对象
        redisTemplate = (RedisTemplate) ac.getBean("redisTemplate");
        hellolistops = redisTemplate.opsForList();
        TestRedis tr = new TestRedis();

        tr.addHello("test1", "first");
        tr.addHello("test2", "second");
        tr.addHello("test3", "third");

        tr.delHello("test3");

        tr.modHello("test2", "di er ge");

        System.out.println(tr.getHello("test1"));
        System.out.println(tr.getHello("test2"));
        System.out.println(tr.getHello("test3"));
    }

//////////增
    public void addHello(String id, String hello) {
         hellolistops.leftPush(id, hello);
        //System.out.println(l);
    }
/////////删
    public void delHello(String id) {
        redisTemplate.delete(id);
    }
/////////改
    public void modHello(String id, String hello_mod) {
        redisTemplate.delete(id);
        hellolistops.leftPush(id, hello_mod);
    }
////////查
    public String getHello(String id) {
        return hellolistops.range(id, 0, -1).toString();
    }
}

pom文件


  
	
   
    4.0.0
   

	
   
    com.hundsun.account
   
	
   
    hs-rt-account
   
	
   
    0.0.1-SNAPSHOT
   
	
   
    jar
   

	
   
    hs-rt-account
   

	
    
    
     UTF-8
     
    
     1.7
     
   

	
    
     
     
      junit
      
     
      junit
      
     
      4.8.1
      
     
     
     
     
      org.springframework.data
      
     
      spring-data-redis
      
     
      1.8.8.RELEASE
      
     
     
     
      redis.clients
      
     
      jedis
      
     
      2.9.0
      
     
     
     
      commons-pool
      
     
      commons-pool
      
     
      1.6
      
     
   
	

  

spring配置文件 applicationContext-redis.xml


  

  

	
    
   
	
   
	
    
     
      
       
        
       
       
       
        
       
       
       
        
       
       
       
        
       
       
      
    
   
  
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Docker 容器的常规用法详情 下一篇SSM框架之MyBatis入门讲解

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目