Redis搭建:Maven+Spring+SpringMVC+Redis-成都快上网建站

Redis搭建:Maven+Spring+SpringMVC+Redis

一、搭建redis环境 3.2.1
Redis gitHub 下载地址

下载之后直接解压得到以下目录结构
Redis搭建:Maven+Spring+SpringMVC+Redis
点击redis-server.exe即可启动Redis数据库
Redis搭建:Maven+Spring+SpringMVC+Redis

主要从事网页设计、PC网站建设(电脑版网站建设)、wap网站建设(手机版网站建设)、成都响应式网站建设公司、程序开发、微网站、小程序定制开发等,凭借多年来在互联网的打拼,我们在互联网网站建设行业积累了丰富的成都网站设计、成都做网站、网络营销经验,集策划、开发、设计、营销、管理等多方位专业化运作于一体,具备承接不同规模与类型的建设项目的能力。

输出信息:启动成功、端口号、pid 即启动成功。

二、搭建开发环境

1>搭建springmvc支持


     
	    org.springframework 
	    spring-webmvc 
	    4.3.4.RELEASE 
     
	     
	    javax.servlet 
	    javax.servlet-api 
	    3.1.0 
     
     
	    com.fasterxml.jackson.core 
	    jackson-core 
	    2.8.4 
     
     
	    com.fasterxml.jackson.core 
	    jackson-databind 
	    2.8.4 
    

2>集成Redis

 
	 
		org.springframework.data 
		spring-data-redis 
		1.7.5.RELEASE 
	 
	 
	 
		redis.clients 
		jedis 
		2.9.0 
	

3>配置Redis:spring-redis.xml




	 
	 
	 
	 
		 
		 
		 
		 
	 
	 
	 
		 
		 
		 
		 
	 
	 
	 
		 
	


4>配置redis.properties

redis.pool.maxTotal=105 
redis.pool.maxIdle=10 
redis.pool.maxWaitMillis=5000 
redis.pool.testOnBorrow=true 
redis.ip=127.0.0.1 
redis.port=6379

三、配置Servlet

此处不在web.xml中配置,采取硬编码方式

1.DispatchServlet拦截器

package com.redis.init;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

/**
 * 配置拦截器
 * @author hc
 *
 */
public class DispatcherServletInit extends AbstractAnnotationConfigDispatcherServletInitializer{

	/**
	 * 配置应用上下文
	 */
	@Override
	protected Class[] getRootConfigClasses() {
		return new Class[]{RootConfig.class};
	}

	/**
	 * 配置web上下文
	 */
	@Override
	protected Class[] getServletConfigClasses() {
		return new Class[]{WebConfig.class};
	}

	@Override
	protected String[] getServletMappings() {
		return new String[]{"/"};
	}

}

2.应用上下文WebConfig

package com.redis.init;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

/**
 * 应用上下文
 * @author hc
 *
 */
@Configuration
@ComponentScan(basePackages={"com.redit"},excludeFilters={
		@Filter(type=FilterType.ANNOTATION,value=Controller.class),
		@Filter(type=FilterType.ANNOTATION,value=EnableWebMvc.class)
})
@ImportResource("classpath:spring-*.xml")//引入redis配置文件
public class RootConfig {

}

3.web上下文

package com.redis.init;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan({"com.web"})
public class WebConfig extends WebMvcConfigurerAdapter{

	/**
	 * 配置视图解析器
	 * @return
	 */
	@Bean
	public ViewResolver viewResolver(){
		InternalResourceViewResolver resolver = new InternalResourceViewResolver();
		resolver.setPrefix("/WEB-INF/views/");
		resolver.setSuffix(".html");
		resolver.setExposePathVariables(true);
		return resolver;
	}
	
	/**
	 * 配置静态资源管理器
	 */
	@Override
	public void configureDefaultServletHandling(
			DefaultServletHandlerConfigurer configurer) {
		super.configureDefaultServletHandling(configurer);
		configurer.enable();
	}
}

4.Controller

package com.redis.web;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.redis.core.ListOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("user")
public class UserController {

	@Resource(name="redisTemplate")
	private ListOperations listUser;
	
	@RequestMapping("/list")
	@ResponseBody
	public List list(){
		List list=listUser.range("user", 0, -1);
		return list;
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public void add(String... user){
		listUser.leftPush("user", user);
	}
}

基本配置完毕,待测试

四、使用Jedis客户端来测试

1.启动redis服务器


		redis.clients
		jedis
		2.7.2
	
	
		org.springframework
		spring-test
		2.5
		test
	
	
      junit
      junit
      4.4
      test
    

junit尽量用高版本的,不然测试的时候会有版本冲突

2.配置参数


	 
		 
		 
		 
		 
		 
	 
	 
		 
		 
		 
	

3.测试

package redis_demo;


import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4Cla***unner;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * 使用Jedis来测试redis
 * @author hc
 *
 */
@RunWith(SpringJUnit4Cla***unner.class)
@ContextConfiguration(locations="classpath:spring-redis.xml")
public class RedisTestByJedis extends AbstractJUnit4SpringContextTests{

	@Autowired
	private JedisPool jedisPool;
	
	@Test
	public void basicTest(){
		Jedis jedis = jedisPool.getResource();
		//存值
		jedis.set("user.name", "aaa");
		jedis.set("user.pass", "123");
		
		//取值
		String name = jedis.get("user.name");
		String pass = jedis.get("user.pass");
		//断言
		Assert.assertEquals("aaa", name);
		//Assert.assertEquals("1234", pass);//错误
		Assert.assertEquals("123", pass);
		
		jedis.del("user.name");
		Boolean result = jedis.exists("user.name");
		Assert.assertEquals(false, result);
		
		result = jedis.exists("user.pass");
		Assert.assertEquals(true, result);
		
		jedis.close();
	}
}

显示通过测试


当前名称:Redis搭建:Maven+Spring+SpringMVC+Redis
文章地址:http://kswjz.com/article/jsgjjg.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流