Ⅰ、准备工作
Redis不仅是使用命令来操作,现在基本主流的语言都有客户端代码支持,比如Java、C、C#等等。在官网上也列出一些Java的客户端,比如Jedis、Redisson、JRedis等等。官网推荐使用Jedis、Redisson,企业中常用Jedis,下面案例中也使用Jedis。
导入jar包
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
Ⅱ、测试连接
/*测试是否连通。*/
@Test
public void test1(){
//ip和端口号
Jedis jedis = new Jedis("10.********0",6379);
//密码
jedis.auth("******");
System.out.println(jedis.ping());//PING
//退出连接
jedis.quit();
}
Ⅲ、存取数据
1、如果是String、List(有序不唯一集合)、Set(无序唯一集合)、Hash(键值对)、Zset(有序唯一集合)直接使用命令对应的方法即可(名字都是一样的,见名知意)
2、如果存储的是对象类型的数据
①对象转Json格式的字符串
②序列化反与反序列化
……
一、对象 - - Json字符串
不怎么使用,因为使用redis的目的就是为了快,强化客户的体验(与内存打交道),但是这样的对象转字符串,字符串转对象显然耽搁时间,与初衷相悖。
1、依赖fastjson
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
2、存取操作
/*向redis存放数据*/
@Test
public void test2(){
//一般是从数据库取数据,这里为了模拟,直接给个数据即可
Student student = new Student();
student.setSname("张三");
student.setSage(17);
Book book = new Book();
book.setBname("语文");
book.setNumber("120");
student.setBook(book);
//转为json格式的字符串
String studentString = JSON.toJSONString(student);
//以键值对的形式存放在redis数据库
jedis.set("student",studentString);
}
/*取出redis指定key的值*/
@Test
public void test3(){
//从redis数据库中获取,键值为student的value
String studentString = jedis.get("student");
//使用JSON工具的方法,将json格式的字符串转为对象
Student student = JSON.parseObject(studentString,Student.class);
System.out.println(student);
}
二、序列化与反序列化
序列化存储速度快
什么是序列化?
转为字节数组01010101…
1、自定义工具:序列化、反序列化
MySerilizableUtil.java
package com.hbw.util;
import java.io.*;
public class MySerilizableUtil {
//序列化
/*将任意对象转换为字节数组*/
public static byte[] serialize(Object obj){
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null;
byte[] bytes = null;
try {
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
bytes = baos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return bytes;
}
//反序列化
/*将字节数组转为任意对象*/
public static Object deserialization(byte[] bytes){
Object obj = null;
ObjectInputStream ois = null;
ByteArrayInputStream bais = null;
try {
bais = new ByteArrayInputStream(bytes);
ois = new ObjectInputStream(bais);
obj = ois.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return obj;
}
}
2、测试序列化与反序列化
//序列化、反序列化
@Test
public void test4(){
Student student = new Student();
student.setSname("张三");
student.setSage(17);
Book book = new Book();
book.setBname("语文");
book.setNumber("120");
student.setBook(book);
//序列化,存储
jedis.set(MySerilizableUtil.serialize("studentSer"),MySerilizableUtil.serialize(student));
//反序列化,获取
byte[] bytes = jedis.get(MySerilizableUtil.serialize("studentSer"));
Student stduentDeser = (Student) MySerilizableUtil.deserialization(bytes);
System.out.println(stduentDeser);
}
Ⅳ、spring整合Redis
1、引入spring的jar包(当然Jedis必不可少啦,见最上面)
<!--spring整合Redis-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.7.RELEASE</version>
</dependency>
2、创建spring配置文件
将对象交给spring核心容器管理即可
applicationContext.xmml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--设置连接池最大连接数、最小空闲连接数等,默认8个连接-->
<bean id="poolConfig" class="org.apache.commons.pool2.impl.GenericObjectPoolConfig">
<property name="maxTotal" value="10"></property>
<property name="maxIdle" value="5"></property>
<property name="minIdle" value="1"></property>
</bean>
<!--使用连接池获取连接-->
<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
<!--JedisPoolm没有提供setter方法,但是提供的构造方法-->
<constructor-arg name="poolConfig" ref="poolConfig"></constructor-arg>
<constructor-arg name="host" value="10.10.43.110"></constructor-arg>
<constructor-arg name="port" value="6379"></constructor-arg>
<!--超时时间-->
<constructor-arg name="timeout" value="2000"></constructor-arg>
<constructor-arg name="password" value="123456"></constructor-arg>
</bean>
</beans>
3、测试
@Test
public void test6(){
//解析spring配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取连接池
JedisPool jedisPool = (JedisPool) applicationContext.getBean("jedisPool");
//得到一个连接
Jedis jedis = jedisPool.getResource();
//得到连接了,想怎么操作就怎么操作
System.out.println(jedis.ping());
Map<String,String> map = new HashMap<>();
map.put("abc","efg");
jedis.hmset("name2",map);
jedis.quit();
}