SpringMvc整合redis

1、在pom.xml中增加以下节点

        
            org.springframework.data
            spring-data-redis
            1.7.4.RELEASE
        

        
            redis.clients
            jedis
            2.9.0
        
        

2、配置redis相关参数

     
        
         
        
         
        
        
         
         
        
          
     

     
	
         
        
         
        
         
        
         
      	
         
     
	
       
           
       	
           
               
           
           
           
               
           
        
             
                
          
             
           
                
         
      

3、在spring配置文件中导入redis配置文件

 

4、在web.xml文件加载spring配置文件

        
		org.springframework.web.context.ContextLoaderListener
	
	
		contextConfigLocation
		
			classpath:applicationContext.xml
		
	
	
		dispatcherServlet
		com.ctjy.wxmis.http.servlet.CustomerDispatcherServlet
		
			contextConfigLocation
			
				classpath:mvc.xml,classpath:dubbo.xml,classpath:mq.xml
			
		
		
			forward
			true
		
		1
	
	
		dispatcherServlet
		/
	


5、java代码中调用redis模版(下面代码只演示key-value类型,根据需要调整)

@Controller
@RequestMapping("/redis")
public class RedisController {
	@Autowired
	private  RedisTemplate redisTemplate;
	
	@ResponseBody
	@RequestMapping("/redis")
	public String redis() {
		ValueOperations value = redisTemplate.opsForValue();
		try {
			value.set("myredis", "myredis");
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		System.out.println(value.get("myredis"));
		return "test redis";
	}

}


你可能感兴趣的:(redis,java)