概述
1.pom.xml
<properties>
<java.version>1.8</java.version>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
</properties>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
2. JedisUtil.java
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.exceptions.JedisException;
public class JedisUtil {
private static final Long UNLOCK_SUCCESS = 1L;
private static JedisPool jedisPool = null;
static {
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
jedisPool = new JedisPool(config, "192.168.56.128", 6379, 6000, "666666");
}
public static Jedis getJedis() throws JedisException {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
} catch (JedisException e) {
e.printStackTrace();
throw e;
}
return jedis;
}
}
3.RedisDataTest.java
package com.test.demo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import redis.clients.jedis.Jedis;
public class RedisDataTest {
public static void main(String[] args) {
Jedis jedis = JedisUtil.getJedis();
//字符类型
jedis.set("username","dadd");
jedis.set("username","wqqe");
System.out.println(jedis.get("username"));
//list
String listName = "list";
jedis.del(listName);
for (int i = 0; i < 10; i++) {
jedis.lpush(listName, "a"+String.valueOf(i));
}
System.out.println(jedis.lrange(listName,0,10));
System.out.println(jedis.lrange(listName,5,7));
//hash
String userKey = "userxxx";
jedis.hset(userKey,"name","jack");
jedis.hset(userKey,"age","26");
jedis.hset(userKey,"phone","1333165465");
System.out.println(jedis.hget(userKey,"name"));
System.out.println(jedis.hgetAll(userKey));
//set
String likekey1 = "setkey1";
String likekey2 = "setkey2";
for (int i = 0; i < 10; i++) {
jedis.sadd(likekey1,String.valueOf(i));
jedis.sadd(likekey2,String.valueOf(i*i));
}
System.out.println(jedis.smembers(likekey1));
System.out.println(jedis.smembers(likekey2));
//zset
String rankKey = "zsetKey";
jedis.zadd(rankKey,15,"ad");
jedis.zadd(rankKey,60,"www");
jedis.zadd(rankKey,90,"bbb");
jedis.zadd(rankKey,75,"ttt");
jedis.zadd(rankKey,80,"tom");
System.out.println(jedis.zcard(rankKey)); //计数
System.out.println(jedis.zcount(rankKey,61,100));
System.out.println(jedis.zscore(rankKey,"tom"));
//初始化表数据
List<Map<String,Object>> tableList = new ArrayList<Map<String,Object>>();
Map<String,Object> row1 = new HashMap<String,Object>();
row1.put("keyId", 1);
row1.put("userCode", "admin");
row1.put("userName", "管理员");
tableList.add(row1);
Map<String,Object> row2 = new HashMap<String,Object>();
row2.put("keyId", 2);
row2.put("userCode", "test");
row2.put("userName", "测试");
tableList.add(row2);
//存储表数据到redis
String tableName = "t_userinfo";
String tableNameKey = tableName+"keyId";
jedis.del(tableNameKey);
String keyId = "";
for (int i = 0; i < tableList.size(); i++) {
Map<String,Object> row = tableList.get(i);
keyId = String.valueOf(row.get("keyId"));
//list存储主键
jedis.lpush(tableNameKey, String.valueOf(keyId));
//存储行数据
jedis.del(tableName + keyId);
jedis.hset(tableName + keyId,"keyId",keyId);
jedis.hset(tableName + keyId,"userCode",(String) row.get("userCode"));
jedis.hset(tableName + keyId,"userName",(String) row.get("userName"));
}
System.out.println(jedis.hgetAll(tableName+"1"));
System.out.println(jedis.hgetAll(tableName+"2"));
//从redis中读取表数据
List<String> keyList = jedis.lrange(tableNameKey, 0, jedis.llen("tableNameKey")+1);
if(null != keyList && keyList.size() > 0){
for (int i = 0; i < keyList.size(); i++) {
String key = keyList.get(i);
System.out.println(jedis.hgetAll(tableName+key));
}
}
}
}
最后
以上就是正直服饰为你收集整理的java操作redis数据类型的demo的全部内容,希望文章能够帮你解决java操作redis数据类型的demo所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复