十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
本篇内容主要讲解“Springboot连接redis的详细教程”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Springboot连接Redis的详细教程”吧!
成都创新互联是专业的塔河网站建设公司,塔河接单;提供成都做网站、网站设计,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行塔河网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
创建springboot项目

在NOSQL中选择Redis

项目目录

pom.xml中还需要加入下面的jar包
org.springframework.boot spring-boot-starter-json
在application.properties文件中添加Redis服务器信息
spring.redis.host=192.168.5.132 spring.redis.port=6379
剩下4个test类,我直接以源码的方式粘出来,里面有些代码是非必须的,我保留了测试的验证过程,所以里面会有冗余代码。(这些代码在我GitHub上的练习项目中也有)
package com.myspringboot.redis;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
ConfigurableApplicationContext configurableApplicationContext = SpringApplication.run(DemoApplication.class, args);
RedisTest redisTest = configurableApplicationContext.getBean(RedisTest.class);
redisTest.testRedis();
}
}package com.myspringboot.redis;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
@Configuration
public class MyTemplate {
@Bean
public StringRedisTemplate getMyTemplate(RedisConnectionFactory redisConnectionFactory){
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(redisConnectionFactory);
stringRedisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializerpackage com.myspringboot.redis;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.hash.Jackson2HashMapper;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
public class RedisTest {
@Autowired
RedisTemplate redisTemplate;
@Autowired
StringRedisTemplate stringRedisTemplate;
@Autowired
ObjectMapper objectMapper;
// 自定义模板
@Autowired
@Qualifier("getMyTemplate")
StringRedisTemplate myStringRedisTemplate;
public void testRedis() {
//redis中直接查看时,乱码
redisTemplate.opsForValue().set("key1", "hello1");
System.out.println(redisTemplate.opsForValue().get("key1"));
//redis中直接查看时,正常
stringRedisTemplate.opsForValue().set("key2", "hello2");
System.out.println(stringRedisTemplate.opsForValue().get("key2"));
RedisConnection connection = redisTemplate.getConnectionFactory().getConnection();
connection.set("key3".getBytes(), "hello3".getBytes());
System.out.println(new String(connection.get("key3".getBytes())));
HashOperations hash = stringRedisTemplate.opsForHash();
hash.put("key4", "name", "张三");
hash.put("key4", "age", "18");
System.out.println(hash.get("key4", "name"));
System.out.println(hash.entries("key4"));
stringRedisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer package com.myspringboot.redis;
public class User {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}到此,相信大家对“Springboot连接Redis的详细教程”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!