一:JedisTemplate默认方式封装javaBean对象(序列化与反序列化)
方式二:修改jedisTemplate修改默认方式 使用jaskson操作javaBean对象
学习导航:
JedisTemplate简介:
今天我们使用新的技术叫做JedisTemplate,这个是spring提供用来操作数据库的模板对象,提供了对象Redis数据库的增删改查的操作,封装了Jedis,同时自身实现了序列化与反序列化的功能(默认)、将对象转化为json字符串进行保存
一:JedisTemplate默认方式封装javaBean对象(序列化与反序列化)
步骤一: 首先创建maven项目:
在这里我们要添加jedis依赖(因为底层提供了封装!但是咱们还是要用嘛是吧)
添加junit测试
添加JedisTemplate依赖
pom中的源码:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bjsxt.jedisTemplate</groupId>
<artifactId>jedisTemplateDemo1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.2</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.6.RELEASE</version>
</dependency>
</dependencies>
</project>
步骤二:添加实体类
package com.bjsxt.pojo;
import java.io.Serializable;
/**
* 用户实体类
* */
public class User implements Serializable{
private String name;
private String sex;
private Integer age;
public User() {
}
public User(String name, String sex, Integer age) {
super();
this.name = name;
this.sex = sex;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User [name=" + name + ", sex=" + sex + ", age=" + age + "]";
}
}
步骤三:因为需要使用JedisTemplate,所以我们需要使用spring容器创建对象。
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd
">
<!-- 连接工厂需要端口号和ip地址 -->
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="192.168.224.11"></property>
<property name="port" value="6379"></property>
</bean>
<!-- 使用redisTemplate访问redis操作对象 -->
<bean id="redisTemplate"
class="org.springframework.data.redis.core.RedisTemplate">
<!-- 访问redis数据库必须要先连接数据库,所以需要一个连接工厂 -->
<property name="connectionFactory" ref="connectionFactory"></property>
</bean>
</beans>
步骤四:测试:使用JedisTemple序列化反序列化操作javaBean对象
package com.bjsxt.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import com.bjsxt.pojo.User;
public class JedisTemplateTest {
@Test
public void jedisRemplate() {
//加载spring容器
ApplicationContext ac
=new ClassPathXmlApplicationContext("application.xml");
//创建redisTemplate对象
RedisTemplate<String, User> redisTemplate =
(RedisTemplate<String, User>)ac.getBean("redisTemplate");
User u=new User("lht", "男", 11);
//保存user对象 默认的方式二进制字节数组
redisTemplate.boundValueOps("user").set(u);
//取值
User user = redisTemplate.boundValueOps("user").get();
System.out.println(user);
}
}
方式二:修改jedisTemplate修改默认方式 使用jaskson操作javaBean对象
步骤一:在之前的基础上再添加一个jackson依赖
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bjsxt.jedisTemplate</groupId>
<artifactId>jedisTemplateDemo2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.2</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.6.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.0</version>
</dependency>
</dependencies>
</project>
步骤二:新建一个application.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
">
<bean id="connectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="192.168.224.11"></property>
<property name="port" value="6379"></property>
</bean>
<bean id="keySerializer"
class="org.springframework.data.redis.serializer.StringRedisSerializer">
</bean>
<bean id="valueSerializer"
class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer">
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory"></property>
<!--修改key的默认序列化 -->
<property name="keySerializer" ref="keySerializer"></property>
<!-- 修改value的默认序列化 -->
<property name="valueSerializer" ref="valueSerializer"></property>
</bean>
</beans>
步骤三:创建实体类 与之前一样
步骤四:测试:
package com.bjsxt.test;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import com.bjsxt.pojo.User;
public class JedisTemplateTest {
@Test
public void jedisTemplateT() {
ApplicationContext ac=new ClassPathXmlApplicationContext("application.xml");
RedisTemplate redisTemplate=(RedisTemplate)ac.getBean("redisTemplate");
User user=new User("a","女",11);
//设置存储时间,后面的TimeUnit是可以设置在数据库的存储时间的单位,中间是值
redisTemplate.boundValueOps("user11").set(user, 10, TimeUnit.MINUTES);
User user2=(User)redisTemplate.boundValueOps("user11").get();
System.out.println(user2);
}
}