Spring Data Redis简单操作

来源:互联网 发布:英国本科留学条件知乎 编辑:程序博客网 时间:2024/05/19 02:26

Spring-data-redis 简单操作

请直接复制代码,如有疑问请加QQ群83402555


maven依赖:

<!-- config redis data and client jar --><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId><version>1.0.2.RELEASE</version></dependency><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>2.1.0</version></dependency>

redis.properties:

redis.host=127.0.0.1redis.port=6379redis.pass=  redis.maxIdle=300redis.maxActive=600redis.maxWait=1000redis.testOnBorrow=true

redis-context.xml:

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="            http://www.springframework.org/schema/beans             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd            http://www.springframework.org/schema/tx             http://www.springframework.org/schema/tx/spring-tx-3.0.xsd            http://www.springframework.org/schema/context            http://www.springframework.org/schema/context/spring-context-3.0.xsd               "><!-- scanner redis properties --><context:property-placeholder location="classpath:/redis.properties" /><bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"><property name="maxIdle" value="300" /><property name="maxActive" value="600" /><property name="maxWait" value="1000" /><property name="testOnBorrow" value="true" /></bean><bean id="connectionFactory"class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"p:host-name="localhost" p:port="6379" p:password=""p:pool-config-ref="poolConfig" /><bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"><property name="connectionFactory" ref="connectionFactory" /><!-- 如果不配置Serializer,那么存储的时候智能使用String,如果用User类型存储,那么会提示错误User can't cast to String!!! --><property name="keySerializer"><beanclass="org.springframework.data.redis.serializer.StringRedisSerializer" /></property><property name="valueSerializer"><beanclass="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /></property></bean></beans>

spring.xml:

<!-- 引入redis属性配置文件 --><import resource="redis-context.xml"/>


RedisService.java

package com.redis.service;public interface RedisService {void set(String id, Object o);Object get(String id);void update(String id,Object o);void delete(String id);}



实现类:RedisServiceImpl.java

package com.redis.service.impl;import javax.annotation.Resource;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.ValueOperations;import org.springframework.stereotype.Service;import com.redis.service.RedisService;/** * 操作redis * @author zhoudong * */@Servicepublic class RedisServiceImpl implements RedisService{@Resourceprivate RedisTemplate redisTemplate;/** * 添加 */public void set(String id, Object o) {ValueOperations<String, Object> valueops = redisTemplate.opsForValue();valueops.set(id, o);}/** * 获取 */public Object get(String id) {ValueOperations<String, Object> valueops = redisTemplate.opsForValue();return valueops.get(id);}/** * 更新 */public void update(String id, Object o) {set(id,o);}/** * 删除 */public void delete(String id) {redisTemplate.delete(id);}}

调用~:

package com.redis.service.impl;import javax.annotation.Resource;import org.apache.log4j.Logger;import org.springframework.stereotype.Service;import com.redis.entity.User;import com.redis.mapper.UserMapper;import com.redis.service.RedisService;import com.redis.service.UserService;/** * redis缓存 spring data redis版本 * @author zhoudong * */@Servicepublic class UserServiceImpl implements UserService{private static Logger logger = Logger.getLogger(UserServiceImpl.class);@Resourceprivate UserMapper userMapper;@Resourceprivate RedisService redisService;/** * 保存 */public void addUser(User user) {userMapper.insert(user);redisService.set(String.valueOf(user.getId()), user);}/** * get */public User findUser(int userId) {User user = (User) redisService.get(String.valueOf(userId));if(user == null){user = userMapper.selectByPrimaryKey(userId);}return user;}/** * update */public void updateUser(User user) {userMapper.updateByPrimaryKey(user);redisService.update(String.valueOf(user.getId()), user);}/** * delete */public void deleteUser(int userId) {userMapper.deleteByPrimaryKey(userId);redisService.delete(String.valueOf(userId));}}

Jedis操作,请关注下一篇。

0 0