一:springboot整合redis

redis版本:3.0.0

运行环境:linux

1.安装redis

1.1安装gcc
yum install gcc-c++
1.2解压redis.3.0.0.tar.gz压缩包
tar -zxvf redis-3.0.0.tar.gz
1.3进入解压后的目录进行编译
cd redis-3.0.0
make
1.4将redis安装到指定目录
make PREFIX=/usr/local/redis install
1.5启动redis
./redis-server ctrl+c停止
1.6复制redis.conf到/usr/local/redis/bin/
cp redis.conf /usr/local/redis/bin/
1.7编辑复制的redis.conf
将daemonize no 改为daemonize yes
1.8启动redis服务(6379)
./redis-server redis.conf
ps aux|grep redis #查看redis是否启动
./redis-cli shutdown #关闭redis服务

2.springboot整合spring data redis

spring data redis是属于spring data下的一个模块.作用就是简化对于redis的操作.

2.1修改pom文件添加redis依赖

<!-- redis的springboot启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<!-- 1.5的版本默认采用的连接池技术是jedis 2.0以上版本默认连接池是lettuce, 在这里采用jedis,所以需要排除lettuce的jar -->
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 添加jedis客户端 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<!-- 将作为Redis对象序列化器 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>

2.2编写springdataredis的配置类

@Configuration
public class RedisConfig {
//1.创建JedisPoolConfig对象,在该对象中完成一些连接池配置
//2.创建JedisConnectionFactory对象,配置redis连接信息
//@ConfigurationProperties(prefix="spring.redis"):会将前缀相同的内容创建一个实体
//3.创建RedisTemplate:用于执行Redis操作方法
@Bean
public RedisTemplate<String, Object> getRedisTemplate(JedisConnectionFactory factory){
RedisTemplate<String, Object> template = new RedisTemplate<>();
//关联连接工厂
template.setConnectionFactory(factory);
//为key设置序列化
template.setKeySerializer(new StringRedisSerializer());
//为value设置序列化
template.setValueSerializer(new StringRedisSerializer());
return template;
}
}

2.3编写测试代码

2.3.1修改pom文件添加test依赖

<!-- test的springboot启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>

2.3.2编写测试类

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes=Application.class) //Application是springboot启动类的类名
public class RedisTest {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
//添加一个字符串
@Test
public void testSet() {
this.redisTemplate.opsForValue().set("key", "测试redis整合");
}
//获取一个字符串
@Test
public void testGet() {
String value = (String)this.redisTemplate.opsForValue().get("key");
System.out.println(value);
}
}

3.提取redis的配置信息

3.1在src/main/resource/目录下新建一个配置文件:application.yaml

@ConfigurationProperties(prefix="spring.redis"):会将前缀相同的内容创建一个实体

# redis配置
spring:
redis:
# redis数据库索引(默认为零)
database: 0
# host是自己虚拟机的ip地址
host: 192.168.91.100
port: 6379
password:
jedis:
pool:
# 连接池最大连接数(使用负值表示没有限制)
max-active: 8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1
# 连接池最大空闲连接
max-idle: 8
# 连接池最小空闲连接
min-idle: 0
# 连接超时时间(毫秒)
timeout: 10000

4.spring Date Redis操作实体对象

4.1创建实体类User

public class User implements Serializable{
private Integer id;
private String name;
private String age;
public User() {
// TODO Auto-generated constructor stub
}
public User(Integer id, String name, String age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
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 String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}

4.2测试代码

//添加user实体类
@Test
public void testSetUser() {
User user = new User();
user.setId(1);
user.setName("zlg");
user.setAge("18");
//重新设置序列化器
this.redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
this.redisTemplate.opsForValue().set("user", user);
}
//根据key获取实体类对象
@Test
public void testGetUser() {
//反序列化,重新设置序列化器
this.redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
User user = (User)this.redisTemplate.opsForValue().get("user");
System.out.println(user);
}

5.spring data redis以json格式存储实体对象

5.1测试代码

//3.基于json格式存user对象
@Test
public void testSetUserUseJson() {
User user = new User();
user.setId(1);
user.setName("zlg");
user.setAge("18");
//重新设置序列化器
this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(User.class));
this.redisTemplate.opsForValue().set("user_json", user);
}
//基于json格式取对象
@Test
public void testGetUserUseJson() {
//反序列化
this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(User.class));
User user = (User)this.redisTemplate.opsForValue().get("user_json");
System.out.println(user);
}

最新文章

  1. Atitit 基于dom的游戏引擎
  2. spring MVC @Resource不支持Lazy加载
  3. 在VS2010中使用Outlook工具栏
  4. codeforces A. Dima and Continuous Line 解题报告
  5. Mysql的一些小知识点
  6. Idiomatic Python手记一: average in FP way
  7. Script.NET Perl解释器代码已经在GitHub开源发布
  8. 【HDOJ】1059 Dividing
  9. python入门安装
  10. DEDECMS中的几个常见的自定义常量DEDEMEMBER等位置
  11. [HAOI2011]向量
  12. maven+springmvc的配置
  13. IP 地址基本知识
  14. 方法调用 Controller的Action 参数
  15. add web server(nginx)
  16. Python简介及环境安装
  17. React脚手架创建一个React应用以及项目目录结构详解
  18. MySQL中varchar最大长度是多少
  19. 实现一个自定义&lt;table&gt;标签
  20. cordova 框架下开发app推送

热门文章

  1. 2018版OCP考试052最新题库及答案-35题
  2. Metasploit Penetration Testing
  3. Python的并发编程
  4. 使用ceph-deploy进行ceph安装
  5. Beautiful Soup解析网页
  6. windows使用putty向远程服务器传送文件
  7. 一些排序 (python实现)
  8. 微信小程序回到顶部的两种方式
  9. js的跨域问题和解决办法
  10. 4:Median of Two Sorted Arrays