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();
}
}
结果