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
- spring-data-redis简单操作
- Spring Data Redis简单操作
- spring-data-redis 操作
- spring data redis 操作redis
- spring data redis 操作redis
- Spring Data Redis简单搭建
- spring-data-redis相关操作
- Spring data redis 操作list
- 使用spring-data-redis操作redis
- spring-data-redis操作redis集群
- spring-data-redis连接操作redis
- Spring-data-redis操作Redis的Sentinel
- Spring-data-redis操作redis知识总结
- Spring-data-redis操作redis知识总结
- spring-data-redis连接操作redis
- Spring-data-redis的简单使用
- 使用Spring-data进行Redis操作
- redis:spring-data-redis
- 分布式设计与开发(二)------几种必须了解的分布式算法
- Lua中的捕获(string.find的综合使用)
- 黑马程序员-----高新技术-----java5新特性详解
- iOS-AppDelegate中回调方法简单介绍
- Android为控件之间添加分割线
- Spring Data Redis简单操作
- 简单是否的简单封装方法
- 为什么成为一名程序员这么难
- Node.js Express 框架学习
- getParam和getAttribute区别
- 分布式设计与开发(三)------高一致性服务ZooKeeper
- WPF学习网址
- 程序员编程面试取胜的8个技巧
- JOBDU-OJ 1452 搬寝室