spring data redis的使用

spring data redis就是将jedis进行了高度分封装,可以直接通过redisTemplate对象来直接操作redis这种nosql型的数据库

1.第一步:导入jar包,包括spring的jar包 jedis的ja包和springdata redis的jar包
4.2.4.RELEASE
org.springframework
spring-context
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-aspects
${spring.version}
org.springframework
spring-jms
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-test
${spring.version}
redis.clients
jedis
2.8.1
org.springframework.data
spring-data-redis
1.7.2.RELEASE
2.第二步:配置spring data redis 的配置文件和redis的属性文件

<属性文件>
# Redis settings
# server IP
redis.host=127.0.0.1
# server port
redis.port=6379
# server pass
redis.pass=
# use dbIndex
redis.database=0
# \u63A7\u5236\u4E00\u4E2Apool\u6700\u591A\u6709\u591A\u5C11\u4E2A\u72B6\u6001\u4E3Aidle(\u7A7A\u95F2\u7684)\u7684jedis\u5B9E\u4F8B
redis.maxIdle=300
# \u8868\u793A\u5F53borrow(\u5F15\u5165)\u4E00\u4E2Ajedis\u5B9E\u4F8B\u65F6\uFF0C\u6700\u5927\u7684\u7B49\u5F85\u65F6\u95F4\uFF0C\u5982\u679C\u8D85\u8FC7\u7B49\u5F85\u65F6\u95F4(\u6BEB\u79D2)\uFF0C\u5219\u76F4\u63A5\u629B\u51FAJedisConnectionException\uFF1B
redis.maxWait=3000
# \u5728borrow\u4E00\u4E2Ajedis\u5B9E\u4F8B\u65F6\uFF0C\u662F\u5426\u63D0\u524D\u8FDB\u884Cvalidate\u64CD\u4F5C\uFF1B\u5982\u679C\u4E3Atrue\uFF0C\u5219\u5F97\u5230\u7684jedis\u5B9E\u4F8B\u5747\u662F\u53EF\u7528\u7684
redis.testOnBorrow=true

配置文件>
"JedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/>
redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
3.第三步,就是实际使用(注入相关的操作对象redisTemplate就可以调用相关api来进行调用了)

@Autowired
private TbContentMapper contentMapper;

@Autowired
private RedisTemplate redisTemplate;

@Override
public void update(TbContent content){
//查询原来的分组ID
Long categoryId = contentMapper.selectByPrimaryKey(content.getId()).getCategoryId();
//清除原分组的缓存
redisTemplate.boundHashOps("content").delete(categoryId)
contentMapper.updateByPrimaryKey(content);
//清除现分组缓存
if(categoryId.longValue()!=content.getCategoryId().longValue()){
redisTemplate.boundHashOps("content").delete(content.getCategoryId());
}
}

你可能感兴趣的:(spring,data,redis)