springboot整合redis(Jeids)

第一步:配置yml文件

redis:
        host: 127.0.0.1
        port: 6379
        password:
        # 连接超时时间(毫秒)
        timeout: 10000
        pool:
          max-idle: 20
          min-idle: 5
          max-active: 20
          max-wait: 2

第二步:编写配置类

   

package org.spring.springcloud.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Created by liyu on 2018/4/27.
 */
@Configuration
public class RedisConfiguration {

    //(一)第一步:配置连接池,
    /*
    @ConfigurationProperties和@Bean和在一起使用
    举个例子,我们需要用@Bean配置一个Config对象,Config对象有a,b,c成员变量需要配置,
    那么我们只要在yml或properties中定义了a=1,b=2,c=3,然后通过@ConfigurationProperties就能把值注入进Config对象中
    */
    @ConfigurationProperties(prefix="redis.pool")
    public JedisPoolConfig getRedisConfig(){
        JedisPoolConfig config = new JedisPoolConfig();
        System.out.println(" max-idle="+config.getMaxIdle());
        return config;
    }


    //第二步:配置工厂类JedisConnectionFactory,并设置ip,端口和连接池
    @Bean
    @ConfigurationProperties(prefix="redis")
    public JedisConnectionFactory getConnectionFactory(){
        JedisConnectionFactory factory = new JedisConnectionFactory();
        factory.setUsePool(true);
        JedisPoolConfig config = getRedisConfig();
        factory.setPoolConfig(config);
        return factory;
    }
    
    //第三步:创建StringRedisTemplate操作类,程序中可直接使用该类进行增加,删除等操作
    @Bean
    public RedisTemplate<?, ?> getRedisTemplate(){
        RedisTemplate<?,?> template = new StringRedisTemplate(getConnectionFactory());
        return template;
    }

第三步:程序插入查询

package org.spring.springcloud.service.impl;


import org.spring.springcloud.dao.CityDao;
import org.spring.springcloud.domain.City;
import org.spring.springcloud.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


@Service
public class CityServiceImpl implements CityService {

    @Autowired
    private CityDao cityDao;

    //存对象,被存对象必须序列化
    @Autowired
    RedisTemplate redisTemplate;

    //存String
    @Autowired
    StringRedisTemplate template;

    //存指定对象,city对象必须序列化
    @Resource(name = "redisTemplate")
    private RedisTemplate<String,City> cityRedisTemplate;

    public City findCityByName(String cityName) {
    //  City city=cityDao.findByName(cityName);
        //设置字符串
        //template.opsForValue().set("mykey100", "helloredis");
        template.opsForValue().set("hk2","hello hk2");
        System.out.println("-----------"+template.opsForValue().get("hk2"));

        //设置对象
       // ValueOperations<String, City> operations = redisTemplate.opsForValue();
        //operations.set("liyu", city);
        // 缓存存在
//        boolean hasKey = redisTemplate.hasKey("liyu");
//        if (hasKey) {
//            System.out.println("....>>"+operations.get("liyu"));
//        }
        return cityDao.findByName(cityName);
    }
}

从上面可以看到我们已经成功插入了值

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值