springDataRedis的使用demo(一)

springDataRedis的使用demo(一)

一个小Demo:

1.创建工程
普通的jar工程即可
2.导入依赖

 <properties>
        <spring.version>4.2.4.RELEASE</spring.version>
        <junit.version>4.12</junit.version>
    </properties>
    <dependencies>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
        </dependency>
        <!-- 缓存 -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.8.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.7.2.RELEASE</version>
        </dependency>
    </dependencies>

3.配置文件
连接linux数据库的配置文件
redis.host=192.168.188.131
redis.port=6379
redis.pass=
redis.database=0
redis.maxIdle=300
redis.maxWait=3000
redis.testOnBorrow=true

  1. springRedis使用加载的配置文件
 <?xml version="1.0" encoding="UTF-8"?>
<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:context="http://www.springframework.org/schema/context"
       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">

    <context:property-placeholder location="classpath*:properties/*.properties" />
    <!-- redis 相关配置 -->
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="${redis.maxIdle}" />
        <property name="maxWaitMillis" value="${redis.maxWait}" />
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />
    </bean>
    <bean id="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"/>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="JedisConnectionFactory" />
    </bean>
</beans>

以上就是环境的搭建。

下面来讲一下redis的集中存储方式:
首先创建一个
1. 值操作:

具体写法:

@ContextConfiguration("classpath:applicationContext-redis.xml")
public class Demo {
    @Autowired
    private RedisTemplate redisTemplate;

    public void add(){
        redisTemplate.boundValueOps("name").set("tom");
    }

    public void del(){
        redisTemplate.delete("name");
    }

    public void find(){
        redisTemplate.boundValueOps("name").get();
    }

    public void update(){
        redisTemplate.boundValueOps("name").set("mike");
    }
}

2.list操作:

public void add(){
        redisTemplate.boundListOps("city").leftPush("bj");
        redisTemplate.boundListOps("city").leftPush("sz");
    }

    public void del(){
        //删除第几个的值
        redisTemplate.boundListOps("city").remove(1,"bj");
    }

    public void find(){
        //从第几个查到第几个,不能全部查
        redisTemplate.boundListOps("city").range(0,1);
    }

    public void update(){
        //找到key值,选择改第几个值
        redisTemplate.boundListOps("city").set(0,"sh");
    }
}

3.** map操作:**

 public void add(){
        redisTemplate.boundHashOps("students").put("A","a");
        redisTemplate.boundHashOps("students").put("C","c");
    }

    public void del(){
        /
        redisTemplate.boundHashOps("student").delete(keys);
    }

    public void find(){
        //得到student下的所有key值
        Set student = redisTemplate.boundHashOps("student").keys();
        for (String s:student){
            //根据key值得到对应的value
            redisTemplate.boundHashOps("student").get(s);
        }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值