The Java Code section references: http://www.runoob.com/redis/redis-java.html
Using Redis in Java requires the introduction of Redis drivers, Is Jedis-2.1.0.jar, and this jar is dependent on Commons-pool-1.6.jar and tomcat-redis-session-manager-1.2-tomcat-7.jar, so we need at least this three jar package.
First step: Start the local Redis service;
Step two: Connect to the local Redis service and set the values for easy access in Java code. You can actually set the value in Java code and then get it, in order to intuitively feel, here directly in the command
At the prompt, set the value of Redis and get the value of the set value in Java by setting the key.
Step three: Get the values set in Redis in Java code.
Then follow these three steps,
1 start the local Redis service,
It should be fortunate to see this figure, as we have successfully launched the local Redis service.
2. Connect to a local Redis service
Here a value of "thename" is set, the value is: "Weiyongle";
3. In the Java code, get the value set in Redis.
1 PackageCom.redis;2 3 Importorg.junit.Test;4 5 ImportRedis.clients.jedis.Jedis;6 7 /**8 * Redis combined with a small demo of Java9 * Turn from:http://www.cnblogs.com/liuhongfeng/p/5033559.html, also visible beginner tutorial:Ten * http://www.runoob.com/redis/redis-java.html One * Three Java since Redis: A * Commons-pool-1.6.jar - Jedis-2.1.0.jar - Tomcat-redis-session-manager-1.2-tomcat-7.jar the * @authorWei - * @time October 26, 2016 PM 2:35:02 - */ - Public classRedisjava { + Public Static voidMain (string[] args) { - //Connect to a local Redis service +Jedis Jedis =NewJedis ("localhost"); ASYSTEM.OUT.PRINTLN ("Connect the local Redis service successfully! "); at //See if the service is running -SYSTEM.OUT.PRINTLN ("Service is running:" +jedis.ping ()); - } - @Test - //Redis Java String (string) instance - Public voidtestredisstring () { in //Connect to a local Redis service -Jedis Jedis =NewJedis ("localhost"); toSystem.out.println ("Connection to Server sucessfully"); + //setting up Redis string Data - //jedis.set ("Runoobkey", "Redis Tutorial"); the // //get the stored data and output * //System.out.println ("Stored string in Redis::" + jedis.get ("Runoobkey")); $String name = Jedis.get ("Thename");Panax NotoginsengSystem.out.println ("Stored thename in Redis::" +name); - } the + A}
Here we execute the testredisstring () This method, the console output:
Connection to Server sucessfully
Stored thename in Redis:: Weiyongle,
:
Description
Extended:
Set up list in Java and get the list of settings as a command prompt in Redis, directly:
Getting Started with Redis demo (Java)