java 李斯特_Spring 中使用 Redis

Spring 集成 Redis

Maven配置

redis.clients

jedis

2.3.1

org.springframework.data

spring-data-redis

1.5.0.RELEASE

org.slf4j

slf4j-log4j12

1.7.10

Spring 配置

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"

xmlns:p="http://www.springframework.org/schema/p"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

annotation-config="true" />

class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"

destroy-method="destroy">

p:connection-factory-ref="jedisConnectionFactory">

class="org.springframework.data.redis.serializer.StringRedisSerializer" />

class="org.springframework.data.redis.serializer.StringRedisSerializer" />

class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />

class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />

p:connection-factory-ref="jedisConnectionFactory" />

Java代码

实体类

package cn.slimsmart.redis.spring.data.redis.demo;

import java.io.Serializable;

import java.util.Date;

public class Order implements Serializable{

private static final long serialVersionUID = 1L;

private String id;

private String orderNo;

private double price;

private Date createDate;

public Order(String id,String orderNo,double price,Date createDate){

this.id = id;

this.orderNo = orderNo;

this.price = price;

this.createDate = createDate;

}

public Order(){

}

public String getId() {

return id;

}

public void setId(String id) {

this.id = id;

}

public String getOrderNo() {

return orderNo;

}

public void setOrderNo(String orderNo) {

this.orderNo = orderNo;

}

public double getPrice() {

return price;

}

public void setPrice(double price) {

this.price = price;

}

public Date getCreateDate() {

return createDate;

}

public void setCreateDate(Date createDate) {

this.createDate = createDate;

}

}

redis操作类

package cn.slimsmart.redis.spring.data.redis.demo;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisOperations;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.data.redis.core.ValueOperations;

import org.springframework.stereotype.Repository;

@Repository

public class OrderDao {

@Autowired

private RedisTemplate redisTemplate;

public void save(Order order) {

/*redisTemplate.opsForList();

redisTemplate.opsForSet();

redisTemplate.opsForHash()*/

ValueOperations valueOper = redisTemplate.opsForValue();

valueOper.set(order.getId(), order);

}

public Order read(String id) {

ValueOperations valueOper = redisTemplate.opsForValue();

return valueOper.get(id);

}

public void delete(String id) {

ValueOperations valueOper = redisTemplate.opsForValue();

RedisOperations RedisOperations = valueOper.getOperations();

RedisOperations.delete(id);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值