日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Redis搭建:Maven+Spring+SpringMVC+Redis

一、搭建redis環(huán)境 3.2.1
Redis gitHub 下載地址

下載之后直接解壓得到以下目錄結(jié)構(gòu)
Redis搭建:Maven+Spring+SpringMVC+Redis
點(diǎn)擊redis-server.exe即可啟動(dòng)Redis數(shù)據(jù)庫(kù)
Redis搭建:Maven+Spring+SpringMVC+Redis

主要從事網(wǎng)頁(yè)設(shè)計(jì)、PC網(wǎng)站建設(shè)(電腦版網(wǎng)站建設(shè))、wap網(wǎng)站建設(shè)(手機(jī)版網(wǎng)站建設(shè))、成都響應(yīng)式網(wǎng)站建設(shè)公司、程序開(kāi)發(fā)、微網(wǎng)站、小程序定制開(kāi)發(fā)等,憑借多年來(lái)在互聯(lián)網(wǎng)的打拼,我們?cè)诨ヂ?lián)網(wǎng)網(wǎng)站建設(shè)行業(yè)積累了豐富的成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、網(wǎng)絡(luò)營(yíng)銷經(jīng)驗(yàn),集策劃、開(kāi)發(fā)、設(shè)計(jì)、營(yíng)銷、管理等多方位專業(yè)化運(yùn)作于一體,具備承接不同規(guī)模與類型的建設(shè)項(xiàng)目的能力。

輸出信息:?jiǎn)?dòng)成功、端口號(hào)、pid 即啟動(dòng)成功。

二、搭建開(kāi)發(fā)環(huán)境

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{

	/**
	 * 配置應(yīng)用上下文
	 */
	@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.應(yīng)用上下文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;

/**
 * 應(yīng)用上下文
 * @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;
	}
	
	/**
	 * 配置靜態(tài)資源管理器
	 */
	@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);
	}
}

基本配置完畢,待測(cè)試

四、使用Jedis客戶端來(lái)測(cè)試

1.啟動(dòng)redis服務(wù)器


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

junit盡量用高版本的,不然測(cè)試的時(shí)候會(huì)有版本沖突

2.配置參數(shù)


	 
		 
		 
		 
		 
		 
	 
	 
		 
		 
		 
	

3.測(cè)試

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來(lái)測(cè)試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);//錯(cuò)誤
		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();
	}
}

顯示通過(guò)測(cè)試


文章題目:Redis搭建:Maven+Spring+SpringMVC+Redis
當(dāng)前URL:http://www.dlmjj.cn/article/jsgjjg.html