前言:此次省略redis的安装,在本地搭建好redis,ip 127.0.0.1,端口为6379
先写工具类:
package com.dada.ssm.SSM;
import java.util.HashSet;
import java.util.Set;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool;
/**
* jedis工具类
* @author lidaxing
* @date 2019-3-12
*/
public class JedisPoolDemo {
private static volatile JedisPool jedisPool = null;
private JedisPoolDemo() {}
//哨兵设置(未实现)
//private static JedisSentinelPool jedisSentinelPool = null;
public static JedisPool getJedisPoolInstance() {
if(null==jedisPool) {
synchronized(JedisPoolDemo.class) {
if(null == jedisPool) {
JedisPoolConfig poolConfig = new JedisPoolConfig();
/*
* 可用连接实例的最大数目,默认值为8;
* 如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
*/
poolConfig.setMaxTotal(1024);
//控制一个pool最多有多少个状态为idle(空闲的)的jedis实例
poolConfig.setMaxIdle(12);
//等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时
poolConfig.setMaxWaitMillis(10*1000);
poolConfig.setTestOnBorrow(true);
jedisPool = new JedisPool(poolConfig,"127.0.0.1",6379);
//哨兵设置...
/*
* Set<String> sentinels = new HashSet<String>() {
private static final long serialVersionUID = -3520920635510412404L;
{
add(SENTINEL_1);
add(SENTINEL_2);
add(SENTINEL_3);
}
};
if (0 == USE_REDIS_PWD_FLAG) {
// 如果密码为空或者没有设置这个配置,则调用不使用密码的方式创建jedis连接池
if ("SingleHost".equals(REDIS_DEPLOY)) {
jedisPool = new JedisPool(config, ADDR_ARRAY, PORT, TIMEOUT);
} else {
jedisSentinelPool = new JedisSentinelPool(MASTER_NAME, sentinels, config, TIMEOUT);
}
} else {
// 如果密码不为空,则调用使用密码的方式创建jedis连接池
if ("SingleHost".equals(REDIS_DEPLOY)) {
jedisPool = new JedisPool(config, ADDR_ARRAY, PORT, TIMEOUT, REDIS_PASSWORD);
} else {
jedisSentinelPool = new JedisSentinelPool(MASTER_NAME, sentinels, config, TIMEOUT, REDIS_AUTH);
}
}
*/
}
}
}
return jedisPool;
}
/**
* 多线程环境
* 获取jedis实例
* (可省略这个方法,上面连接池也可定义)
* @return
*/
public static synchronized Jedis getJedis() {
Jedis jedis = null;
try {
if (jedisPool != null) {
jedis = jedisPool.getResource();
}
} catch (Exception e) {
throw new IllegalStateException(e);
}
return jedis;
}
/**
* 释放资源
* @param jedisPool
* @param jedis
*/
@SuppressWarnings("deprecation")
public static void release(JedisPool jedisPool,Jedis jedis) {
if(null!=jedis) {
jedisPool.returnResourceObject(jedis);
}
}
}
//测试代码
测试准备
建Person实体类
package com.dada.ssm.SSM;
import java.io.Serializable;
/**
* person类
* @author lidaxing
* @date 2019-3-12
*/
public class Person implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
private Integer age;
private Integer sex;
public Person() {
super();
}
public Person(Integer id, String name, Integer age, Integer sex) {
super();
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + "]";
}
}
//测试
package com.dada.ssm.SSM;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.junit.Test;
import org.springframework.util.StringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
/**
* 测试方法
* @author lidaxing
* @date 2019-3-12
*/
public class TestRedis {
@Test
public void setPerson() {
Person person1 = new Person(1, "dada", 32, 1);
Person person2 = new Person(2, "xixi", 29, 2);
JedisPool jedisPool = null;
Jedis jedis = null;
List<Person> list = new ArrayList<>();
list.add(person1);
list.add(person2);
try {
jedisPool = JedisPoolDemo.getJedisPoolInstance();
jedis = jedisPool.getResource();
String persons = jedis.get("pKey");
if (StringUtils.isEmpty(persons)) {
//对象==>转 json
jedis.set("pKey", JSON.toJSONString(list, SerializerFeature.WriteMapNullValue));
System.out.println("set值到redis");
} else {
System.out.println("redis已经有值!不set了!");
}
} catch (Exception e) {
JedisPoolDemo.release(jedisPool, jedis);
e.printStackTrace();
}
}
@Test
public void getPerson() {
JedisPool jedisPool = null;
Jedis jedis = null;
List<Person> list = null;
try {
jedisPool = JedisPoolDemo.getJedisPoolInstance();
jedis = jedisPool.getResource();
String listPerson = jedis.get("pKey");
if (StringUtils.isEmpty(listPerson)) {
System.out.println("获取person值失败");
} else {
//json==>转对象
list = JSON.parseArray(listPerson, Person.class);
System.out.println("list=====>:" + list.toString());
}
} catch (Exception e) {
JedisPoolDemo.release(jedisPool, jedis);
e.printStackTrace();
System.out.println("获取值失败!");
}
}
//打印结果list=====>:[Person [id=1, name=dada, age=32, sex=1], Person [id=2, name=xixi, age=29, sex=2]]
}
//查看redis数据库
//只是实现简单的set值和查询,其他后续跟进。