SSM整合之redis设置值和获取值三种方式
准备工作(导入一些必要的依赖)
<dependency>
<groupId>org.springframework.datagroupId>
<artifactId>spring-data-redisartifactId>
<version>2.4.1version>
dependency>
<dependency>
<groupId>redis.clientsgroupId>
<artifactId>jedisartifactId>
<version>3.3.0version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.75version>
dependency>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
1.1 特点
默认的,必须实现序列化接口,可读性差(redis的可视化界面)
1.2 实例代码
1.2.1 实体类Emp类
package com.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import tk.mybatis.mapper.annotation.KeySql;
import javax.persistence.Id;
import java.io.Serializable;
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Emp implements Serializable{
@Id
@KeySql(useGeneratedKeys = true)
private Integer empno;
private String ename;
private String mgr;
private String job;
private String hiredate;
private Double sal;
private Double comm;
private Integer deptno;
private static final long serialVersionUID = 1L;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
1.2.2 创建RedisConfig类
package com.config;
import com.fasterxml.jackson.databind.ser.std.StringSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String,Object> redisTemplate(JedisConnectionFactory cf){
RedisTemplate<String, Object> redis = new RedisTemplate<>();
redis.setConnectionFactory(cf);
JdkSerializationRedisSerializer jdk = new JdkSerializationRedisSerializer();
redis.setKeySerializer(jdk);
redis.setValueSerializer(jdk);
return redis;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
1.2.3 注入RedisTemplate类
@Autowired
RedisTemplate redisTemplate;
1.2.4 测试代码
@Test
public void t(){
Emp emp = em.selectByPrimaryKey(7369);
redisTemplate.opsForValue().set("emp:7369",emp);
Emp e = (Emp) redisTemplate.opsForValue().get("emp:7369");
System.out.println("取出来的值: "+emp);
}
1.2.5 测试运行截图
a redis可视化工具展示
b 控制台打印结果展示
2.String序列化器
2.1 特点
String序列话器 存储数据必须是String类型
查询到的数据需要通过fastjson中的JSON.toJSONString设置进去
2.2 实例代码
2.2.1 实体类Emp类
package com.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import tk.mybatis.mapper.annotation.KeySql;
import javax.persistence.Id;
import java.io.Serializable;
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Emp{
@Id
@KeySql(useGeneratedKeys = true)
private Integer empno;
private String ename;
private String mgr;
private String job;
private String hiredate;
private Double sal;
private Double comm;
private Integer deptno;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
2.2.2 创建RedisConfig类
package com.config;
import com.fasterxml.jackson.databind.ser.std.StringSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String,Object> redisTemplate(JedisConnectionFactory cf){
RedisTemplate<String, Object> redis = new RedisTemplate<>();
redis.setConnectionFactory(cf);
StringRedisSerializer string=new StringRedisSerializer();
redis.setKeySerializer(string);
redis.setValueSerializer(string);
return redis;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
2.2.3 注入RedisTemplate类
@Autowired
RedisTemplate redisTemplate;
2.2.4 测试代码
@Test
public void t(){
Emp emp = em.selectByPrimaryKey(7369);
String s= JSON.toJSONString(emp);
redisTemplate.opsForValue().set("emp:7369",s);
String s1 = (String) redisTemplate.opsForValue().get("emp:7369");
Emp emp1 = JSON.parseObject(s1, Emp.class);
System.out.println("取出来的值: "+emp1);
}
2.2.5 测试运行截图
a redis可视化工具展示
b 控制台打印结果展示
3.GenericJackson2JsonRedisSerializer
3.1 特点
GenericJackson2JsonRedisSerializer不需要序列化 可以存储任意对象 自动转换
3.2 实例代码
3.2.1 实体类Emp类
package com.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import tk.mybatis.mapper.annotation.KeySql;
import javax.persistence.Id;
import java.io.Serializable;
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Emp{
@Id
@KeySql(useGeneratedKeys = true)
private Integer empno;
private String ename;
private String mgr;
private String job;
private String hiredate;
private Double sal;
private Double comm;
private Integer deptno;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
3.2.2 创建RedisConfig类
package com.config;
import com.fasterxml.jackson.databind.ser.std.StringSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String,Object> redisTemplate(JedisConnectionFactory cf){
RedisTemplate<String, Object> redis = new RedisTemplate<>();
redis.setConnectionFactory(cf);
GenericJackson2JsonRedisSerializer gjj=new GenericJackson2JsonRedisSerializer();
redis.setKeySerializer(gjj);
redis.setValueSerializer(gjj);
return redis;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
3.2.3 注入RedisTemplate类
@Autowired
RedisTemplate redisTemplate;
3.2.4 测试代码
@Test
public void t(){
Emp emp = em.selectByPrimaryKey(7369);
redisTemplate.opsForValue().set("emp:7369",emp);
Emp emp1 = (Emp) redisTemplate.opsForValue().get("emp:7369");
System.out.println("取出来的值: "+emp1);
}
3.2.5 测试运行截图
a redis可视化工具展示
b 控制台打印结果展示