First, to build the Redis environment 3.2.1
Redis GitHub
after download, extract the following directory structure directly
650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M01/8D/BB/wKiom1ioSPTRugkPAACi-NO7_Jw034.jpg-wh_500x0-wm_ 3-wmp_4-s_1323255870.jpg "title=" 36020170218211445907.jpg "alt=" Wkiom1iosptrugkpaaci-no7_jw034.jpg-wh_50 "/>
Click Redis-server.exe to launch the Redis database
650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M02/8D/BB/wKiom1ioSTvyuh7UAADWNJs5kt0423.jpg-wh_500x0-wm_ 3-wmp_4-s_821966801.jpg "title=" Redis server.jpg "alt=" Wkiom1iostvyuh7uaadwnjs5kt0423.jpg-wh_50 "/>
Output information: Startup success, port number, PID is started successfully.
Second, build the development environment
1> Build SPRINGMVC Support
<!-- Build springmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId> Spring-webmvc</artifactid> <version>4.3.4.release</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId> javax.servlet-api</artifactid> <version>3.1.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> < Artifactid>jackson-core</artifactid> <version>2.8.4</version > </depEndency> <dependency> <groupid> com.fasterxml.jackson.core</groupid> <artifactid>jackson-databind< /artifactid> <version>2.8.4</version> </dependency>
2> Integrated Redis
<!--integrated Redis--><dependency> <groupId>org.springframework.data</groupId> <artifactid >spring-data-redis</artifactId> <version>1.7.5.RELEASE</version> </dependency> <!-- Redis client-<dependency> <groupId>redis.clients</groupId> <artifactid>jedis</ Artifactid> <version>2.9.0</version> </dependency>
3> Configuration Redis:spring-redis.xml
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "http://www.springframework.org/ Schema/beans "xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "xmlns:context="/HTTP/ Www.springframework.org/schema/context "xsi:schemalocation=" http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd "><!-- Introducing Redis configuration -- > <context:property-placeholder location= "Classpath:redis.properties" ignore-unresolvable = "true"/> <!-- Redis configuration --> <bean id= "Jedispoolconfig" class= " Redis.clients.jedis.JedisPoolConfig "> <property name=" Maxtotal " value=" ${ Redis.pool.maxTotal} " /> <property name=" Maxidle " value=" ${redis.pool.maxidle} " /> <property name= "Maxwaitmillis" valuE= "${redis.pool.maxwaitmillis}" /> <property name= "Testonborrow" value= "${ Redis.pool.testOnBorrow} " /> </bean> <!-- redis Single-node database connection configuration --> <bean id= "Jedisconnectionfactory" class= " Org.springframework.data.redis.connection.jedis.JedisConnectionFactory "> <property name=" HostName " value=" ${redis.ip} " /> <property name=" Port " value=" ${redis.port} " /> <!-- <property name= "password" value= "${redis.pass}" /> -- > <property name= "Poolconfig" ref= "Jedispoolconfig" /> </bean> <!-- redistemplate configuration, redistemplate is an extension of Jedis to Redis operations, with more operations and encapsulation to make operations easier --> <bean Id= "Redistemplate" class= "Org.springframework.data.redis.core.RedisTemplate" > <property Name= "ConnectionFactory" ref= "Jedisconnectionfactory" /> </bean></beans>
4> Configuration 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
Third, configure the servlet
Not configured here in Web. XML, hard-coded
1.DispatchServlet Interceptor
Package Com.redis.init;import org.springframework.web.servlet.support.abstractannotationconfigdispatcherservletinitializer;/** * Configuring Interceptors * @author HC * */public class Dispatcherservletinit extends abstractannotationconfigdispatcherservletinitializer{/** * Configure app Context */ @Overrideprotected class<?>[] Getrootconfigclasses () {return new class<?>[]{rootconfig.class}; /** * Configure Web Context */@Overrideprotected class<?>[] getservletconfigclasses () {return new class<?>[]{ Webconfig.class};} @Overrideprotected string[] Getservletmappings () {return new string[]{"/"};}}
2. Application Context 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;/** * Application context * @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 ")//introduction of Redis Profile public class RootConfig {}
3.web context
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{/** * Configuration View Resolver * @return */@Beanpublic viewresolver viewresolver () {Internalresourceviewresolver resolver = new internalresourceviewresolver (); Resolver.setprefix ("/WEB-INF/views/"); Resolver.setsuffix (". html"); ResolVer.setexposepathvariables (true); return resolver;} /** * Configuring static resource Manager */@Overridepublic 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<string, string[]> listuser;@ Requestmapping ("/list") @ResponseBodypublic list<string[]> list () {list<string[]> List=listuser.range (" User ", 0,-1); return list;} @RequestMapping ("/add") @ResponseBodypublic void Add (String ... user) {Listuser.leftpush ("user", user);}}
Basic configuration complete, pending test
Iv. using the Jedis client to test
1. Start Redis Server
<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId>< version>2.7.2</version></dependency><dependency><groupid>org.springframework</ Groupid><artifactid>spring-test</artifactid><version>2.5</version><scope>test </scope></dependency><dependency> <groupId>junit</groupId> <artifactid>junit </artifactId> <version>4.4</version> <scope>test</scope> </dependency>
JUnit try to use the higher version, or there will be a version conflict when testing
2. Configuration parameters
<!--configuring Jedis linked server Parameters--><bean id= "Poolconfig" class= "Redis.clients.jedis.JedisPoolConfig" > <property Name= "Maxtotal" value= "4096"/> <property name= "Maxidle" value= "$"/> <property name= "MaxWaitMillis" Value= "/> <property name=" Testonborrow "value=" true "/> <property name=" Testonreturn "value=" true "/ > </bean> <bean id= "Jedispool" class= "Redis.clients.jedis.JedisPool" > <constructor-arg index= "0" ref= "Poolconfig"/> <constructor-arg index= "1" value= "127.0.0.1"/> <constructor-arg index= "2" value= "6379 "type=" int "/> </bean>
3. Testing
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.springjunit4classrunner;import redis.clients.jedis.jedis;import redis.clients.jedis.JedisPool;/** * use Jedis to test redis * @author hc * */ @RunWith (Springjunit4classrunner.class) @ContextConfiguration (locations= "Classpath:spring-redis.xml") public class redistestbyjedis extends abstractjunit4springcontexttests{@Autowiredprivate jedispool jedispool; @Testpublic void basictest () {jedis jedis = Jedispool.getresource ();///Value Jedis.set ("User.Name", "AAA") Jedis.set ("User.pass", "123");//value string name =&nbSp;jedis.get ("User.Name"); String pass = jedis.get ("User.pass");//Assert Assert.assertequals ("AAA", name);// Assert.assertequals ("1234", pass);//Error 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 ();}}
Show pass Test
This article is from the "It Rookie" blog, make sure to keep this source http://mazongfei.blog.51cto.com/3174958/1899127
Redis Build: Maven+spring+springmvc+redis