第六节 SSM项目集成RedisTemplate以及解决乱码问题

亮子 2024-07-30 07:14:09 7353 0 0 0

1、添加依赖

    <!--redis-->
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-redis</artifactId>
      <version>2.1.20.RELEASE</version>
    </dependency>

2、springRedis.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       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-3.1.xsd ">

    <!--连接池-->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="10" />
        <property name="maxIdle" value="5" />
        <property name="minIdle" value="1" />
        <property name="testOnBorrow" value="true" />
        <property name="testOnReturn" value="true" />
        <property name="testWhileIdle" value="true" />
    </bean>

    <!-- Jedis 链接工厂 -->
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="127.0.0.1" />
        <property name="port" value="6379" />
        <property name="password" value="" />
        <property name="database" value="0" />
        <property name="poolConfig" ref="jedisPoolConfig" />
    </bean>

    <!-- Redis Template -->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory" />
        <!-- 使用 StringRedisSerializer 作为 key 的序列化器 -->
        <property name="keySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <!-- 使用 GenericJackson2JsonRedisSerializer 作为 value 的序列化器 -->
        <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>
    </bean>

	<bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory" />
        <!-- You can customize serializers if needed -->
    </bean>

</beans>

3、使用

package com.bw;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import com.bw.domain.TbRole;
import com.bw.service.TbRoleService;
import com.bw.utils.R;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author 军哥
 * @version 1.0
 * @description: TODO
 * @date 2024/7/30 13:34
 */

@SpringJUnitConfig(locations = "classpath:conf/spring.xml")
public class TestRedis {

    @Autowired
    TbRoleService tbRoleService;

    @Autowired
    RedisTemplate redisTemplate;

    @Test
    public void testRedis() {
        //--1
        Long times = redisTemplate.opsForValue().increment("times");

        //--2
        List<TbRole> list = tbRoleService.list();
        redisTemplate.opsForList().leftPushAll("role:list", list);


        Long size = redisTemplate.opsForList().size("role:list");
        System.out.println("list size:" + size);

        // 修改
        TbRole tbRole = (TbRole)redisTemplate.opsForList().index("role:list", 2);
        tbRole.setRoleName("aaaaaaa");
        redisTemplate.opsForList().set("role:list", 2, tbRole);
        System.out.println(redisTemplate.opsForList().index("role:list", 2));

        //--3
        redisTemplate.opsForValue().set("code", "12345", 1, TimeUnit.MINUTES);
        String code = (String) redisTemplate.opsForValue().get("code");
        if(code != null) {
            System.out.println("code=" + code);
        }

    }

    @Test
    public void testAes() {

        // 生产密钥
        byte[] encoded = SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded();

        // 加密
        AES aes = SecureUtil.aes(encoded);
        byte[] encrypt = aes.encrypt("hello,world");

        // 解密
        byte[] decrypt = aes.decrypt(encrypt);
        String s = new String(decrypt);
        System.out.println(s);
    }

}