引入spring-data-redis包、jedis、connection-pool包

applicationContext.xml的配置

    <!-- redis Connection -->
    <bean id="redisConnection" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
    <property name="hostName" value="localhost"></property>
    <property name="port" value="6379"></property>
    </bean>
    <!-- redisTemplate -->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
    <property name="connectionFactory" ref="redisConnection"></property>
    </bean>

做一个简单的测试

@Test
    public void test1() {
        Jedis jd = new Jedis("localhost",6379);
        String ping = jd.ping();
        System.out.println(ping);
        Set<String> keys = jd.keys("*");
        for(String k:keys){
            System.out.println(k + ":"+ jd.type(k));
        }
        jd.close();
      
    }
    
    @Test
    public void test2(){
        Jedis j=new Jedis("localhost", 6379);
        System.out.println(j.ping());
        Set<String>  filed=  j.hkeys("dept");
        System.out.println(filed);
        for(String s:filed){
            System.out.println(j.hget("dept", s));
        }
        j.close();
    }

自己写一个工具类将,进行序列化与反序列化,

public class SerializableUtil {

    public static byte[] objectToBytes(Object obj) {// 将对象转换为byte数组
        ByteArrayOutputStream baos = null;
        ObjectOutputStream oos = null;
        try {
            baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);
            oos.writeObject(obj);// 将数据序列化后写入到baos中
            byte[] byte1 = baos.toByteArray();
            return byte1;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        } finally {
            try {
                baos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                oos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

    public static Object byteToObject(byte[] bytes) {
        ByteArrayInputStream bais = null;
        ObjectInputStream ois = null;
        try {
            bais = new ByteArrayInputStream(bytes);
            ois = new ObjectInputStream(bais);
            Object obj = ois.readObject();
            return obj;

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        } finally {

            try {
                bais.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                ois.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }
View Code

相关文章:

  • 2022-12-23
  • 2021-08-14
  • 2022-12-23
  • 2019-10-29
  • 2022-01-12
  • 2022-12-23
  • 2022-12-23
  • 2022-01-15
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-06-03
  • 2021-09-29
  • 2021-07-09
  • 2021-09-02
  • 2021-09-15
相关资源
相似解决方案