您当前的位置: 首页 >  redis

梁云亮

暂无认证

  • 3浏览

    0关注

    1211博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Spring Data Redis

梁云亮 发布时间:2020-06-10 22:10:01 ,浏览量:3

第一步:创建项目,按如下所示添加Maven依赖:

    junit
    junit
    4.12
    test


    org.springframework
    spring-context
    5.0.5.RELEASE


    org.springframework
    spring-aop
    5.0.5.RELEASE


    org.springframework
    spring-tx
    5.0.5.RELEASE


    org.springframework
    spring-test
    5.0.5.RELEASE


    commons-logging
    commons-logging
    1.2


    org.springframework.data
    spring-data-redis
    2.0.7.RELEASE


    redis.clients
    jedis
    2.9.0


    com.fasterxml.jackson.core
    jackson-core
    2.9.5


    com.fasterxml.jackson.core
    jackson-databind
    2.9.5


    com.fasterxml.jackson.core
    jackson-annotations
    2.9.5

第二步:创建配置文件,代码如下: redis.properties
redis.pool.maxToal=20
redis.pool.maxIdel=10
redis.pool.minIdel=5
redis.conn.hostName= 127.0.0.1
redis.conn.port=6379
redis.conn.pass=
jedis.xml


    
    
    
    
        
        
        
    
    
    
    
    
        
        
        
        
            
        
        
            
        
    

第三步:创建实体类,代码如下:
public class Dept implements Serializable {
    private  Integer deptno;
    private String dname;
    private String loc;
    //…… getter/setter方法、toString()方法、默认构造方法、全参构造方法
}
第四步:测试代码:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/jedis.xml")
public class RedisTest {
    @Autowired
    private RedisTemplate redisTemplate;
    @Test
    public void fun1() {    //    向Redis添加字符串形式的健值对
        redisTemplate.opsForValue().set("name", "zhangsan");
    }
    @Test
    public void fun2() {
        Object name = redisTemplate.opsForValue().get("name");
        System.out.println(name);
    }
    @Test
    public void fun3() {
        Dept dept = new Dept(123, "sales", "new york");
        redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());//序列化器
        redisTemplate.opsForValue().set("dept", dept);
    }
    @Test
    public void fun4() {
        redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());//序列化器
        Dept dept = (Dept) redisTemplate.opsForValue().get("dept");
        System.out.println(dept);
    }
    @Test
    public void fun5() {
        Dept dept = new Dept(123, "sales", "new york");
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(Dept.class)); 
        redisTemplate.opsForValue().set("dept", dept);
    }
    @Test
    public void fun6() {
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(Dept.class)); 
        Dept dept = (Dept) redisTemplate.opsForValue().get("dept");
        System.out.println(dept);
    } 
}
关注
打赏
1665409997
查看更多评论
立即登录/注册

微信扫码登录

0.0472s