pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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.example</groupId>
<artifactId>redis-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>redis-demo</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.14</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

Redis配置:

debug=true
# REDIS (RedisProperties)
# Redis数据库索引(默认为0)
spring.redis.database=
# Redis服务器地址
spring.redis.host=192.168.2.138
# Redis服务器连接端口
spring.redis.port=
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.lettuce.pool.max-active=
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.lettuce.pool.max-wait=-
# 连接池中的最大空闲连接
spring.redis.lettuce.pool.max-idle=
# 连接池中的最小空闲连接
spring.redis.lettuce.pool.min-idle=
# 连接超时时间(毫秒)
spring.redis.timeout=

User.java

import lombok.Getter;
import lombok.Setter; import java.io.Serializable; /**
* Created by zenglw on 2018/3/11.
*/ public class User implements Serializable { private static final long serialVersionUID = -1L; private @Getter @Setter String username;
private @Getter @Setter Integer age; public User(String username, Integer age) {
this.username = username;
this.age = age;
}
}

RedisConf.java

@Configuration
public class RedisConfig { @Bean(name = "userRedisTemplate")
public RedisTemplate<String, User> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, User> template = new RedisTemplate<String, User>();
template.setConnectionFactory(factory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new RedisObjectSerializer());
return template;
}
}

RedisDemoApplicationTests.java

package com.example.demo;

import com.example.demo.bean.User;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import redis.clients.jedis.Jedis; @RunWith(SpringRunner.class)
@SpringBootTest
public class RedisDemoApplicationTests { @Autowired
private StringRedisTemplate stringRedisTemplate; @Autowired
@Qualifier(value = "userRedisTemplate")
private RedisTemplate<String, User> userRedisTemplate; @Test
public void test() throws Exception {
// 保存字符串
stringRedisTemplate.opsForValue().set("aaa", "111");
RedisConnectionFactory connectionFactory = stringRedisTemplate.getConnectionFactory(); Assert.assertEquals("111", stringRedisTemplate.opsForValue().get("aaa"));
} @Test
public void test1() throws Exception {
Jedis jedis = new Jedis("192.168.2.138",6379);
jedis.auth("123456");
jedis.set("age", "1");
System.out.println(jedis.get("age"));
} @Test
public void testUser() throws Exception { User user1 = new User("小红", 10);
User user2 = new User("小白", 20);
User user3 = new User("小李", 30); userRedisTemplate.opsForValue().set(user1.getUsername(), user1);
userRedisTemplate.opsForValue().set(user2.getUsername(), user2);
userRedisTemplate.opsForValue().set(user3.getUsername(), user3); Assert.assertEquals(10, userRedisTemplate.opsForValue().get("小红").getAge().longValue());
Assert.assertEquals(20, userRedisTemplate.opsForValue().get("小白").getAge().longValue());
Assert.assertEquals(30, userRedisTemplate.opsForValue().get("小李").getAge().longValue());
}
}

github

Spring boot集成redis初体验的更多相关文章

  1. (35)Spring Boot集成Redis实现缓存机制【从零开始学Spring Boot】

    [本文章是否对你有用以及是否有好的建议,请留言] 本文章牵涉到的技术点比较多:Spring Data JPA.Redis.Spring MVC,Spirng Cache,所以在看这篇文章的时候,需要对 ...

  2. Spring Boot 2.X(六):Spring Boot 集成Redis

    Redis 简介 什么是 Redis Redis 是目前使用的非常广泛的免费开源内存数据库,是一个高性能的 key-value 数据库. Redis 与其他 key-value 缓存(如 Memcac ...

  3. SpringBoot(十一): Spring Boot集成Redis

    1.在 pom.xml 中配置相关的 jar 依赖: <!-- 加载 spring boot redis 包 --> <dependency> <groupId>o ...

  4. spring boot集成redis基础入门

    redis 支持持久化数据,不仅支持key-value类型的数据,还拥有list,set,zset,hash等数据结构的存储. 可以进行master-slave模式的数据备份 更多redis相关文档请 ...

  5. 【spring boot】【redis】spring boot 集成redis的发布订阅机制

    一.简单介绍 1.redis的发布订阅功能,很简单. 消息发布者和消息订阅者互相不认得,也不关心对方有谁. 消息发布者,将消息发送给频道(channel). 然后是由 频道(channel)将消息发送 ...

  6. spring boot集成redis实现session共享

    1.pom文件依赖 <!--spring boot 与redis应用基本环境配置 --> <dependency> <groupId>org.springframe ...

  7. spring boot 集成 redis lettuce

    一.简介 spring boot框架中已经集成了redis,在1.x.x的版本时默认使用的jedis客户端,现在是2.x.x版本默认使用的lettuce客户端,两种客户端的区别如下 # Jedis和L ...

  8. Spring Boot集成Redis实现缓存机制【从零开始学Spring Boot】

    转自:https://blog.csdn.net/linxingliang/article/details/52263763 spring boot 自学笔记(三) Redis集成—RedisTemp ...

  9. spring boot 集成 redis lettuce(jedis)

    spring boot框架中已经集成了redis,在1.x.x的版本时默认使用的jedis客户端,现在是2.x.x版本默认使用的lettuce客户端 引入依赖 <!-- spring boot ...

随机推荐

  1. gdb coredum 信息例子

    http://blog.csdn.net/hzhsan/article/details/38227647

  2. 编译程序提示配置PKG_CONFIG_PATH

    http://blog.csdn.net/langeldep/article/details/6804331 在安装开源软件的过程中, 经常会碰到提示配置PKG_CONFIG_PATH路径, 或者直接 ...

  3. [剑指Offer] 17.树的子结构

    题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) [思路]要查找树A中是否存在和树B结构一样的子树,可以分成两步: 1.第一步在树A中找到和B的根节 ...

  4. sqoop将oracle数据导入hdfs集群

    使用sqoop将oracle数据导入hdfs集群 集群环境: hadoop1.0.0 hbase0.92.1 zookeeper3.4.3 hive0.8.1 sqoop-1.4.1-incubati ...

  5. P4754 True Vegetable

    题目描述 小A现在有 NN 道题,编号为 1,2,\cdots,N1,2,⋯,N .每道题的起始毒瘤程度为 00 或 11 .在每回合,小A可以将编号连续的 KK 道题的毒瘤程度+1.但小B因为本身比 ...

  6. P3434 [POI2006]KRA-The Disks

    题目描述 For his birthday present little Johnny has received from his parents a new plaything which cons ...

  7. 2017 Multi-University Training Contest - Team 3 Kanade's trio(字典树+组合数学)

    题解: 官方题解太简略了orz 具体实现的方式其实有很多 问题就在于确定A[j]以后,如何找符合条件的A[i] 这里其实就是要提前预处理好 我是倒序插入点的,所以要沿着A[k]爬树,找符合的A[i] ...

  8. win7中输入文件夹首字母跳到相应的文件或者文件夹,却在搜索栏出现输入的字母

    组织->文件夹和搜索选项->查看->在视图中选择键入项

  9. [Leetcode] count and say 计数和说

    The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...

  10. [Leetcode] set matrix zeroes 矩阵置零

    Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click ...