关于springboot和spring整合redis

扩展:关于redis如何设置密码
1.可以在redis.windows.conf中设置requirepass参数,例如将密码设置为foobared在这里插入图片描述
2.在启动的时候将redis的配置文件加载启动,例如在window中的redis服务器,redis-server.exe redis.windows.conf
在这里插入图片描述

一、关于springboot整合redis
1.在pom文件中引入依赖

		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

2.在yml配置文件中,进行如下配置
在这里插入图片描述
3.service中直接注入RedisTemplate即可使用
在这里插入图片描述
需要注意的是,RedisTemplate默认的是RedisTemplate< String , String >,所以在设置key和value的时候,必须是字符串类型,如果是user对象,则不能自动序列化。如果想要序列化,则必须引入json依赖(这里不再展示json依赖),之后RedisTemplate还必须设置为json序列化策略,如下

        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        User user = new User();
        user.setUsername("王五");
        user.setPassword("123456");
        redisTemplate.boundValueOps("user").set(user);
        System.out.println(redisTemplate.opsForValue().get("user"));

二、关于sping整合redis
1.引入相关依赖

		<dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.7.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.7.2.RELEASE</version>
        </dependency>

2.配置applicationContext-redis.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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
                             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
                            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!-- 连接池基本参数配置,类似数据库连接池 -->
    <context:property-placeholder location="classpath:redis.properties"
                                  ignore-unresolvable="true" />

    <!-- redis连接池 -->
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="${redis.maxActive}" />
        <property name="maxIdle" value="${redis.maxIdle}" />
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />
    </bean>

    <!-- 连接池配置,类似数据库连接池 -->
    <bean id="jedisConnectionFactory"
          class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="${redis.host}"></property>
        <property name="port" value="${redis.port}"></property>
        <property name="password" value="${redis.password}"></property>
        <property name="poolConfig" ref="poolConfig"></property>
    </bean>

    <!--redis操作模版,使用该对象可以操作redis  -->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" >
        <property name="connectionFactory" ref="jedisConnectionFactory" />
        <!--如果不配置Serializer,那么存储的时候缺省使用String,如果用User类型存储,那么会提示错误User can't cast to String!!  -->
        <property name="keySerializer" >
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <property name="valueSerializer" >
            <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
        </property>
        <property name="hashKeySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <property name="hashValueSerializer">
            <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
        </property>
        <!--开启事务  -->
        <property name="enableTransactionSupport" value="true"></property>
    </bean >
</beans>

需要注意的是,这里直接配置的序列化策略就是GenericJackson2JsonRedisSerializer,所以字符串和对象(json串)都可以直接存储。

之后还需要在applicationContext.xml核心配置文件中加入述依赖才能生效

<import resource="classpath:applicationContext-redis.xml"/>

3.在业务层引入RedisTemplate,即可使用

    @Autowired
    private RedisTemplate redisTemplate;

4.假乱码问题
由于spring内部使用的maven最高只支持tomcat7,所以在测试的时候,如果在redis存储和读取中文的话,控制台会出现乱码

redisTemplate.delete("myHash");
        redisTemplate.boundHashOps("myHash").put("BJ","北京");
        redisTemplate.opsForHash().put("myHash", "SH", "上海");
        redisTemplate.opsForHash().put("myHash", "HN", "河南");
        Map<Object, Object> hashCache = redisTemplate.opsForHash().entries("myHash");
        for (Map.Entry entry : hashCache.entrySet()) {
            if(entry.getValue().equals("上海")){
                System.out.println("no problem");
            }
            System.out.println(entry.getKey() + " - " + entry.getValue());
        }

控制台如下乱码:
在这里插入图片描述

但上述乱码其实是假乱码,是控制台中出现的问题,而在程序中加入了下面的代码,打印出no problem

			if(entry.getValue().equals("上海")){
                System.out.println("no problem");
            }

说明在程序中读出到的文字是可以正常识别的,乱码是由于tomcat控制台引起的。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值