java 连接 redis
2022/8/28 2:24:00
本文主要是介绍java 连接 redis,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Jedis
导入对应的依赖
<dependencies> <!-- jedis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>4.2.3</version> </dependency> <!-- fastjson--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.79</version> </dependency> </dependencies>
连接数据库
记得在本地打开redis
// 1. new Jedis Jedis jedis = new Jedis("127.0.0.1",6379); System.out.println(jedis.ping());
测试
System.out.println("添加一个值username:"+jedis.set("username", "huisnaya")); System.out.println("添加一个值password:"+jedis.set("password", "123456")); System.out.println("查询一个值username:"+jedis.get("username")); System.out.println("查询一个值password:"+jedis.get("password")); System.out.println("查询所有的键:"+jedis.keys("*")); System.out.println("清空当前数据库;" + jedis.flushDB()); /** 结果: 添加一个值username:OK 添加一个值password:OK 查询一个值username:huisnaya 查询一个值password:123456 查询所有的键:[password, username] 清空当前数据库;OK **/
事务成功
public static void main(String[] args) { Jedis jedis = new Jedis("127.0.0.1", 6379); Transaction multi = jedis.multi(); //开启事务 try { multi.set("user1","hahah");//指令入队 multi.set("user2","xixixi"); multi.exec();//执行事务 }catch (Exception e){ multi.discard(); //放弃事务 e.printStackTrace(); }finally { System.out.println(jedis.get("user1")); System.out.println(jedis.get("user2")); jedis.close();//关闭连接 } } 结果: hahah xixixi
事务失败
public static void main(String[] args) { Jedis jedis = new Jedis("127.0.0.1", 6379); jedis.flushDB(); //清空上一次的值 Transaction multi = jedis.multi(); //开启事务 try { multi.set("user1","hahah");//指令入队 multi.set("user2","xixixi"); int i = 1/0; multi.exec();//执行事务 }catch (Exception e){ multi.discard(); //放弃事务 e.printStackTrace(); }finally { System.out.println(jedis.get("user1")); System.out.println(jedis.get("user2")); jedis.close();//关闭连接 } } 结果: null null
java远程连接reids
第一步开启6379这个端口号,防火墙要开启
firewall-cmd --zone=public --add-port=8080/tcp --permanent 开启8080端口防火墙 systemctl restart firewalld.service 刷新防火墙 firewall-cmd --list-ports 查看防火墙的端口
第二步修改配置文件
注意:用redis-cli连接时要登录面
[root@shuisanya bin]# redis-cli 127.0.0.1:6379> auth 123456 OK
java测试
public class Test { public static void main(String[] args) { // 1. new Jedis Jedis jedis = new Jedis("47.96.26.127",6379); jedis.auth("123456"); System.out.println("添加一个值username:"+jedis.set("username", "huisnaya")); System.out.println("添加一个值password:"+jedis.set("password", "123456")); System.out.println("查询一个值username:"+jedis.get("username")); System.out.println("查询一个值password:"+jedis.get("password")); System.out.println("查询所有的键:"+jedis.keys("*")); // System.out.println("清空当前数据库;" + jedis.flushDB()); jedis.close(); } }
这个时候你在redis-cli中查看就有了
127.0.0.1:6379> keys * 1) "username" 2) "password" 127.0.0.1:6379> get username "huisnaya" 127.0.0.1:6379>
SpringBoot 整合 redis
导入依赖
<!-- redis--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
编写配置文件
spring.redis.host=127.0.0.1 spring.redis.port=6379
测试
@Test void contextLoads() { redisTemplate.opsForValue().set("haha","哈哈哈"); System.out.println(redisTemplate.opsForValue().get("haha")); System.out.println(redisTemplate.keys("*")); }
编写一个配置类 来序列化操作
package com.mhy.springredis.config; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; 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.RedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory); // 配置具体的序列化方式 Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(objectMapper); StringRedisSerializer redisSerializer = new StringRedisSerializer(); template.setKeySerializer(redisSerializer); template.setHashKeySerializer(redisSerializer); template.setValueSerializer(jackson2JsonRedisSerializer); template.setHashValueSerializer(jackson2JsonRedisSerializer); return template; } }
这篇关于java 连接 redis的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-25Springboot应用的多环境打包项目实战
- 2024-11-25SpringBoot应用的生产发布项目实战入门教程
- 2024-11-25Viite多环境配置项目实战:新手入门教程
- 2024-11-25Vite多环境配置项目实战入门教程
- 2024-11-25Springboot应用的生产发布资料:新手入门教程
- 2024-11-25创建springboot项目资料:新手入门教程
- 2024-11-25创建Springboot项目资料:新手入门教程
- 2024-11-25JAVA创业资料:初学者必备的JAVA创业指南
- 2024-11-25Java创业资料:新手入门必备Java编程教程与创业指南
- 2024-11-25JAVA语音识别项目项目实战入门教程