springboot整合RedisJSON
添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.71</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>4.0.0</version>
</dependency>
</dependencies>
redisjson 客户端配置,地址配置修改为自己访问的地址
需要增加redisjson模块,本地没有试成功,使用的redis官网提供的免费申请的redis服务器。
@Configuration
public class RedisJsonConfig {
@Bean
public UnifiedJedis unifiedJedis() {
HostAndPort config = new HostAndPort("localhsot", 6379);
PooledConnectionProvider provider = new PooledConnectionProvider(config);
UnifiedJedis unifiedJedis = new UnifiedJedis(provider);
unifiedJedis.sendCommand(Protocol.Command.AUTH, new String[]{"123456"});
return unifiedJedis;
}
}
HelloService
@Service
public class HelloService {
@Resource
private UnifiedJedis unifiedJedis;
public String start() {
User user = new User();
user.setUsername("zhangsan");
user.setAge(20);
unifiedJedis.jsonSet("user:zhangsan", JSON.toJSONString(user));
User u = unifiedJedis.jsonGet("user:zhangsan", User.class);
System.out.println(u);
return u.toString();
}
}
结果
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)